I have a very simple Django decorator my_decorator1
that looks like this:
def my_decorator_1(function):
@wraps(function)
def decorator(self, *args, **kwargs):
self.my_val = random.randint(0,1)
return function(self, *args, **kwargs)
return decorator
My Django APIView that looks like this:
class MyApiView(views.APIView):
@what_decorator_goes_here
@my_decorator_1
def post(self, request, *args, **kwargs):
"""
Blah Blah Blah. The rest is snipped out for brevity.
"""
Now I want a decorator to grant access to MyApiView iff self.my_val == 1. Otherwise it should give a permission denied error (403). How can I do it? I need it to replace @what_decorator_goes_here
. This can't be that uncommon of a workflow.
Isn't there a prewritten django decorator I can use for this purpose? The two similar ones that I have seen are @user_passes_test
and @permission_required
. However neither of them operate on the self
argument. The first one assumes the input is a User
and the second one takes something different.