Titivillus/titivillus/jinja2.py

38 lines
913 B
Python
Raw Normal View History

2018-08-10 08:39:51 -04:00
#!/usr/bin/env python3
"""
Custom Jinja2 environment.
"""
import jinja2
2018-10-12 10:34:57 -04:00
from user_messages import api
2018-08-10 08:39:51 -04:00
from django.urls import reverse
from django.utils.timezone import template_localtime
from django.contrib.staticfiles.storage import staticfiles_storage
2018-08-10 08:39:51 -04:00
def get_sub_msgs(**kwargs):
"""Returns all messages 'subscription' in the tag."""
messages = api.get_messages(**kwargs)
sub_msgs = []
for message in messages:
if 'subscription' in message.tags:
sub_msgs.insert(0, message)
return sub_msgs
2018-08-10 08:39:51 -04:00
def environment(**options):
"""
Custom Jinja2 environment.
"""
env = jinja2.Environment(**options)
env.globals.update({
'undefined': jinja2.StrictUndefined,
'autoescape': True,
'trim_blocks': True,
'lstrip_blocks': True,
'url': reverse,
'static': staticfiles_storage.url,
2018-10-12 10:34:57 -04:00
'get_messages': api.get_messages,
'localtime': template_localtime,
'get_sub_msgs': get_sub_msgs,
2018-08-10 08:39:51 -04:00
})
return env