2008-08-11 12:31:55 +08:00
|
|
|
from django import template, forms
|
2005-10-18 13:12:45 +08:00
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.template import loader
|
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.contrib.sites.models import Site
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.conf import settings
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2008-08-11 12:31:55 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def template_validator(request):
|
|
|
|
"""
|
|
|
|
Displays the template validator form, which finds and displays template
|
|
|
|
syntax errors.
|
|
|
|
"""
|
|
|
|
# get a dict of {site_id : settings_module} for the validator
|
|
|
|
settings_modules = {}
|
|
|
|
for mod in settings.ADMIN_FOR:
|
2009-03-19 00:55:59 +08:00
|
|
|
settings_module = import_module(mod)
|
2005-07-13 09:25:57 +08:00
|
|
|
settings_modules[settings_module.SITE_ID] = settings_module
|
2008-08-11 12:31:55 +08:00
|
|
|
site_list = Site.objects.in_bulk(settings_modules.keys()).values()
|
2005-07-13 09:25:57 +08:00
|
|
|
if request.POST:
|
2008-08-11 12:31:55 +08:00
|
|
|
form = TemplateValidatorForm(settings_modules, site_list,
|
|
|
|
data=request.POST)
|
|
|
|
if form.is_valid():
|
2006-07-21 04:16:03 +08:00
|
|
|
request.user.message_set.create(message='The template is valid.')
|
2008-08-11 12:31:55 +08:00
|
|
|
else:
|
|
|
|
form = TemplateValidatorForm(settings_modules, site_list)
|
2006-05-02 09:31:56 +08:00
|
|
|
return render_to_response('admin/template_validator.html', {
|
2005-07-13 09:25:57 +08:00
|
|
|
'title': 'Template validator',
|
2008-08-11 12:31:55 +08:00
|
|
|
'form': form,
|
2006-05-02 09:31:56 +08:00
|
|
|
}, context_instance=template.RequestContext(request))
|
2005-10-18 13:12:45 +08:00
|
|
|
template_validator = staff_member_required(template_validator)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-08-11 12:31:55 +08:00
|
|
|
|
|
|
|
class TemplateValidatorForm(forms.Form):
|
|
|
|
site = forms.ChoiceField(_('site'))
|
|
|
|
template = forms.CharField(
|
|
|
|
_('template'), widget=forms.Textarea({'rows': 25, 'cols': 80}))
|
|
|
|
|
|
|
|
def __init__(self, settings_modules, site_list, *args, **kwargs):
|
2005-07-13 09:25:57 +08:00
|
|
|
self.settings_modules = settings_modules
|
2008-08-11 12:31:55 +08:00
|
|
|
super(TemplateValidatorForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields['site'].choices = [(s.id, s.name) for s in site_list]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-08-11 12:31:55 +08:00
|
|
|
def clean_template(self):
|
|
|
|
# Get the settings module. If the site isn't set, we don't raise an
|
|
|
|
# error since the site field will.
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2008-08-11 12:31:55 +08:00
|
|
|
site_id = int(self.cleaned_data.get('site', None))
|
2005-07-13 09:25:57 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return
|
|
|
|
settings_module = self.settings_modules.get(site_id, None)
|
|
|
|
if settings_module is None:
|
|
|
|
return
|
|
|
|
|
2008-08-11 12:31:55 +08:00
|
|
|
# So that inheritance works in the site's context, register a new
|
|
|
|
# function for "extends" that uses the site's TEMPLATE_DIRS instead.
|
2005-07-13 09:25:57 +08:00
|
|
|
def new_do_extends(parser, token):
|
2005-10-15 04:10:13 +08:00
|
|
|
node = loader.do_extends(parser, token)
|
2005-07-13 09:25:57 +08:00
|
|
|
node.template_dirs = settings_module.TEMPLATE_DIRS
|
|
|
|
return node
|
2005-11-27 06:46:31 +08:00
|
|
|
register = template.Library()
|
|
|
|
register.tag('extends', new_do_extends)
|
|
|
|
template.builtins.append(register)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-08-11 12:31:55 +08:00
|
|
|
# Now validate the template using the new TEMPLATE_DIRS, making sure to
|
|
|
|
# reset the extends function in any case.
|
2005-07-13 09:25:57 +08:00
|
|
|
error = None
|
2008-08-11 12:31:55 +08:00
|
|
|
template_string = self.cleaned_data['template']
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2008-08-11 12:31:55 +08:00
|
|
|
tmpl = loader.get_template_from_string(template_string)
|
2005-07-13 09:25:57 +08:00
|
|
|
tmpl.render(template.Context({}))
|
|
|
|
except template.TemplateSyntaxError, e:
|
|
|
|
error = e
|
2005-11-27 06:46:31 +08:00
|
|
|
template.builtins.remove(register)
|
2005-07-13 09:25:57 +08:00
|
|
|
if error:
|
2008-08-11 12:31:55 +08:00
|
|
|
raise forms.ValidationError, e.args
|