Ejemplo n.º 1
0
    def test_non_extending_template(self):
        template = Template('''Stuff in here.''')
        content = get_unreachable_content(template)
        self.assertFalse(content)

        template = Template('''{% block head %}Stuff in here.{% endblock %}''')
        content = get_unreachable_content(template)
        self.assertFalse(content)

        template = Template('''
            Free i am.
            {% block head %}I'm not :({% endblock %}
            ''')
        content = get_unreachable_content(template)
        self.assertEqual(content, [])
Ejemplo n.º 2
0
    def test_non_extending_template(self):
        template = Template('''Stuff in here.''')
        content = get_unreachable_content(template)
        self.assertFalse(content)

        template = Template('''{% block head %}Stuff in here.{% endblock %}''')
        content = get_unreachable_content(template)
        self.assertFalse(content)

        template = Template(
            '''
            Free i am.
            {% block head %}I'm not :({% endblock %}
            ''')
        content = get_unreachable_content(template)
        self.assertEqual(content, [])
Ejemplo n.º 3
0
    def template_has_no_dead_code(self, template_name):
        from otree.checks.templates import get_unreachable_content
        from otree.checks.templates import has_valid_encoding

        # Only test files that are valid templates.
        if not has_valid_encoding(template_name):
            return

        try:
            with open(template_name, 'r') as f:
                compiled_template = Template(f.read())
        except (IOError, OSError, TemplateSyntaxError):
            # Ignore errors that occured during file-read or compilation.
            return

        def format_content(text):
            text = text.strip()
            lines = text.splitlines()
            lines = ['> {0}'.format(line) for line in lines]
            return '\n'.join(lines)

        contents = get_unreachable_content(compiled_template)
        content_bits = '\n\n'.join(format_content(bit) for bit in contents)
        if contents:
            return self.error(
                'Template contains the following text outside of a '
                '{% block %}. This text will never be displayed.'
                '\n\n' + content_bits,
                obj=os.path.join(self.config.label,
                                 self.get_rel_path(template_name)))
