Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refs #7742 -- Changed the `django.contrib.admin.views.template.templa…

…te_validator` view to use newforms instead of oldforms.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8294 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dd842ad34d1199093b7498514cd274279eb1e1ca 1 parent 0b01d50
Gary Wilson Jr. authored August 11, 2008
16  django/contrib/admin/templates/admin/template_validator.html
@@ -6,18 +6,18 @@
6 6
 
7 7
 <form action="" method="post">
8 8
 
9  
-{% if form.error_dict %}
10  
-<p class="errornote">Your template had {{ form.error_dict.items|length }} error{{ form.error_dict.items|pluralize }}:</p>
  9
+{% if form.errors %}
  10
+<p class="errornote">Your template had {{ form.errors|length }} error{{ form.errors|pluralize }}:</p>
11 11
 {% endif %}
12 12
 
13 13
 <fieldset class="module aligned">
14  
-<div class="form-row{% if form.site.errors %} error{% endif %} required">
15  
-    {% if form.site.errors %}{{ form.site.html_error_list }}{% endif %}
16  
-    <h4><label for="id_site">Site:</label> {{ form.site }}</h4>
  14
+<div class="form-row{% if form.errors.site %} error{% endif %} required">
  15
+    {% if form.errors.site %}{{ form.errors.site }}{% endif %}
  16
+    <h4><label for="id_site">{{ form.site.label }}:</label> {{ form.site }}</h4>
17 17
 </div>
18  
-<div class="form-row{% if form.template.errors %} error{% endif %} required">
19  
-    {% if form.template.errors %}{{ form.template.html_error_list }}{% endif %}
20  
-    <h4><label for="id_template">Template:</label> {{ form.template }}</h4>
  18
+<div class="form-row{% if form.errors.template %} error{% endif %} required">
  19
+    {% if form.errors.template %}{{ form.errors.template }}{% endif %}
  20
+    <h4><label for="id_template">{{ form.template.label }}:</label> {{ form.template }}</h4>
21 21
 </div>
22 22
 </fieldset>
23 23
 
55  django/contrib/admin/views/template.py
... ...
@@ -1,10 +1,11 @@
  1
+from django import template, forms
1 2
 from django.contrib.admin.views.decorators import staff_member_required
2  
-from django.core import validators
3  
-from django import template, oldforms
4 3
 from django.template import loader
5 4
 from django.shortcuts import render_to_response
6 5
 from django.contrib.sites.models import Site
7 6
 from django.conf import settings
  7
+from django.utils.translation import ugettext_lazy as _
  8
+
8 9
 
9 10
 def template_validator(request):
10 11
     """
@@ -16,41 +17,44 @@ def template_validator(request):
16 17
     for mod in settings.ADMIN_FOR:
17 18
         settings_module = __import__(mod, {}, {}, [''])
18 19
         settings_modules[settings_module.SITE_ID] = settings_module
19  
-    manipulator = TemplateValidator(settings_modules)
20  
-    new_data, errors = {}, {}
  20
+    site_list = Site.objects.in_bulk(settings_modules.keys()).values()
21 21
     if request.POST:
22  
-        new_data = request.POST.copy()
23  
-        errors = manipulator.get_validation_errors(new_data)
24  
-        if not errors:
  22
+        form = TemplateValidatorForm(settings_modules, site_list,
  23
+                                     data=request.POST)
  24
+        if form.is_valid():
25 25
             request.user.message_set.create(message='The template is valid.')
  26
+    else:
  27
+        form = TemplateValidatorForm(settings_modules, site_list)
26 28
     return render_to_response('admin/template_validator.html', {
27 29
         'title': 'Template validator',
28  
-        'form': oldforms.FormWrapper(manipulator, new_data, errors),
  30
+        'form': form,
29 31
     }, context_instance=template.RequestContext(request))
30 32
 template_validator = staff_member_required(template_validator)
31 33
 
32  
-class TemplateValidator(oldforms.Manipulator):
33  
-    def __init__(self, settings_modules):
  34
+
  35
+class TemplateValidatorForm(forms.Form):
  36
+    site = forms.ChoiceField(_('site'))
  37
+    template = forms.CharField(
  38
+        _('template'), widget=forms.Textarea({'rows': 25, 'cols': 80}))
  39
+
  40
+    def __init__(self, settings_modules, site_list, *args, **kwargs):
34 41
         self.settings_modules = settings_modules
35  
-        site_list = Site.objects.in_bulk(settings_modules.keys()).values()
36  
-        self.fields = (
37  
-            oldforms.SelectField('site', is_required=True, choices=[(s.id, s.name) for s in site_list]),
38  
-            oldforms.LargeTextField('template', is_required=True, rows=25, validator_list=[self.isValidTemplate]),
39  
-        )
  42
+        super(TemplateValidatorForm, self).__init__(*args, **kwargs)
  43
+        self.fields['site'].choices = [(s.id, s.name) for s in site_list]
40 44
 
41  
-    def isValidTemplate(self, field_data, all_data):
42  
-        # get the settings module
43  
-        # if the site isn't set, we don't raise an error since the site field will
  45
+    def clean_template(self):
  46
+        # Get the settings module. If the site isn't set, we don't raise an
  47
+        # error since the site field will.
44 48
         try:
45  
-            site_id = int(all_data.get('site', None))
  49
+            site_id = int(self.cleaned_data.get('site', None))
46 50
         except (ValueError, TypeError):
47 51
             return
48 52
         settings_module = self.settings_modules.get(site_id, None)
49 53
         if settings_module is None:
50 54
             return
51 55
 
52  
-        # so that inheritance works in the site's context, register a new function
53  
-        # for "extends" that uses the site's TEMPLATE_DIRS instead.
  56
+        # So that inheritance works in the site's context, register a new
  57
+        # function for "extends" that uses the site's TEMPLATE_DIRS instead.
54 58
         def new_do_extends(parser, token):
55 59
             node = loader.do_extends(parser, token)
56 60
             node.template_dirs = settings_module.TEMPLATE_DIRS
@@ -59,14 +63,15 @@ def new_do_extends(parser, token):
59 63
         register.tag('extends', new_do_extends)
60 64
         template.builtins.append(register)
61 65
 
62  
-        # Now validate the template using the new template dirs
63  
-        # making sure to reset the extends function in any case.
  66
+        # Now validate the template using the new TEMPLATE_DIRS, making sure to
  67
+        # reset the extends function in any case.
64 68
         error = None
  69
+        template_string = self.cleaned_data['template']
65 70
         try:
66  
-            tmpl = loader.get_template_from_string(field_data)
  71
+            tmpl = loader.get_template_from_string(template_string)
67 72
             tmpl.render(template.Context({}))
68 73
         except template.TemplateSyntaxError, e:
69 74
             error = e
70 75
         template.builtins.remove(register)
71 76
         if error:
72  
-            raise validators.ValidationError, e.args
  77
+            raise forms.ValidationError, e.args

0 notes on commit dd842ad

Please sign in to comment.
Something went wrong with that request. Please try again.