Refactor precondition check into the cache lookup backends

This commit is contained in:
George Hickman 2013-04-02 10:45:47 +01:00
parent 65b16071c4
commit cede7d86a3
2 changed files with 6 additions and 3 deletions

View File

@ -2,6 +2,7 @@
Provides a set of pluggable cache policies. Provides a set of pluggable cache policies.
""" """
from django.core.cache import cache from django.core.cache import cache
from rest_framework.exceptions import PreconditionFailed
class BaseCacheLookup(object): class BaseCacheLookup(object):
@ -37,6 +38,10 @@ class ETagCacheLookup(BaseCacheLookup):
cache.set(key, etag) cache.set(key, etag)
return {'ETag': etag} return {'ETag': etag}
def precondition_check(self, obj, request):
if self.get_etag(obj) != request.META.get('HTTP_IF_NONE_MATCH'):
raise PreconditionFailed
def resource_unchanged(self, request, key): def resource_unchanged(self, request, key):
etag = cache.get(key) etag = cache.get(key)
header = request.META.get('HTTP_IF_NONE_MATCH') header = request.META.get('HTTP_IF_NONE_MATCH')

View File

@ -323,10 +323,8 @@ class APIView(View):
raise exceptions.IfMatchMissing raise exceptions.IfMatchMissing
def cache_precondition_check(self, obj, request): def cache_precondition_check(self, obj, request):
header = request.META.get('HTTP_IF_MATCH')
for cache_lookup in self.get_cache_lookups(): for cache_lookup in self.get_cache_lookups():
if cache_lookup.get_etag(obj) != header: cache_lookup.precondition_check(obj, request)
raise exceptions.PreconditionFailed
# Dispatch methods # Dispatch methods