3

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?

David Schumann
  • 13,380
  • 9
  • 75
  • 96
  • Found the answer here http://stackoverflow.com/questions/12993951/using-etag-last-modified-decorators-with-djangos-class-based-generic-views – David Schumann Apr 10 '15 at 15:06

2 Answers2

2

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.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
  • I have successfully used the django etag mixins by using the fix described [here](http://stackoverflow.com/questions/12993951/using-etag-last-modified-decorators-with-djangos-class-based-generic-views). By creating a superclass that overwrites the dispatch method the code is not that ugly – David Schumann Apr 13 '15 at 14:55
2

You can use django-rest-framework-condition

Install it:

pip install django-rest-framework-condition

Use it in the same way as decorator from Django:

from rest_framework_condition import condition

class MyView(GenericAPIView):
    serializer_class = MySerializer

    @condition(etag_func=get_language_etag)
    def get(self, request, *args, **kwargs):
        return Response(self.get_cached_response())
jozo
  • 4,232
  • 1
  • 27
  • 29