我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用rest_framework.permissions.AllowAny()。
def setUp(self): self.endpoint = Endpoint(model=Product) self.fields = tuple([field.name for field in Product._meta.get_fields()]) self.permission_classes = (AllowAny, ) self.filter_fields = ('name', 'category_id') self.search_fields = ('id', 'name') self.ordering_fields = ('name', ) self.page_size = 2 self.alternate_endpoint = Endpoint(model=Product, permission_classes=self.permission_classes, filter_fields=self.filter_fields, search_fields=self.search_fields, ordering_fields=self.ordering_fields, page_size=self.page_size)
def get_swagger_view(title=None, url=None, patterns=None, urlconf=None): """ Returns schema view which renders Swagger/OpenAPI. """ class SwaggerSchemaView(APIView): _ignore_model_permissions = True exclude_from_schema = True permission_classes = [AllowAny] renderer_classes = [ CoreJSONRenderer, CustomOpenAPIRenderer, renderers.SwaggerUIRenderer ] def get(self, request): generator = SchemaGenerator( title=title, url=url, patterns=patterns, urlconf=urlconf ) schema = generator.get_schema(request=request) if not schema: raise exceptions.ValidationError( 'The schema generator did not return a schema Document' ) return Response(schema) return SwaggerSchemaView.as_view()
def get_permissions(self): if self.request.method in permissions.SAFE_METHODS: return (permissions.AllowAny(),) if self.request.method == 'POST': return (permissions.AllowAny(),) return (permissions.IsAuthenticated(),IsAccountOwner(),)
def get_permissions(self): if self.request.method in permissions.SAFE_METHODS: return (permissions.AllowAny(),) return (permissions.IsAuthenticated(), IsAuthor(),)
def get_queryset(self): user_id = self.kwargs['userid'] sorting = self.request.query_params.get('sorting', None) if sorting == 'upvotes': return Comment.sorted_objects.upvotes().filter(author=user_id) elif sorting == 'newest': return Comment.sorted_objects.newest().filter(author=user_id) else: return Comment.sorted_objects.upvotes().filter(author=user_id) # class PostUpvoteViewSet(viewsets.ModelViewSet): # queryset = PostUpvote.objects.all() # serializer_class = PostUpvoteSerializer # # def get_permissions(self): # if self.request.method in permissions.SAFE_METHODS: # return (permissions.AllowAny(),) # return (permissions.IsAuthenticated(), IsOwner(),) # # def perform_create(self, serializer): # instance = serializer.save(voter=self.request.user) # return super(PostUpvoteViewSet, self).perform_create(serializer) # # # class CommentUpvoteViewSet(viewsets.ModelViewSet): # queryset = PostUpvote.objects.all() # serializer_class = CommentUpvoteSerializer # # def get_permissions(self): # if self.request.method in permissions.SAFE_METHODS: # return (permissions.AllowAny(),) # return (permissions.IsAuthenticated(), IsOwner(),) # # def perform_create(self, serializer): # instance = serializer.save(voter=self.request.user) # return super(PostUpvoteViewSet, self).perform_create(serializer)
def test_allow_any(self): viewset = views.SupportTicketViewSet() self.assertIn(permissions.AllowAny, viewset.permission_classes)