Ejemplo n.º 1
0
def test_email_templates(plugins, expected, monkeypatch):
    monkeypatch.setattr(JinjaEnv, '_env', None)
    monkeypatch.setattr(JinjaEnv, '_exportable_functions', {})
    monkeypatch.setattr(JinjaEnv, '_filter_functions', {})
    monkeypatch.setattr(JinjaEnv, '_test_functions', {})
    monkeypatch.setattr(JinjaEnv, '_template_dirs', [])
    monkeypatch.setattr(AutomatedEmail, '_fixtures', OrderedDict())

    for plugin in plugins:
        JinjaEnv.insert_template_dir(os.path.join(__here__, plugin, 'templates'))

    with Session() as session:
        for email in session.query(AutomatedEmail).all():
            session.delete(email)

    for i in range(1, 4):
        AutomatedEmailFixture(
            Attendee,
            'Test Template {}'.format(i),
            'test_template.txt',
            lambda a: True,
            needs_approval=False,
            ident='test_template_{}'.format(i))

    AutomatedEmail.reconcile_fixtures()

    with Session() as session:
        attendee = Attendee(first_name='Test', last_name='Email', email='*****@*****.**')
        for i in range(1, 4):
            automated_email = session.query(AutomatedEmail).filter_by(ident='test_template_{}'.format(i)).one()
            for i in range(1, 4):
                assert expected == automated_email.render_body(attendee).strip(), 'render() call {}'.format(i)
Ejemplo n.º 2
0
 def test_timedelta_filter_in_template(self):
     dt = datetime.utcnow()
     env = JinjaEnv.env()
     template = env.from_string(
         '{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = (dt + timedelta(days=-5)).strftime("%A, %B %-e")
     assert expected == template.render(dt=dt)
Ejemplo n.º 3
0
def render(template_name_list, data=None, encoding='utf-8'):
    data = renderable_data(data)
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list)
    rendered = template.render(data)
    if encoding:
        return rendered.encode(encoding)
    return rendered
Ejemplo n.º 4
0
def render(template_name_list, data=None, encoding='utf-8'):
    data = renderable_data(data)
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list)
    rendered = template.render(data)
    if encoding:
        return rendered.encode(encoding)
    return rendered
Ejemplo n.º 5
0
def test_verify_jinja_autoescape_string():
    env = JinjaEnv.env()
    template = env.from_string('''\
{{ unsafe_string }}
{% autoescape false %}{{ safe_string }}{% endautoescape %}''')

    result = template.render(unsafe_string='<img src="#">', safe_string='<b>bold</b>')
    expected = '''\
&lt;img src=&#34;#&#34;&gt;
<b>bold</b>'''

    assert result == expected
Ejemplo n.º 6
0
def test_email_templates(plugins, expected, monkeypatch):
    monkeypatch.setattr(JinjaEnv, '_env', None)
    monkeypatch.setattr(JinjaEnv, '_exportable_functions', {})
    monkeypatch.setattr(JinjaEnv, '_filter_functions', {})
    monkeypatch.setattr(JinjaEnv, '_test_functions', {})
    monkeypatch.setattr(JinjaEnv, '_template_dirs', [])
    monkeypatch.setattr(AutomatedEmail, '_fixtures', OrderedDict())

    for plugin in plugins:
        JinjaEnv.insert_template_dir(
            os.path.join(__here__, plugin, 'templates'))

    with Session() as session:
        for email in session.query(AutomatedEmail).all():
            session.delete(email)

    for i in range(1, 4):
        AutomatedEmailFixture(Attendee,
                              'Test Template {}'.format(i),
                              'test_template.txt',
                              lambda a: True,
                              needs_approval=False,
                              ident='test_template_{}'.format(i))

    AutomatedEmail.reconcile_fixtures()

    with Session() as session:
        attendee = Attendee(first_name='Test',
                            last_name='Email',
                            email='*****@*****.**')
        for i in range(1, 4):
            automated_email = session.query(AutomatedEmail).filter_by(
                ident='test_template_{}'.format(i)).one()
            for i in range(1, 4):
                assert expected == automated_email.render_body(
                    attendee).strip(), 'render() call {}'.format(i)
Ejemplo n.º 7
0
def test_verify_jinja_autoescape_template():
    env = JinjaEnv.env()
    cur_dir = os.path.dirname(__file__)
    env.loader.searchpath.append(cur_dir)
    try:
        template = env.get_template('autoescape_template.html')
    finally:
        if cur_dir in env.loader.searchpath:
            env.loader.searchpath.remove(cur_dir)

    result = template.render(unsafe_string='<img src="#">', safe_string='<b>bold</b>')
    expected = '''\
<!DOCTYPE HTML>
<html>
<body>
&lt;img src=&#34;#&#34;&gt;
<b>bold</b></body>
</html>'''

    assert result == expected
