From cb5510f06f37e22d0c990d5bbe188b3b266fb5d0 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 /var/lib/publik/theme-counter (so it's possible to "force" a reload by touching that file). --- hobo/__init__.py | 3 +++ hobo/context_processors.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) 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..34e9371 100644 --- a/hobo/context_processors.py +++ b/hobo/context_processors.py @@ -3,18 +3,30 @@ 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: + statics_hash.update(str(os.stat('/var/lib/publik/theme-counter').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