Projet

Général

Profil

0001-misc-add-a-new-statics_hash-in-context-variables-887.patch

Frédéric Péters, 18 novembre 2015 14:08

Télécharger (2,66 ko)

Voir les différences:

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(-)
debian/debian_config_common.py
194 194
# Celery configuration
195 195
BROKER_URL = 'amqp://'
196 196
BROKER_TASK_EXPIRES = 120
197

  
198
STATICS_HASH_COUNTER = '/var/lib/publik/statics-counter'
hobo/__init__.py
1
# import the context_processors module so it gets to compute the versions hash
2
# early on.
3
from . import context_processors
hobo/context_processors.py
3 3
import logging
4 4
import requests
5 5
import threading
6
import os
6 7
import urlparse
7 8

  
8 9
from django.conf import settings
9 10
from django.core.cache import cache
10 11
from django.template import Template
11 12

  
12
def template_vars(request):
13
    return getattr(settings, 'TEMPLATE_VARS', {})
13
from hobo.scrutiny.wsgi.middleware import VersionMiddleware
14 14

  
15 15
logger = logging.getLogger('hobo')
16 16

  
17 17
CACHE_REFRESH_TIMEOUT = 300
18
VERSIONS = VersionMiddleware.get_packages_version()
19

  
20

  
21
def template_vars(request):
22
    statics_hash = hashlib.md5(repr(sorted(VERSIONS.items())))
23
    try:
24
        counter_filename = getattr(settings, 'STATICS_HASH_COUNTER', None)
25
        if counter_filename:
26
            statics_hash.update(str(os.stat(counter_filename).st_atime))
27
    except OSError:
28
        pass
29
    template_vars = getattr(settings, 'TEMPLATE_VARS', {})
30
    template_vars.update({'statics_hash': statics_hash.hexdigest()})
31
    return template_vars
18 32

  
19 33
class RemoteTemplate(object):
20 34
    def __init__(self, source):
21
-