From cede7d86a3db9cf45c29c2f5a8a4d1f8e22e7bbd Mon Sep 17 00:00:00 2001 From: George Hickman Date: Tue, 2 Apr 2013 10:45:47 +0100 Subject: [PATCH] Refactor precondition check into the cache lookup backends --- rest_framework/cache_lookups.py | 5 +++++ rest_framework/views.py | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/rest_framework/cache_lookups.py b/rest_framework/cache_lookups.py index 628e3c748..2d9271b0c 100644 --- a/rest_framework/cache_lookups.py +++ b/rest_framework/cache_lookups.py @@ -2,6 +2,7 @@ Provides a set of pluggable cache policies. """ from django.core.cache import cache +from rest_framework.exceptions import PreconditionFailed class BaseCacheLookup(object): @@ -37,6 +38,10 @@ class ETagCacheLookup(BaseCacheLookup): 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'): + raise PreconditionFailed + def resource_unchanged(self, request, key): etag = cache.get(key) header = request.META.get('HTTP_IF_NONE_MATCH') diff --git a/rest_framework/views.py b/rest_framework/views.py index 8e38d7e6a..0c780be74 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -323,10 +323,8 @@ class APIView(View): raise exceptions.IfMatchMissing def cache_precondition_check(self, obj, request): - header = request.META.get('HTTP_IF_MATCH') for cache_lookup in self.get_cache_lookups(): - if cache_lookup.get_etag(obj) != header: - raise exceptions.PreconditionFailed + cache_lookup.precondition_check(obj, request) # Dispatch methods