Files
postgres-web/pgweb/util/templateloader.py
Magnus Hagander dcd98cf6fe Add support for automatic template based xkey
This adds an xkey header to all outgoing requests with the hash of the
name of all templates loaded. In the future we will be able to use this
to purge "all pages that included a specific template", regardless of
where in the hierarchy it was loaded.

Do this by faking a template loader that never finds anything -- but it
will record the path of the template that it tried to load, and then
leave it to another template loader to actually load it. Store this in
thread local storage (it's a bit ugly, but it's the only thing Django
supports for storing things at the request level from a template
loader), and fetch it from the middleware.
2018-06-29 18:01:35 +02:00

25 lines
752 B
Python

from django.template import Origin, TemplateDoesNotExist
import django.template.loaders.base
# Store in TLS, since a template loader can't access the request
try:
from threading import local, currentThread
except ImportError:
from django.utils._threading_local import local
_thread_locals = local()
def initialize_template_collection():
_thread_locals.templates = []
def get_all_templates():
return getattr(_thread_locals, 'templates', [])
class TrackingTemplateLoader(django.template.loaders.base.Loader):
def get_template_sources(self, template_name):
_thread_locals.templates = getattr(_thread_locals, 'templates', []) + [template_name, ]
yield Origin(None)
def get_contents(self, origin):
raise TemplateDoesNotExist(origin)