Example #1
0
    def clean_content(self):
        content = self.cleaned_data.get('content', '')
        if content:
            origin = Origin(content)

            # Try to create a Template
            try:
                template = Template(template_string=force_text(content),
                                    origin=origin)
            except Exception as e:
                # This is an error with creating the template
                self.exc_info = {
                    'message': e.args,
                    'line': e.token.lineno,
                    'name': origin.name,
                }
                raise forms.ValidationError('Invalid Django Template')

            # Template has been created; try to parse
            try:
                template.compile_nodelist()
            except Exception as e:
                # This is an error with parsing
                # The data we pass to the views is in e.template_debug
                e.template_debug = template.get_exception_info(e, e.token)
                self.exc_info = e.template_debug
                raise forms.ValidationError('Parsing Error')
        return content
Example #2
0
 def clean(self):
     template = DjangoTemplate('')
     template.source = self.changed_content
     try:
         engine = Engine.get_default()
         lexer = DebugLexer(self.changed_content)
         tokens = lexer.tokenize()
         parser = Parser(tokens, engine.template_libraries,
                         engine.template_builtins, Origin(UNKNOWN_SOURCE))
         parser.parse()
     except TemplateSyntaxError as e:
         exception_info = template.get_exception_info(e, e.token)
         raise ValidationError({
             'changed_content':
             mark_safe(
                 '{message}<br/>Line {line}:\n<pre>{before}<b>{during}</b>{after}</pre>'
                 .format(**exception_info))
         }) from e
Example #3
0
    def clean_content(self):
        content = self.cleaned_data.get('content', '')
        if content:
            origin = StringOrigin(content)

            try:
                from django.template.debug import DebugLexer, DebugParser
            except ImportError:
                # django.template.debug doesn't exist in Django >= 1.9, so use
                # Template from django.template instead
                from django.template import Template
                # Try to create a Template
                try:
                    template = Template(template_string=origin)
                # This is an error with creating the template
                except Exception as e:
                    self.exc_info = {
                        'message': e.args,
                        'line': e.token.lineno,
                        'name': origin.name,
                    }
                    raise forms.ValidationError('Invalid Django Template')
                # Template has been created; try to parse
                try:
                    template.compile_nodelist()
                # This is an error with parsing
                except Exception as e:
                    # The data we pass to the views is in e.template_debug
                    e.template_debug = template.get_exception_info(e, e.token)
                    self.exc_info = e.template_debug
                    raise forms.ValidationError('Parsing Error')
            else:
                lexer = DebugLexer(content, origin)
                try:
                    parser = DebugParser(lexer.tokenize())
                    parser.parse()
                except Exception as e:
                    self.exc_info = sys.exc_info()
                    if not hasattr(self.exc_info[1], 'django_template_source'):
                        self.exc_info[1].django_template_source = origin, (0, 0)
                    raise forms.ValidationError('Invalid Django Template')
        return content