Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. REST_FRAMEWORK = {
  2. 'DEFAULT_AUTHENTICATION_CLASSES': (
  3. 'rest_framework.authentication.TokenAuthentication',
  4. 'rest_framework.authentication.BasicAuthentication',
  5. 'rest_framework.authentication.SessionAuthentication',
  6. ),
  7. }
  8.  
  9. class ContactViewSet(viewsets.ModelViewSet):
  10. queryset = Contact.objects.all()
  11. serializer_class = ContactSerializer
  12. permission_classes = (IsAuthenticated,)
  13.  
  14. def perform_create(self, serializer):
  15. serializer.save(user_id=self.request.user)
  16.  
  17. class AdminAuthenticationPermission(permissions.BasePermission):
  18. ADMIN_ONLY_AUTH_CLASSES = [rest_framework.authentication.BasicAuthentication, rest_framework.authentication.SessionAuthentication]
  19.  
  20. def has_permission(self, request, view):
  21. user = request.user
  22. if user and user.is_authenticated():
  23. return user.is_superuser or
  24. not any(isinstance(request._authenticator, x) for x in self.ADMIN_ONLY_AUTH_CLASSES)
  25. return False
  26.  
  27. class ContactViewSet(viewsets.ModelViewSet):
  28. queryset = Contact.objects.all()
  29. serializer_class = ContactSerializer
  30. permission_classes = (IsAuthenticated, AdminAuthenticationPermission,)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement