Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @condition decorator for REST view classes

I am trying to use the ETAG HTTP header to send 304 NOT MODIFIED responses. The following code is used:

class MyView(GenericAPIView):
    serializer_class = MySerializer

    @condition(etag_func=get_language_etag)
    def get(self, request, *args, **kwargs):
        return Response(self.get_cached_response())

The problem lies in the 'self' parameter of the get method. This jumbles the parameters in the @condition generator method here the beginning of the condition method:

def condition(etag_func=None, last_modified_func=None):
    def decorator(func):
        @wraps(func, assigned=available_attrs(func))
        def inner(request, *args, **kwargs):

as now 'self' gets assigned to request and the actual requests ends up in *args.

Has anyone had a similar problem concerning decorators and their expected order of parameters?

like image 933
David Schumann Avatar asked Nov 22 '25 01:11

David Schumann


1 Answers

drf-extensions provides caching and ETag mixins that you can use on your views, instead of using the ones provided by Django.

https://chibisov.github.io/drf-extensions/docs/#cache-etag-mixins

It is not possible to use the methods provided by Django before DRF does not use the standard HttpResponse classes, and most of the decorators are expecting it.

like image 98
Kevin Brown-Silva Avatar answered Nov 24 '25 13:11

Kevin Brown-Silva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!