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

            # Pre-Django1.9
            try:
                from django.template.debug import DebugLexer, DebugParser
            # Django1.9
            except ImportError:
                from django.template import Template
                template = Template(template_string=content)
                template.compile_nodelist()
            # Pre-Django1.9
            else:
                origin = StringOrigin(content)
                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
Beispiel #2
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
Beispiel #3
0
    def clean_content(self):
        content = self.cleaned_data.get('content', '')
        if content:

            # Pre-Django1.9
            try:
                from django.template.debug import DebugLexer, DebugParser
            # Django1.9
            except ImportError:
                from django.template import Template
                template = Template(template_string=content)
                template.compile_nodelist()
            # Pre-Django1.9
            else:
                origin = StringOrigin(content)
                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
Beispiel #4
0
 def __init__(self, tag: str, template: Template):
     self.tag = tag
     self.data = None
     self.load(template.source)
     self.name = template.name
     self.deps = [
         r.token.contents.split(' ')[-1]
         for r in template.compile_nodelist() if isinstance(r, LoadNode)
     ]
Beispiel #5
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