mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 17:39:48 +03:00
Refactor precondition check into the cache lookup backends
This commit is contained in:
parent
65b16071c4
commit
cede7d86a3
|
@ -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')
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user