本文整理汇总了Python中rest_framework.permissions.IsAuthenticated方法的典型用法代码示例。如果您正苦于以下问题:Python permissions.IsAuthenticated方法的具体用法?Python permissions.IsAuthenticated怎么用?Python permissions.IsAuthenticated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.permissions
的用法示例。
在下文中一共展示了permissions.IsAuthenticated方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_auth_required
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def test_auth_required(rf):
class RestrictedPersonViewSet(PersonViewSet):
permission_classes = [IsAuthenticated]
data = dump_json({"people": {"name": "Jason Api"}})
request = rf.post(
reverse("person-list"), data=data,
content_type="application/vnd.api+json")
view = RestrictedPersonViewSet.as_view({'post': 'create'})
response = view(request)
response.render()
assert response.status_code == 403, response.content
assert not models.Person.objects.exists()
results = {
"errors": [{
"status": "403",
"title": "Authentication credentials were not provided."
}]
}
assert response.content == dump_json(results)
示例2: add_viewset
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def add_viewset(table):
data_index = table.name
record_data_index = "{}.".format(table.name)
deleted_data_index = "{}..".format(table.name)
def retrieve(self, request, *args, **kwargs):
try:
res = es.search(index=record_data_index, doc_type="record-data", body={"query": {"term": {"S-data-id": kwargs["pk"]}}}, sort="S-update-time:desc")
except NotFoundError as exc:
raise exceptions.NotFound("Document {} was not found in Type data of Index {}".format(kwargs["pk"], record_data_index))
except TransportError as exc:
return Response([])
return Response(res["hits"])
viewset = type(table.name, (mixins.RetrieveModelMixin, viewsets.GenericViewSet), dict(
permission_classes=(permissions.IsAuthenticated, ), retrieve=retrieve))
setattr(views, table.name, viewset)
return viewset
示例3: get
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get(self, request):
"""Get"""
quest = Question.objects.filter(test_id=request.GET.get("pk", None)).order_by("-id")
counter = CompleteQuestion().get_counter(request.user, request.GET.get("pk", None))
serializer = QuestionSerializer(quest, many=True)
return JsonResponse(serializer.data, safe=False)
# class QuestionsInTest(BlankGetAPIView):
# """
# Вывод вопросов в отдельном тесте,
# параметр: pk, значение: id теста, вопросы которого нужны
# """
# permission_classes = [permissions.IsAuthenticated]
# model = Question
# serializer = QuestionSerializer
# filter_name = 'test_id'
# order_params = 'id'
示例4: test_bearer_authentication
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def test_bearer_authentication(self):
@api_view(['GET'])
@permission_classes([IsAuthenticated])
@authentication_classes([BearerAuthentication])
def my_view(request):
return Response({})
request = self.factory.get('/')
response = my_view(request)
self.assertEqual(response.status_code, 401)
token = self._obtain_auth_token()
request = self.factory.get('/', HTTP_AUTHORIZATION=f'Bearer {token}')
response = my_view(request)
self.assertEqual(response.status_code, 200)
示例5: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
"""Assign permission based on action."""
permissions = [IsAuthenticated, IsActiveCircleMember]
if self.action in ['update', 'partial_update', 'finish']:
permissions.append(IsRideOwner)
if self.action == 'join':
permissions.append(IsNotRideOwner)
return [p() for p in permissions]
示例6: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
"""Assign permissions based on action."""
permissions = [IsAuthenticated]
if self.action != 'create':
permissions.append(IsActiveCircleMember)
if self.action == 'invitations':
permissions.append(IsSelfMember)
return [p() for p in permissions]
示例7: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
"""Assign permissions based on action."""
permissions = [IsAuthenticated]
if self.action in ['update', 'partial_update']:
permissions.append(IsCircleAdmin)
return [permission() for permission in permissions]
示例8: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
if self.action == 'list' or self.action == 'retrieve':
permission_classes = [permissions.IsAuthenticated]
else:
permission_classes = [permissions.IsAdminUser]
return [permission() for permission in permission_classes]
示例9: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
if self.action == 'partial_update' or self.action == 'update':
permission_classes = [permissions.IsAdminUser]
elif self.action == 'list' or self.action == 'retrieve':
permission_classes = [permissions.IsAuthenticated]
else:
permission_classes = [DenyAny]
return [permission() for permission in permission_classes]
示例10: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
if self.action == 'image':
permission_classes = ()
elif self.action in ('list', 'retrieve', 'conversation'):
permission_classes = (IsAuthenticated, )
else:
permission_classes = (IsAuthenticated, IsOfferUser)
return [permission() for permission in permission_classes]
示例11: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.IsAuthenticated(),)
if self.request.method == 'POST':
return (permissions.AllowAny(),)
return (permissions.IsAuthenticated(), IsAccountOwner(),)
示例12: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
"""
Instantiates and returns the list of permissions that this view requires.
"""
if self.action == "create":
# Allow any user to create an account, but limit other actions to logged-in users.
permission_classes = [AllowAny]
else:
permission_classes = [IsAuthenticated]
return [permission() for permission in permission_classes]
示例13: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
if self.action == 'create':
return []
elif self.action == 'update':
return [permissions.IsAuthenticated()]
return [permissions.IsAuthenticated()]
示例14: as_view
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def as_view(cls, *args, **kwargs):
view = super(AuthenticatedGraphQLView, cls).as_view(*args, **kwargs)
view = permission_classes((IsAuthenticated,))(view)
view = authentication_classes(api_settings.DEFAULT_AUTHENTICATION_CLASSES)(view)
view = throttle_classes(api_settings.DEFAULT_THROTTLE_CLASSES)(view)
view = api_view(["GET", "POST"])(view)
view = csrf_exempt(view)
return view
示例15: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAuthenticated [as 别名]
def get_permissions(self):
"""Define custom permissions for different methods"""
# at minimum require users to be authenticated
self.permission_classes = [IsAuthenticated]
# for PUT requests require users to be admins
if self.request.method == 'PUT':
self.permission_classes.append(IsAdminUser)
return super(viewsets.ViewSet, self).get_permissions()