Example #1
0
 def _get_templates_used(self, template_instance):
     try:
         compiled_template = _Template(template_instance.content)
         used_templates = get_all_templates_used(compiled_template.nodelist)
     except TemplateSyntaxError, e:
         raise ValidationError(
             self._error_msg('syntax_error', template_instance.name, e))
Example #2
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 #3
0
 def _get_used_templates(self, template_name, site_domain, pages_search):
     """
     Returns a set of templates that template `A` uses.
     Raises validation errors if the templates used are invalid.
     This method is used in 2 cases:
         1. when searching for templates of pages for a certain site A that
             is selected to be unassigned
         2. when searching for templates assigned to a certain site A, site
             that is selected to be unassigned from template `A`
         pages_search - determines which error message should be used
                      - is true if this method is called when checking
                         templates from pages of a site that is about to be
                         unassigned
     """
     try:
         template = Template.objects.get(name=template_name)
         return set(get_all_templates_used(_Template(
             template.content).nodelist))
     except Template.DoesNotExist:
         if pages_search:
             pages_to_print = Page.objects.filter(
                 site__domain=site_domain, template=template_name)
             raise ValidationError(self._error_msg(
                 'nonexistent_in_pages', template_name, site_domain,
                 _format_pages(pages_to_print)))
     except TemplateSyntaxError, e:
         raise ValidationError(self._error_msg(
             'syntax_error_unassigning', template_name, e, site_domain))
 def _get_used_templates(self, template_name, site_domain, pages_search):
     try:
         template = Template.objects.get(name=template_name)
         return set(get_all_templates_used(_Template(
             template.content).nodelist))
     except Template.DoesNotExist:
         if pages_search:
             pages_to_print = Page.objects.filter(
                 site__domain=site_domain, template=template_name)
             raise ValidationError(self._error_msg(
                 'nonexistent_in_pages', template_name, site_domain,
                 _format_pages(pages_to_print)))
     except TemplateSyntaxError, e:
         raise ValidationError(self._error_msg(
             'syntax_error_unassigning', template_name, e, site_domain))
Example #5
0
def get_templates_that_use_template(template_name, only_one_required=False):
    quoted_name = "'%s'" % template_name
    double_quoted_name = '"%s"' % template_name

    candidates = Template.objects.filter(Q(content__contains=quoted_name) |
                                         Q(content__contains=double_quoted_name))
    child_templates = []
    for child_template in candidates:
        try:
            parents = get_all_templates_used(_Template(child_template.content).nodelist)
        except TemplateDoesNotExist:
            parents = []
        if template_name in parents:
            if only_one_required:
                return [child_template]
            child_templates.append(child_template)
    return child_templates
    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))