Ejemplo n.º 4
0
    def test_ignore_comments(self):
        template = Template('''
            {% extends "base.html" %}
            {# comment #}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 0)

        template = Template('''
            {% extends "base.html" %}
            {% comment %}comment{% endcomment %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 0)
Ejemplo n.º 5
0
def template_valid(template_name: str, helper: AppCheckHelper):
    from otree.checks.templates import get_unreachable_content
    from otree.checks.templates import has_valid_encoding
    from otree.checks.templates import format_source_snippet

    # Only test files that are valid templates.
    if not has_valid_encoding(template_name):
        return

    try:
        with io.open(template_name, 'r', encoding='utf8') as f:
            compiled_template = Template(f.read())
    except (IOError, OSError, TemplateSyntaxError):
        # When we used Django 1.8
        # we used to show the line from the source that caused the error,
        # but django_template_source was removed at some point,
        # so it's better to let the yellow error page show the error nicely
        return

    def format_content(text):
        text = text.strip()
        lines = text.splitlines()
        lines = ['> {0}'.format(line) for line in lines]
        return '\n'.join(lines)

    contents = get_unreachable_content(compiled_template)
    content_bits = '\n\n'.join(format_content(bit) for bit in contents)
    if contents:
        helper.add_error('Template contains the following text outside of a '
                         '{% block %}. This text will never be displayed.'
                         '\n\n' + content_bits,
                         obj=os.path.join(helper.app_config.label,
                                          helper.get_rel_path(template_name)),
                         numeric_id=7)
Ejemplo n.º 6
0
    def template_has_no_dead_code(self, template_name):
        from otree.checks.templates import get_unreachable_content
        from otree.checks.templates import has_valid_encoding

        # Only test files that are valid templates.
        if not has_valid_encoding(template_name):
            return

        try:
            with open(template_name, 'r') as f:
                compiled_template = Template(f.read())
        except (IOError, OSError, TemplateSyntaxError):
            # Ignore errors that occured during file-read or compilation.
            return

        def format_content(text):
            text = text.strip()
            lines = text.splitlines()
            lines = ['> {0}'.format(line) for line in lines]
            return '\n'.join(lines)

        contents = get_unreachable_content(compiled_template)
        content_bits = '\n\n'.join(
            format_content(bit)
            for bit in contents)
        if contents:
            return self.error(
                'Template contains the following text outside of a '
                '{% block %}. This text will never be displayed.'
                '\n\n' + content_bits,
                obj=os.path.join(self.config.label,
                                 self.get_rel_path(template_name)))
Ejemplo n.º 7
0
    def test_ignore_comments(self):
        template = Template(
            '''
            {% extends "base.html" %}
            {# comment #}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 0)

        template = Template(
            '''
            {% extends "base.html" %}
            {% comment %}comment{% endcomment %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 0)
Ejemplo n.º 8
0
def template_content_is_in_blocks(template_name: str, helper: AppCheckHelper):
    from otree.checks.templates import get_unreachable_content
    from otree.checks.templates import has_valid_encoding
    from otree.checks.templates import format_source_snippet

    # Only test files that are valid templates.
    if not has_valid_encoding(template_name):
        return

    try:
        with io.open(template_name, 'r', encoding='utf8') as f:

            # when we upgraded to Django 1.11, we got an error
            # if someone used "{% include %}" with a relative
            # path (like ../Foo.html):
            # File "c:\otree\ve_dj11\lib\site-packages\django\template\loader_tags.py", line 278, in construct_relative_path
            #  posixpath.dirname(current_template_name.lstrip('/')),
            # AttributeError: 'NoneType' object has no attribute 'lstrip'
            # can fix this by passing a dummy 'Origin' param.
            # i tried also with Engin.get_default().from_string(template_name),
            # but got the same error.
            class Origin:
                name = ''
                template_name = ''

            compiled_template = Template(f.read(), origin=Origin)
    except (IOError, OSError, TemplateSyntaxError):
        # When we used Django 1.8
        # we used to show the line from the source that caused the error,
        # but django_template_source was removed at some point,
        # so it's better to let the yellow error page show the error nicely
        return

    def format_content(text):
        text = text.strip()
        lines = text.splitlines()
        lines = ['> {0}'.format(line) for line in lines]
        return '\n'.join(lines)

    contents = get_unreachable_content(compiled_template)

    content_bits = '\n\n'.join(
        format_content(bit)
        for bit in contents)
    # note: this seems to not detect unreachable content
    # if the template has a relative include,
    # like {% include "../Foo.html" %}
    # not sure why, but that's not common usage.
    if contents:
        helper.add_error(
            'Template contains the following text outside of a '
            '{% block %}. This text will never be displayed.'
            '\n\n' + content_bits,
            obj=os.path.join(helper.app_config.label,
                             helper.get_rel_path(template_name)),
            numeric_id=7)
Ejemplo n.º 9
0
    def test_text_after_block(self):
        template = Template('''
            {% extends "base.html" %}
            {% block content %}Stuff in here.{% endblock %}
            After the block.
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 1)
        self.assertTrue('After the block.' in content[0])
        self.assertTrue('Stuff in here.' not in content[0])
Ejemplo n.º 10
0
    def test_text_after_block(self):
        template = Template(
            '''
            {% extends "base.html" %}
            {% block content %}Stuff in here.{% endblock %}
            After the block.
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 1)
        self.assertTrue('After the block.' in content[0])
        self.assertTrue('Stuff in here.' not in content[0])
Ejemplo n.º 11
0
    def test_ok_extending_template(self):
        template = Template('''
            {% extends "base.html" %}

            {% block content %}
            Stuff in here.
            {% if 1 %}Un-Conditional{% endif %}
            {% endblock %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(content, [])
Ejemplo n.º 12
0
    def test_non_block_statements(self):
        # We do not dive into other statements.
        template = Template('''
            {% extends "base.html" %}

            {% if 1 %}
            Free i am.
            {% endif %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 0)
Ejemplo n.º 13
0
    def test_non_block_statements(self):
        # We do not dive into other statements.
        template = Template(
            '''
            {% extends "base.html" %}

            {% if 1 %}
            Free i am.
            {% endif %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 0)
Ejemplo n.º 14
0
    def test_ok_extending_template(self):
        template = Template(
            '''
            {% extends "base.html" %}

            {% block content %}
            Stuff in here.
            {% if 1 %}Un-Conditional{% endif %}
            {% endblock %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(content, [])
Ejemplo n.º 15
0
    def test_extending_template_with_non_wrapped_code(self):
        template = Template('''
            {% extends "base.html" %}

            Free i am.

            {% block content %}Stuff in here.{% endblock %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 1)
        self.assertTrue('Free i am.' in content[0])
        self.assertTrue('Stuff in here.' not in content[0])
Ejemplo n.º 16
0
    def test_extending_template_with_non_wrapped_code(self):
        template = Template(
            '''
            {% extends "base.html" %}

            Free i am.

            {% block content %}Stuff in here.{% endblock %}
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 1)
        self.assertTrue('Free i am.' in content[0])
        self.assertTrue('Stuff in here.' not in content[0])
Ejemplo n.º 17
0
    def test_multiple_text_nodes(self):
        template = Template('''
            {% extends "base.html" %}
            First.
            {% block content %}Stuff in here.{% endblock %}
            Second.
            {% load i18n %}
            Third.
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 3)
        self.assertTrue('First.' in content[0])
        self.assertTrue('Second.' in content[1])
        self.assertTrue('Third.' in content[2])
Ejemplo n.º 18
0
    def test_multiple_text_nodes(self):
        template = Template(
            '''
            {% extends "base.html" %}
            First.
            {% block content %}Stuff in here.{% endblock %}
            Second.
            {% load i18n %}
            Third.
            ''')

        content = get_unreachable_content(template)
        self.assertEqual(len(content), 3)
        self.assertTrue('First.' in content[0])
        self.assertTrue('Second.' in content[1])
        self.assertTrue('Third.' in content[2])