Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created November 1, 2010 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchaplinsky/658116 to your computer and use it in GitHub Desktop.
Save dchaplinsky/658116 to your computer and use it in GitHub Desktop.
Patch for jinja2 loader to enable debug_toolbar support (original implementation made by Dmitri Kharlamov)
diff -r 315812aa3f61 apps/utils/jinja.py
--- a/apps/utils/jinja.py Thu Oct 28 19:38:28 2010 +0300
+++ b/apps/utils/jinja.py Mon Nov 01 14:51:36 2010 +0200
@@ -1,6 +1,6 @@
from coffin.template import Template as JTemplate
from django.template import TemplateDoesNotExist
-
+from django.conf import settings
try:
from django.template.loaders import app_directories, filesystem
@@ -12,7 +12,7 @@
passed template seems to be jinja one and raising exception otherwise"""
if 'jinja_template' in origin:
# TODO: integration with debug_toolbar
- return JTemplate(source)
+ return JTemplate(source, origin)
else:
# This will cause django to try next loader in list
raise TemplateDoesNotExist('Not a jinja template')
@@ -35,5 +35,36 @@
source, origin = self.load_template_source(template_name, template_dirs)
return _render_or_die(source, origin), origin
+
+ if settings.DEBUG:
+ from django.test.signals import template_rendered
+
+ class NewOrigin(object):
+ def __init__(self, origin):
+ self.name = origin
+
+ def render_new(self, context):
+ """
+ An instrumented Template render method, providing a signal
+ that can be intercepted by the test system Client
+ """
+ template_rendered.send(sender=self, template=self, context=context)
+ return self.original_render(context)
+
+ if JTemplate.render != render_new:
+ JTemplate.original_render = JTemplate.render
+ JTemplate.render = render_new
+
+ @staticmethod
+ def new_template_new(cls, template_string, origin=None, name='<Unknown Template>'):
+ obj = cls.old_template_new(cls, template_string, origin, name)
+ obj.origin = NewOrigin(origin)
+ obj.name = origin
+ return obj
+
+ if JTemplate.__new__ != new_template_new:
+ JTemplate.old_template_new = JTemplate.__dict__['__new__']
+ JTemplate.__new__ = new_template_new
+
except ImportError:
pass
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment