permissions.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from rest_framework.permissions import BasePermission
  2. class APIPermission(BasePermission):
  3. allow_read_only = False
  4. @staticmethod
  5. def is_safe(request):
  6. return request.method in ["GET", "HEAD", "OPTIONS"]
  7. class AllowAny(APIPermission):
  8. def has_permission(self, request, view):
  9. return True
  10. class IsAdmin(APIPermission):
  11. def has_permission(self, request, view):
  12. return request.user and getattr(request.user, "is_admin", False)
  13. class IsStaff(APIPermission):
  14. def has_permission(self, request, view):
  15. return request.user and getattr(request.user, "is_staff", False)
  16. class IsStaffOrAdmin(APIPermission):
  17. def has_permission(self, request, view):
  18. return request.user and (getattr(request.user, "is_admin", False) or getattr(request.user, "is_staff", False))
  19. class IsAuthenticated(APIPermission):
  20. def has_permission(self, request, view):
  21. return request.user and request.user.is_authenticated
  22. class IsOwner(APIPermission):
  23. def has_object_permission(self, request, view, obj):
  24. return request.user and obj.user == request.user