1 from django
import template
, forms
2 from django
.contrib
.admin
.views
.decorators
import staff_member_required
3 from django
.template
import loader
4 from django
.shortcuts
import render_to_response
5 from django
.contrib
.sites
.models
import Site
6 from django
.conf
import settings
7 from django
.utils
.importlib
import import_module
8 from django
.utils
.translation
import ugettext_lazy
as _
9 from django
.contrib
import messages
12 def template_validator(request
):
14 Displays the template validator form, which finds and displays template
17 # get a dict of {site_id : settings_module} for the validator
19 for mod
in settings
.ADMIN_FOR
:
20 settings_module
= import_module(mod
)
21 settings_modules
[settings_module
.SITE_ID
] = settings_module
22 site_list
= Site
.objects
.in_bulk(settings_modules
.keys()).values()
24 form
= TemplateValidatorForm(settings_modules
, site_list
,
27 messages
.info(request
, 'The template is valid.')
29 form
= TemplateValidatorForm(settings_modules
, site_list
)
30 return render_to_response('admin/template_validator.html', {
31 'title': 'Template validator',
33 }, context_instance
=template
.RequestContext(request
))
34 template_validator
= staff_member_required(template_validator
)
37 class TemplateValidatorForm(forms
.Form
):
38 site
= forms
.ChoiceField(_('site'))
39 template
= forms
.CharField(
40 _('template'), widget
=forms
.Textarea({'rows': 25, 'cols': 80}))
42 def __init__(self
, settings_modules
, site_list
, *args
, **kwargs
):
43 self
.settings_modules
= settings_modules
44 super(TemplateValidatorForm
, self
).__init
__(*args
, **kwargs
)
45 self
.fields
['site'].choices
= [(s
.id, s
.name
) for s
in site_list
]
47 def clean_template(self
):
48 # Get the settings module. If the site isn't set, we don't raise an
49 # error since the site field will.
51 site_id
= int(self
.cleaned_data
.get('site', None))
52 except (ValueError, TypeError):
54 settings_module
= self
.settings_modules
.get(site_id
, None)
55 if settings_module
is None:
58 # So that inheritance works in the site's context, register a new
59 # function for "extends" that uses the site's TEMPLATE_DIRS instead.
60 def new_do_extends(parser
, token
):
61 node
= loader
.do_extends(parser
, token
)
62 node
.template_dirs
= settings_module
.TEMPLATE_DIRS
64 register
= template
.Library()
65 register
.tag('extends', new_do_extends
)
66 template
.builtins
.append(register
)
68 # Now validate the template using the new TEMPLATE_DIRS, making sure to
69 # reset the extends function in any case.
71 template_string
= self
.cleaned_data
['template']
73 tmpl
= loader
.get_template_from_string(template_string
)
74 tmpl
.render(template
.Context({}))
75 except template
.TemplateSyntaxError
, e
:
77 template
.builtins
.remove(register
)
79 raise forms
.ValidationError(e
.args
)