permission.py 639 B

123456789101112131415161718192021
  1. from rest_framework_api_key.permissions import BaseHasAPIKey
  2. from .models import APIKey
  3. from rest_framework import permissions
  4. class HasAPIKey(BaseHasAPIKey):
  5. model = APIKey
  6. class HAsKeyPermissionn(permissions.BasePermission):
  7. """
  8. Object-level permission to only allow owners of an object to edit it.
  9. Assumes the model instance has an `owner` attribute.
  10. """
  11. def has_object_permission(self, request, view, obj):
  12. print(request)
  13. if request.method in permissions.SAFE_METHODS:
  14. return True
  15. # Instance must have an attribute named `owner`.
  16. return obj.owner == request.user