Titivillus/titivillus/jinja2.py

38 lines
913 B
Python

#!/usr/bin/env python3
"""
Custom Jinja2 environment.
"""
import jinja2
from user_messages import api
from django.urls import reverse
from django.utils.timezone import template_localtime
from django.contrib.staticfiles.storage import staticfiles_storage
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
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,
'get_messages': api.get_messages,
'localtime': template_localtime,
'get_sub_msgs': get_sub_msgs,
})
return env