diff --git a/rest_framework/cache_lookups.py b/rest_framework/cache_lookups.py index 2d9271b0c..2973d5029 100644 --- a/rest_framework/cache_lookups.py +++ b/rest_framework/cache_lookups.py @@ -20,6 +20,7 @@ class ETagCacheLookup(BaseCacheLookup): """ """ etag_variable = 'etag' + request_header = 'HTTP_IF_NONE_MATCH' @staticmethod def get_cache_key(cls, pk): @@ -32,19 +33,22 @@ class ETagCacheLookup(BaseCacheLookup): def get_etag(self, obj): return getattr(obj, self.etag_variable) - def get_header(self, obj): + def get_request_header(self): + return self.request_header + + def get_response_header(self, obj): key = self.get_cache_key(obj, 'pk') etag = self.get_etag(obj) cache.set(key, etag) return {'ETag': etag} def precondition_check(self, obj, request): - if self.get_etag(obj) != request.META.get('HTTP_IF_NONE_MATCH'): + if self.get_etag(obj) != request.META.get(self.get_request_header()): raise PreconditionFailed def resource_unchanged(self, request, key): etag = cache.get(key) - header = request.META.get('HTTP_IF_NONE_MATCH') + header = request.META.get(self.get_request_header()) if etag is not None and etag == header: return True return False diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index e5de46abb..29d0f03dc 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -105,7 +105,7 @@ class RetrieveModelMixin(object): filtered_queryset = self.filter_queryset(queryset) self.object = self.get_object(filtered_queryset) - headers = self.get_cache_lookup_headers(self.object) + headers = self.get_cache_lookup_response_headers(self.object) serializer = self.get_serializer(self.object) return Response(serializer.data, headers=headers) diff --git a/rest_framework/views.py b/rest_framework/views.py index 4caec27c0..837d20912 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -307,10 +307,10 @@ class APIView(View): if cache_lookup.resource_unchanged(request, cache_key): return Response(status=304) - def get_cache_lookup_headers(self, obj): + def get_cache_lookup_response_headers(self, obj): headers = {} for cache_lookup in self.get_cache_lookups(): - headers.update(cache_lookup.get_header(obj)) + headers.update(cache_lookup.get_response_header(obj)) return headers def check_update_validity(self, request):