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.core import validators
|
2006-12-16 02:00:50 +08:00
|
|
|
from django import template, oldforms
|
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
|
|
|
|
|
|
|
|
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:
|
2006-10-31 04:50:27 +08:00
|
|
|
settings_module = __import__(mod, {}, {}, [''])
|
2005-07-13 09:25:57 +08:00
|
|
|
settings_modules[settings_module.SITE_ID] = settings_module
|
|
|
|
manipulator = TemplateValidator(settings_modules)
|
|
|
|
new_data, errors = {}, {}
|
|
|
|
if request.POST:
|
|
|
|
new_data = request.POST.copy()
|
|
|
|
errors = manipulator.get_validation_errors(new_data)
|
|
|
|
if not errors:
|
2006-07-21 04:16:03 +08:00
|
|
|
request.user.message_set.create(message='The template is valid.')
|
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',
|
2006-12-16 02:00:50 +08:00
|
|
|
'form': oldforms.FormWrapper(manipulator, new_data, errors),
|
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
|
|
|
|
2006-12-16 02:00:50 +08:00
|
|
|
class TemplateValidator(oldforms.Manipulator):
|
2005-07-13 09:25:57 +08:00
|
|
|
def __init__(self, settings_modules):
|
|
|
|
self.settings_modules = settings_modules
|
2006-07-21 04:16:03 +08:00
|
|
|
site_list = Site.objects.in_bulk(settings_modules.keys()).values()
|
2005-07-13 09:25:57 +08:00
|
|
|
self.fields = (
|
2006-12-16 02:00:50 +08:00
|
|
|
oldforms.SelectField('site', is_required=True, choices=[(s.id, s.name) for s in site_list]),
|
|
|
|
oldforms.LargeTextField('template', is_required=True, rows=25, validator_list=[self.isValidTemplate]),
|
2005-07-13 09:25:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def isValidTemplate(self, field_data, all_data):
|
|
|
|
# get the settings module
|
|
|
|
# if the site isn't set, we don't raise an error since the site field will
|
|
|
|
try:
|
|
|
|
site_id = int(all_data.get('site', None))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return
|
|
|
|
settings_module = self.settings_modules.get(site_id, None)
|
|
|
|
if settings_module is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# so that inheritance works in the site's context, register a new function
|
2005-11-27 06:46:31 +08:00
|
|
|
# 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
|
|
|
|
2005-11-27 06:46:31 +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
|
|
|
|
try:
|
2005-10-15 04:10:13 +08:00
|
|
|
tmpl = loader.get_template_from_string(field_data)
|
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:
|
|
|
|
raise validators.ValidationError, e.args
|