I want to do something akin to this, which (understandably) results in a SyntaxError:
@api_view(['GET'])
if settings.USE_API_KEY:
@authentication_classes((TokenAuthentication, ))
@permission_classes((IsAuthenticated, ))
def all_announcements(request):
announcements = Announcement.objects.all()
serializer = AnnouncementSerializer(announcements, many=True)
return Response(serializer.data)
How can I implement something similiar to this?
I would not write a decorator for this - rather note that the decorator syntax allows a function call that returns a decorator (and indeed, this is what decorators-with-parameters actually do). So:
def maybe_decorate(condition, decorator):
return decorator if condition else lambda x: x
@api_view(['GET'])
@maybe_decorate(settings.USE_API_KEY, authentication_classes((TokenAuthentication, )))
@maybe_decorate(settings.USE_API_KEY, permission_classes((IsAuthenticated, )))
def all_announcements(request):
...
When you use a decorator like this:
@decorator
def f():
...
It's equivalent to this:
def f():
...
f = decorator(f)
With that in mind, you could accomplish what you want like this:
def all_announcements(request):
announcements = Announcement.objects.all()
serializer = AnnouncementSerializer(announcements, many=True)
return Response(serializer.data)
if settings.USE_API_KEY:
all_announcements = permission_classes((IsAuthenticated,))(all_announcements)
all_announcements = authentication_classes((TokenAuthentication,))(all_announcements)
all_announcements = api_view(['GET'])(all_announcements)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With