Ejemplo n.º 8
0
def test_render_empty():
    env = JinjaEnv.env()
    cur_dir = os.path.dirname(__file__)
    env.loader.searchpath.append(cur_dir)
    try:
        template = env.get_template('autoescape_template.html')
    finally:
        if cur_dir in env.loader.searchpath:
            env.loader.searchpath.remove(cur_dir)

    result = str(open(template.filename, 'r').read())
    expected = '''\
<!DOCTYPE HTML>
<html>
<body>
{{ unsafe_string }}
{% autoescape false %}{{ safe_string }}{% endautoescape %}
</body>
</html>
'''

    assert result == expected
Ejemplo n.º 9
0
 def render_template(self, text, data):
     with request_cached_context(clear_cache_on_start=True):
         return JinjaEnv.env().from_string(text).render(data)
Ejemplo n.º 10
0
 def test_timedelta_filter_in_template(self):
     dt = datetime.utcnow()
     env = JinjaEnv.env()
     template = env.from_string('{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = (dt + timedelta(days=-5)).strftime("%A, %B %-e")
     assert expected == template.render(dt=dt)
Ejemplo n.º 11
0
 def test_timedelta_filter_in_template_with_empty_date(self):
     env = JinjaEnv.env()
     template = env.from_string(
         '{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = ''
     assert expected == template.render(dt=None)
Ejemplo n.º 12
0
        return text
    else:
        return Markup(text)


@JinjaEnv.jinja_filter
def basename(s):
    return os.path.basename(s) if s else ''


@JinjaEnv.jinja_test(name='class')
def is_class(value):
    return inspect.isclass(value)


fieldify = JinjaEnv.jinja_filter(fieldify)
unfieldify = JinjaEnv.jinja_filter(unfieldify)
readable_join = JinjaEnv.jinja_filter(readable_join)


@JinjaEnv.jinja_filter(name='datetime')
def datetime_filter(dt, fmt='%-I:%M%p %Z on %A, %b %-e'):
    return '' if not dt else ' '.join(dt.strftime(fmt).split()).replace('AM', 'am').replace('PM', 'pm')


@JinjaEnv.jinja_filter(name='datetime_local')
def datetime_local_filter(dt, fmt='%-I:%M%p %Z on %A, %b %-e'):
    return '' if not dt else datetime_filter(dt.astimezone(c.EVENT_TIMEZONE), fmt=fmt)


@JinjaEnv.jinja_filter
Ejemplo n.º 13
0
        return text
    else:
        return Markup(text)


@JinjaEnv.jinja_filter
def basename(s):
    return os.path.basename(s) if s else ''


@JinjaEnv.jinja_test(name='class')
def is_class(value):
    return inspect.isclass(value)


fieldify = JinjaEnv.jinja_filter(fieldify)
unfieldify = JinjaEnv.jinja_filter(unfieldify)
readable_join = JinjaEnv.jinja_filter(readable_join)


@JinjaEnv.jinja_filter(name='datetime')
def datetime_filter(dt, fmt='%-I:%M%p %Z on %A, %b %-e'):
    return '' if not dt else ' '.join(dt.strftime(fmt).split()).replace(
        'AM', 'am').replace('PM', 'pm')


@JinjaEnv.jinja_filter(name='datetime_local')
def datetime_local_filter(dt, fmt='%-I:%M%p %Z on %A, %b %-e'):
    return '' if not dt else datetime_filter(dt.astimezone(c.EVENT_TIMEZONE),
                                             fmt=fmt)
Ejemplo n.º 14
0
def render_empty(template_name_list):
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list)
    return str(open(template.filename, 'r').read())
Ejemplo n.º 15
0
 def test_timedelta_filter_in_template_with_empty_date(self):
     env = JinjaEnv.env()
     template = env.from_string('{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = ''
     assert expected == template.render(dt=None)
Ejemplo n.º 16
0
def is_valid_jinja_template(template_path):
    env = JinjaEnv.env()
    with open(template_path) as template_file:
        template = template_file.read()
        ast = env.parse(template)
        meta.find_undeclared_variables(ast)
Ejemplo n.º 17
0
def render_empty(template_name_list):
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list, use_request_cache=False)
    return open(template.filename, 'rb').read().decode('utf-8')
Ejemplo n.º 18
0
def is_valid_jinja_template(template_path):
    env = JinjaEnv.env()
    with open(template_path) as template_file:
        template = template_file.read()
        ast = env.parse(template)
        meta.find_undeclared_variables(ast)
Ejemplo n.º 19
0
def render_empty(template_name_list):
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list, use_request_cache=False)
    return open(template.filename, 'rb').read().decode('utf-8')
Ejemplo n.º 20
0
 def render_template(self, text, data):
     return JinjaEnv.env().from_string(text).render(data)