Example #1
0
    def clean(self):
        """
        Validates whether this template will work on site pages.
        In order for a template `A` to work on a page from site `A` the
                following conditions need to exist:
            * template A needs to be assigned to site A
            * content from template A needs to be a valid django template code
            * other templates used in template A (with `include`/`extend`/etc.
                template tags) need to be assigned to site `A`
            * other templates used should not create infinite recursive calls(
                template A extends template B, template B extends template A)
        This method makes sure all this conditions comply.
        """

        cleaned_data = super(ExtendedTemplateAdminForm, self).clean()
        if not set(['name', 'content', 'sites']) <= set(cleaned_data.keys()):
            return cleaned_data

        if not cleaned_data['sites']:
            cleaned_data['sites'] = Site.objects.none()

        sites_assigned_in_widget = [site.domain
                                    for site in cleaned_data['sites']]
        try:
            compiled_template = _Template(cleaned_data.get('content'))

            #at this point template content does not have any syntax errors
            handle_recursive_calls(cleaned_data['name'],
                                   cleaned_data['content'])

            used_templates = get_all_templates_used(compiled_template.nodelist)
        except TemplateSyntaxError, e:
            raise ValidationError(
                self._error_msg('syntax_error', cleaned_data['name'], e))
Example #2
0
    def test(self):
        t1 = Template.objects.create(name="tpl1", \
                            content=InfiniteRecursivityErrorTest.tpl1)
        t2 = Template.objects.create(name="tpl2", \
                            content=InfiniteRecursivityErrorTest.tpl2)
        t3 = Template.objects.create(name="tpl3", \
                            content=InfiniteRecursivityErrorTest.tpl3)
        t4 = Template.objects.create(name="tpl4", \
                            content=InfiniteRecursivityErrorTest.tpl4)

        self.assertRaises(InfiniteRecursivityError,
                          handle_recursive_calls, t1.name, t1.content)
        try:
            handle_recursive_calls(t1.name, t1.content)
        except InfiniteRecursivityError, e:
            self.assertEqual(set([u'tpl1', u'tpl2', u'tpl3', u'tpl4']), \
                                     set(e.cycle_items))
    def clean(self):
        cleaned_data = super(ExtendedTemplateAdminForm, self).clean()
        if not set(['name', 'content', 'sites']) <= set(cleaned_data.keys()):
            return cleaned_data

        required_sites = [site.domain for site in cleaned_data['sites']]

        try:
            compiled_template = _Template(cleaned_data.get('content'))

            #here the template syntax is valid
            handle_recursive_calls(cleaned_data['name'], cleaned_data['content'])

            used_templates = get_all_templates_used(compiled_template.nodelist)
        except TemplateSyntaxError, e:
            raise ValidationError(
                self._error_msg('syntax_error', cleaned_data['name'], e))