Created
June 2, 2009 19:31
-
-
Save johnboxall/122517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This Middleware inserts a link to the file which rendered the view for you. | |
Subclass and add the protocol of your favorite text editor to open the files | |
right from your web browser! | |
Now on DjangoSnippets: | |
http://www.djangosnippets.org/snippets/1546/ | |
Inspiration DjangoSnippets: | |
http://www.djangosnippets.org/snippets/766/ | |
http://www.djangosnippets.org/snippets/1033/ | |
""" | |
import os.path | |
from django.conf import settings | |
from django.template import Template, Context | |
from django.test.signals import template_rendered | |
from django.test.utils import instrumented_test_render | |
from django.utils.encoding import force_unicode | |
TEMPLATE = """ | |
<div id="debug" style="background:#85BE40 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAASCAIAAAAVNSPrAAAAOElEQVQIHWMwSnZi2PGkjunWi5NM/xkYmBj+/2f6/x9IA9n/gGwGIPs/SOwfkGaA0iA+TP4fWD0AipEie4hB/+0AAAAASUVORK5CYII%3D) repeat-x;position:absolute;top:3px;left:3px;padding:5px;-moz-border-radius:5px;text-align:left;"> | |
<ul> | |
<li> | |
<a href="{{ view_link }}">{{ view_name }}</a> | |
<a style="position:absolute;right:7px;" onclick="document.getElementById('debug').style.display='none';">x</a><br /> | |
</li> | |
{% for t in templates %} | |
<li {% if forloop.first %}style="border-top:1px solid white;margin-top:3px;padding-top:3px;"{% endif %}> | |
<a href="{{ t.0 }}">{{ t.1 }}</a> | |
</li> | |
{% endfor %} | |
</ul> | |
<div> | |
</body> | |
""" | |
# Monkeypatch instrumented test renderer from django.test.utils - we could use | |
# django.test.utils.setup_test_environment for this but that would also set up | |
# e-mail interception, which we don't want | |
if Template.render != instrumented_test_render: | |
Template.original_render = Template.render | |
Template.render = instrumented_test_render | |
# MONSTER monkey-patch | |
old_template_init = Template.__init__ | |
def new_template_init(self, template_string, origin=None, name='<Unknown Template>'): | |
old_template_init(self, template_string, origin, name) | |
self.origin = origin | |
Template.__init__ = new_template_init | |
class EditingMiddleware(object): | |
link_format = "edit://%s?%s" | |
def process_request(self, request): | |
self.templates = [] | |
template_rendered.connect(self.store_template) | |
def process_view(self, request, view_func, view_args, view_kwargs): | |
self.view_func = view_func | |
self.view_args = view_args | |
self.view_kwargs = view_kwargs | |
def process_response(self, request, response): | |
if not self.should_process_response(request, response): | |
return response | |
view_func = getattr(self, "view_func", None) | |
if view_func is None: | |
return response | |
debug_content = self.get_template().render(self.get_context()) | |
content = response.content | |
response.content = force_unicode(content).replace('</body>', debug_content) | |
return response | |
def get_template(self): | |
return Template(TEMPLATE) | |
def should_process_response(self, request, response): | |
return 'text/html' in response['Content-Type'] | |
def store_template(self, **kwargs): | |
template = kwargs.get("template") | |
if template is not None: | |
self.templates.append(template) | |
def get_context(self): | |
templates = [ | |
(self.build_link(os.path.join(settings.TEMPLATE_DIRS[0], t.name)), t.name) | |
for t in self.templates | |
] | |
view_link = self.build_link(self.view_func.func_code.co_filename, | |
self.view_func.func_code.co_firstlineno) | |
view_name = "%s.%s" % (self.view_func.__module__, self.view_func.func_name) | |
return Context({ | |
"view_link": view_link, | |
"view_name": view_name, | |
"templates": templates}) | |
def build_link(self, filename, lineno=0): | |
return self.link_format % (filename, lineno) | |
class TextMateEditingMiddleware(EditingMiddleware): | |
link_format = "txmt://open/?url=file://%s&line=%s" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment