Im using Django Rest Framework . I want to give permission class to RetrieveUpdateDestroyAPI View
My permission class:
class AssetItemPermission(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
user = request.user
shared_item_course = False
is_content_author = PermissionHelper().check_permission(request.user, ['create_content'])
courses = Course.objects.filter(module=obj)
if any(obj.tenant in course.tenants.all() for course in courses):
shared_item_course = True
elif any(obj.status==TenantShareItemStatusValues.SHARED_TO_ALL.value for course in courses):
shared_item_course = True
if Enrolment.objects.filter(enrollable__in=courses, enrollee=request.user, status=EnrolmentStatus(short_name=EnrolmentStatusValues.APPROVED.value)).exists():
print("Is enrolled by user")
return True
elif is_content_author and obj.tenant == request.user.tenant:
print("is content author and it is in tenant")
return True
elif is_content_author and shared_item_course:
print("is content author and it is in tenant share item")
return True
return False
My View:
class AssetItemView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = AssetItemsSerializer
permission_classes = [AssetItemPermission]
def get_queryset(self):
return Module.objects.filter(id=self.kwargs['pk'])
It works but i see the permission check is firing 5 times in my case:
For example The print statement "is content author and it is in tenant" is printing 5 times instead of one.WHy is this firing 5 times?
from Object permission function firing more than once
No comments:
Post a Comment