Refs #7742 -- Changed the `django.contrib.admin.views.template.template_validator` view to use newforms instead of oldforms.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8294 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0b01d50076
commit
dd842ad34d
|
@ -6,18 +6,18 @@
|
||||||
|
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
|
|
||||||
{% if form.error_dict %}
|
{% if form.errors %}
|
||||||
<p class="errornote">Your template had {{ form.error_dict.items|length }} error{{ form.error_dict.items|pluralize }}:</p>
|
<p class="errornote">Your template had {{ form.errors|length }} error{{ form.errors|pluralize }}:</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<fieldset class="module aligned">
|
<fieldset class="module aligned">
|
||||||
<div class="form-row{% if form.site.errors %} error{% endif %} required">
|
<div class="form-row{% if form.errors.site %} error{% endif %} required">
|
||||||
{% if form.site.errors %}{{ form.site.html_error_list }}{% endif %}
|
{% if form.errors.site %}{{ form.errors.site }}{% endif %}
|
||||||
<h4><label for="id_site">Site:</label> {{ form.site }}</h4>
|
<h4><label for="id_site">{{ form.site.label }}:</label> {{ form.site }}</h4>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row{% if form.template.errors %} error{% endif %} required">
|
<div class="form-row{% if form.errors.template %} error{% endif %} required">
|
||||||
{% if form.template.errors %}{{ form.template.html_error_list }}{% endif %}
|
{% if form.errors.template %}{{ form.errors.template }}{% endif %}
|
||||||
<h4><label for="id_template">Template:</label> {{ form.template }}</h4>
|
<h4><label for="id_template">{{ form.template.label }}:</label> {{ form.template }}</h4>
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
|
from django import template, forms
|
||||||
from django.contrib.admin.views.decorators import staff_member_required
|
from django.contrib.admin.views.decorators import staff_member_required
|
||||||
from django.core import validators
|
|
||||||
from django import template, oldforms
|
|
||||||
from django.template import loader
|
from django.template import loader
|
||||||
from django.shortcuts import render_to_response
|
from django.shortcuts import render_to_response
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
def template_validator(request):
|
def template_validator(request):
|
||||||
"""
|
"""
|
||||||
|
@ -16,41 +17,44 @@ def template_validator(request):
|
||||||
for mod in settings.ADMIN_FOR:
|
for mod in settings.ADMIN_FOR:
|
||||||
settings_module = __import__(mod, {}, {}, [''])
|
settings_module = __import__(mod, {}, {}, [''])
|
||||||
settings_modules[settings_module.SITE_ID] = settings_module
|
settings_modules[settings_module.SITE_ID] = settings_module
|
||||||
manipulator = TemplateValidator(settings_modules)
|
site_list = Site.objects.in_bulk(settings_modules.keys()).values()
|
||||||
new_data, errors = {}, {}
|
|
||||||
if request.POST:
|
if request.POST:
|
||||||
new_data = request.POST.copy()
|
form = TemplateValidatorForm(settings_modules, site_list,
|
||||||
errors = manipulator.get_validation_errors(new_data)
|
data=request.POST)
|
||||||
if not errors:
|
if form.is_valid():
|
||||||
request.user.message_set.create(message='The template is valid.')
|
request.user.message_set.create(message='The template is valid.')
|
||||||
|
else:
|
||||||
|
form = TemplateValidatorForm(settings_modules, site_list)
|
||||||
return render_to_response('admin/template_validator.html', {
|
return render_to_response('admin/template_validator.html', {
|
||||||
'title': 'Template validator',
|
'title': 'Template validator',
|
||||||
'form': oldforms.FormWrapper(manipulator, new_data, errors),
|
'form': form,
|
||||||
}, context_instance=template.RequestContext(request))
|
}, context_instance=template.RequestContext(request))
|
||||||
template_validator = staff_member_required(template_validator)
|
template_validator = staff_member_required(template_validator)
|
||||||
|
|
||||||
class TemplateValidator(oldforms.Manipulator):
|
|
||||||
def __init__(self, settings_modules):
|
|
||||||
self.settings_modules = settings_modules
|
|
||||||
site_list = Site.objects.in_bulk(settings_modules.keys()).values()
|
|
||||||
self.fields = (
|
|
||||||
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]),
|
|
||||||
)
|
|
||||||
|
|
||||||
def isValidTemplate(self, field_data, all_data):
|
class TemplateValidatorForm(forms.Form):
|
||||||
# get the settings module
|
site = forms.ChoiceField(_('site'))
|
||||||
# if the site isn't set, we don't raise an error since the site field will
|
template = forms.CharField(
|
||||||
|
_('template'), widget=forms.Textarea({'rows': 25, 'cols': 80}))
|
||||||
|
|
||||||
|
def __init__(self, settings_modules, site_list, *args, **kwargs):
|
||||||
|
self.settings_modules = settings_modules
|
||||||
|
super(TemplateValidatorForm, self).__init__(*args, **kwargs)
|
||||||
|
self.fields['site'].choices = [(s.id, s.name) for s in site_list]
|
||||||
|
|
||||||
|
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.
|
||||||
try:
|
try:
|
||||||
site_id = int(all_data.get('site', None))
|
site_id = int(self.cleaned_data.get('site', None))
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
return
|
return
|
||||||
settings_module = self.settings_modules.get(site_id, None)
|
settings_module = self.settings_modules.get(site_id, None)
|
||||||
if settings_module is None:
|
if settings_module is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
# so that inheritance works in the site's context, register a new function
|
# So that inheritance works in the site's context, register a new
|
||||||
# for "extends" that uses the site's TEMPLATE_DIRS instead.
|
# function for "extends" that uses the site's TEMPLATE_DIRS instead.
|
||||||
def new_do_extends(parser, token):
|
def new_do_extends(parser, token):
|
||||||
node = loader.do_extends(parser, token)
|
node = loader.do_extends(parser, token)
|
||||||
node.template_dirs = settings_module.TEMPLATE_DIRS
|
node.template_dirs = settings_module.TEMPLATE_DIRS
|
||||||
|
@ -59,14 +63,15 @@ class TemplateValidator(oldforms.Manipulator):
|
||||||
register.tag('extends', new_do_extends)
|
register.tag('extends', new_do_extends)
|
||||||
template.builtins.append(register)
|
template.builtins.append(register)
|
||||||
|
|
||||||
# Now validate the template using the new template dirs
|
# Now validate the template using the new TEMPLATE_DIRS, making sure to
|
||||||
# making sure to reset the extends function in any case.
|
# reset the extends function in any case.
|
||||||
error = None
|
error = None
|
||||||
|
template_string = self.cleaned_data['template']
|
||||||
try:
|
try:
|
||||||
tmpl = loader.get_template_from_string(field_data)
|
tmpl = loader.get_template_from_string(template_string)
|
||||||
tmpl.render(template.Context({}))
|
tmpl.render(template.Context({}))
|
||||||
except template.TemplateSyntaxError, e:
|
except template.TemplateSyntaxError, e:
|
||||||
error = e
|
error = e
|
||||||
template.builtins.remove(register)
|
template.builtins.remove(register)
|
||||||
if error:
|
if error:
|
||||||
raise validators.ValidationError, e.args
|
raise forms.ValidationError, e.args
|
||||||
|
|
Loading…
Reference in New Issue