Created
September 8, 2015 11:51
Revisions
-
jperelli created this gist
Sep 8, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ # Usage <pre> class SomeViewSet(viewsets.ModelViewSet): queryset = models.Some.objects.all() serializer_class = SomeSerializer @detail_route(methods=['post']) @route_permissions('some_app.some_permission') def custom_method(self, request, pk=None): return Response(...) </pre> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ from rest_framework.exceptions import PermissionDenied def route_permissions(permission): """ django-rest-framework permission decorator for custom methods """ def decorator(drf_custom_method): def _decorator(self, *args, **kwargs): if self.request.user.has_perm(permission): return drf_custom_method(self, *args, **kwargs) else: raise PermissionDenied() return _decorator return decorator