From f76950896a64bd5012fe490ba546a1be3a9f4a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 18 Nov 2015 12:22:03 +0100 Subject: [PATCH] misc: add a new statics_hash in context variables (#8875) This variable is to be used to prevent browser using stale resources (javascript and stylesheets) after package upgrades. Use it like href="{% static 'foobar.css' %}?{{statics_hash}}". It is computed from installed module versions + the access time of settings.STATICS_HASH_COUNTER (so it's possible to "force" a reload by touching that file). Set settings.STATICS_HASH_COUNTER to /var/lib/publik/statics-counter in the debian packaging. --- debian/debian_config_common.py | 2 ++ hobo/__init__.py | 3 +++ hobo/context_processors.py | 18 ++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index 66074bd..3c4384a 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -194,3 +194,5 @@ USE_TZ = True # Celery configuration BROKER_URL = 'amqp://' BROKER_TASK_EXPIRES = 120 + +STATICS_HASH_COUNTER = '/var/lib/publik/statics-counter' diff --git a/hobo/__init__.py b/hobo/__init__.py index e69de29..33635e5 100644 --- a/hobo/__init__.py +++ b/hobo/__init__.py @@ -0,0 +1,3 @@ +# import the context_processors module so it gets to compute the versions hash +# early on. +from . import context_processors diff --git a/hobo/context_processors.py b/hobo/context_processors.py index 85c1a5f..31bb8ba 100644 --- a/hobo/context_processors.py +++ b/hobo/context_processors.py @@ -3,18 +3,32 @@ import hashlib import logging import requests import threading +import os import urlparse from django.conf import settings from django.core.cache import cache from django.template import Template -def template_vars(request): - return getattr(settings, 'TEMPLATE_VARS', {}) +from hobo.scrutiny.wsgi.middleware import VersionMiddleware logger = logging.getLogger('hobo') CACHE_REFRESH_TIMEOUT = 300 +VERSIONS = VersionMiddleware.get_packages_version() + + +def template_vars(request): + statics_hash = hashlib.md5(repr(sorted(VERSIONS.items()))) + try: + counter_filename = getattr(settings, 'STATICS_HASH_COUNTER', None) + if counter_filename: + statics_hash.update(str(os.stat(counter_filename).st_atime)) + except OSError: + pass + template_vars = getattr(settings, 'TEMPLATE_VARS', {}) + template_vars.update({'statics_hash': statics_hash.hexdigest()}) + return template_vars class RemoteTemplate(object): def __init__(self, source): -- 2.6.2