Skip to content

Instantly share code, notes, and snippets.

@andrewebdev
Created May 31, 2013 14:32
Show Gist options
  • Save andrewebdev/5685377 to your computer and use it in GitHub Desktop.
Save andrewebdev/5685377 to your computer and use it in GitHub Desktop.
Django: ETAG middleware based on project version
from django.conf import settings
PROJECT_VERSION = getattr(settings, 'PROJECT_VERSION')
class ETAGMiddleware(object):
def process_response(self, request, response):
"""
Calculate the ETag, based on site version.
This is amost the same as in django's own commonmiddleware,
but we dont want the extra processing when it comes to the
content hashing. So we just use the version number for the
etag.
"""
if response.has_header('ETag'):
etag = response['ETag']
elif response.streaming:
etag = None
else:
etag = PROJECT_VERSION
if etag is not None:
if (200 <= response.status_code < 300
and request.META.get('HTTP_IF_NONE_MATCH') == etag):
cookies = response.cookies
response = http.HttpResponseNotModified()
response.cookies = cookies
else:
response['ETag'] = etag
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment