Commits

Anonymous committed 52bd6ac

+ added template existence validation in PageForm
+ added PAGECONTENTS_TEMPLATE_CHOICES in settings.
If defined, will be used as choices for template field in PageForm

  • Participants
  • Parent commits 84e5b46

Comments (0)

Files changed (4)

File pagecontents/forms.py

 from django.forms.fields import CharField, MultiValueField
 from django.forms.widgets import TextInput, MultiWidget, Textarea, Select
 from django.forms.models import inlineformset_factory
+from django.template.loader import find_template, TemplateDoesNotExist
 from django.utils.translation import ugettext as _
 from django.conf import settings
 
         else:
             position = 'root', None
         self.fields['position'] = PagePositionField(initial=position)
+        # templates choices list
+        template_choices = getattr(settings, 'PAGECONTENTS_TEMPLATE_CHOICES', [])
+        if template_choices:
+            self.fields['template'] = forms.ChoiceField(choices=template_choices)
+        
+    def clean_template(self):
+        template = self.cleaned_data.get('template')
+        try:
+            find_template(template)
+        except TemplateDoesNotExist:
+            raise forms.ValidationError(_(u"Template does not exist"))
+        return template
     
     def has_template(self):
         if self.instance.template:
     def delete(self):
         if not self.is_valid():
             return False
-        print "about to delete %s" % self.instance
+        # print "about to delete %s" % self.instance
         return self.instance.delete()
-# end
+
+
+# end

File pagecontents/lib/nested_tree.py

         count = self.tree().filter(
             tree_left__gte=self.tree_left,
             tree_right__lte=self.tree_right).count()
-        print "about to delete %s <= x <= %s" % (self.tree_left, self.tree_right)
+        # print "about to delete %s <= x <= %s" % (self.tree_left, self.tree_right)
         self.tree().delete_between(self.tree_left, self.tree_right)
         return count
     

File pagecontents/tests/__init__.py

 # @author J.Ducastel <nospam0@ducastel.name>
 
 # imports
-from .tests_tree import *
-from .tests_api import *
-from .tests_forms import *
-from .tests_models import *
-from .tests_templates import *
+from tests_tree import *
+from tests_api import *
+from tests_forms import *
+from tests_models import *
+from tests_templates import *
 
 
 # exceptions

File pagecontents/tests/tests_forms.py

         #    for key, value in contents.items():
         #        expected = "%s_%s" % (key, lang)
         #        self.assertEqual(value, expected)
+    
+    def test_clean_template(self):
+        form = PageForm(self._post(template='invalid.html'))
+        self.assertFalse(form.is_valid(), 'should not validate with invalid template path')
+        form = PageForm(self._post(template='tests/pc_hello.html'))
+        self.assertTrue(form.is_valid())
+        #form = PageForm(self._post(template=''))
+        #self.assertTrue(form.is_valid())
 
     def assertSavedPosition(self, page, left, right, depth):
         self.assertNotEqual(page.id, None)
         page = f.save()
         return page
     
-    def _post(self, slug='slug', status='online', position=('root',None), title='Title'):
+    def _post(self, slug='slug', status='online', position=('root',None), title='Title', template='tests/pc_pageform.html'):
         """creates a post dict"""
         POST = {
             'slug': slug,
             'status': status,
-            'template': 'tests/pc_pageform.html',
+            'template': template,
             'position_0': position[0], 'position_1': position[1],
         }
         n = 0