Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

jinja2: Serialize page-params with orjson.

Anders Kaseorg79c087471c26
diff --git a/zproject/jinja2/__init__.py b/zproject/jinja2/__init__.py
index 02624abaa4c..2a3692db2cc 100644
--- a/zproject/jinja2/__init__.py
+++ b/zproject/jinja2/__init__.py
@@ -1,5 +1,6 @@
from typing import Any
+import orjson
from django.contrib.staticfiles.storage import staticfiles_storage
from django.template.defaultfilters import pluralize, slugify
from django.urls import reverse
@@ -12,6 +13,10 @@ from zerver.context_processors import DEFAULT_PAGE_PARAMS
from zerver.templatetags.app_filters import display_list, render_markdown_path, webpack_entry
+def json_dumps(obj: object) -> str:
+ return orjson.dumps(obj).decode()
+
+
def environment(**options: Any) -> Environment:
env = Environment(**options)
env.globals.update(
@@ -33,4 +38,7 @@ def environment(**options: Any) -> Environment:
env.filters["device_action"] = device_action
env.filters["timesince"] = timesince
+ env.policies["json.dumps_function"] = json_dumps # type: ignore[attr-defined] # type annotation will be fixed in Jinja2 3.0.0
+ env.policies["json.dumps_kwargs"] = {} # type: ignore[attr-defined] # type annotation will be fixed in Jinja2 3.0.0
+
return env