2016-08-08 11:09:23 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2021-05-17 23:51:02 +02:00
|
|
|
import orjson
|
2020-04-04 01:47:18 +02:00
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.template.defaultfilters import pluralize, slugify
|
2018-01-30 06:05:25 +01:00
|
|
|
from django.urls import reverse
|
2016-04-21 08:48:33 +02:00
|
|
|
from django.utils import translation
|
2019-03-04 13:55:32 +01:00
|
|
|
from django.utils.timesince import timesince
|
2016-04-21 08:48:33 +02:00
|
|
|
from jinja2 import Environment
|
2022-07-05 22:14:19 +02:00
|
|
|
from two_factor.plugins.phonenumber.templatetags.phonenumber import device_action
|
2016-04-21 08:48:33 +02:00
|
|
|
|
2021-04-24 01:32:32 +02:00
|
|
|
from zerver.context_processors import DEFAULT_PAGE_PARAMS
|
2021-06-11 10:45:10 +02:00
|
|
|
from zerver.lib.templates import display_list, render_markdown_path, webpack_entry
|
2016-04-21 08:48:33 +02:00
|
|
|
|
|
|
|
|
2021-05-17 23:51:02 +02:00
|
|
|
def json_dumps(obj: object) -> str:
|
|
|
|
return orjson.dumps(obj).decode()
|
|
|
|
|
|
|
|
|
2017-10-27 13:03:15 +02:00
|
|
|
def environment(**options: Any) -> Environment:
|
2016-04-21 08:48:33 +02:00
|
|
|
env = Environment(**options)
|
2020-09-03 05:32:15 +02:00
|
|
|
env.globals.update(
|
2021-04-24 01:32:32 +02:00
|
|
|
# default_page_params is provided here for responses where
|
|
|
|
# zulip_default_context is not run, including the 404.html and
|
|
|
|
# 500.html error pages.
|
|
|
|
default_page_params=DEFAULT_PAGE_PARAMS,
|
2020-09-03 05:32:15 +02:00
|
|
|
static=staticfiles_storage.url,
|
|
|
|
url=reverse,
|
|
|
|
render_markdown_path=render_markdown_path,
|
2021-03-17 03:11:19 +01:00
|
|
|
webpack_entry=webpack_entry,
|
2020-09-03 05:32:15 +02:00
|
|
|
)
|
2016-04-21 08:48:33 +02:00
|
|
|
|
2021-06-04 20:11:00 +02:00
|
|
|
env.install_gettext_translations(translation, True) # type: ignore[attr-defined] # Added by jinja2.ext.i18n
|
2016-04-21 08:48:33 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
env.filters["slugify"] = slugify
|
|
|
|
env.filters["pluralize"] = pluralize
|
|
|
|
env.filters["display_list"] = display_list
|
|
|
|
env.filters["device_action"] = device_action
|
|
|
|
env.filters["timesince"] = timesince
|
2016-04-21 08:48:33 +02:00
|
|
|
|
2021-06-04 20:11:00 +02:00
|
|
|
env.policies["json.dumps_function"] = json_dumps
|
|
|
|
env.policies["json.dumps_kwargs"] = {}
|
2021-05-17 23:51:02 +02:00
|
|
|
|
2016-04-21 08:48:33 +02:00
|
|
|
return env
|