society-self-service/selfserv/jinja2.py

48 lines
1.5 KiB
Python
Raw Normal View History

2018-12-07 15:53:46 +11:00
# Society Self-Service
# Copyright © 2018 Yingtong Li (RunasSudo)
2018-06-26 20:14:16 +10:00
#
2018-12-07 15:53:46 +11:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2018-06-26 20:14:16 +10:00
#
2018-12-07 15:53:46 +11:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2018-06-26 20:14:16 +10:00
#
2018-12-07 15:53:46 +11:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import markdown
2018-06-26 20:14:16 +10:00
from django.contrib.staticfiles.storage import staticfiles_storage
2018-12-07 16:12:23 +11:00
from django.conf import settings
2018-06-26 20:14:16 +10:00
from django.urls import reverse
from django.utils import timezone
2019-03-06 10:43:32 +11:00
from jinja2 import Environment, Markup, select_autoescape
2018-06-26 20:14:16 +10:00
import importlib
def environment(**options):
2019-03-06 10:43:32 +11:00
options['autoescape'] = select_autoescape(
disabled_extensions=('txt',),
default_for_string=True,
default=True,
)
2018-06-26 20:14:16 +10:00
env = Environment(**options)
env.globals.update({
'import': importlib.import_module, # forgive me for I have sinned
'localtime': lambda dt: timezone.localtime(dt).strftime('%Y-%m-%d %H:%M'),
'settings': settings,
2018-06-26 20:14:16 +10:00
'static': staticfiles_storage.url,
'url': reverse,
2018-12-07 16:12:23 +11:00
'MEDIA_URL': settings.MEDIA_URL,
2018-06-26 20:14:16 +10:00
})
2018-12-07 15:53:46 +11:00
env.filters.update({
'markdown': lambda x: Markup(markdown.markdown(x))
})
2018-06-26 20:14:16 +10:00
return env