I've set up a custom permissions class to be reused from multiple views, in an app where some users have ownership-like rights on behalf of other users:
class IsOwnerLike(permissions.BasePermission):
def has_permission(self, request, view):
if (
user_is_owner(request.user, request.data["owned_by"])
| user_is_owner_like(request.user, request.data["owned_by"])
):
return True
return False
This works as expected for one ModelViewSet.
However, for legacy reasons, different requests coming in to different views may not have an "owned_by" data element -- it may be called "owned", "owner", "created_by", etc. -- and therefore I can't reuse this custom permission as written.
What is the correct way to abstract things at the viewset, to normalize data being passed to my custom permissions class? Can this be done, or should I be thinking about handling these permissions differently?