From 4b0d91cb2e52dfa95d6505bd214a2f6979ac1b61 Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 19 Jun 2020 15:45:57 +0200 Subject: [PATCH] common (template): better json dumping: default method is set for json dumping (the `tojson` filter), if the object has a `json` attribute, it will be called, otherwise it is replaced by None, this avoids crash when rendering the template. --- sat/tools/common/template.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sat/tools/common/template.py b/sat/tools/common/template.py index 3bd07003..6790a350 100644 --- a/sat/tools/common/template.py +++ b/sat/tools/common/template.py @@ -451,6 +451,11 @@ class Renderer(object): # policies self.env.policies["ext.i18n.trimmed"] = True + self.env.policies["json.dumps_kwargs"] = { + "sort_keys": True, + # if object can't be serialised, we use None + "default": lambda o: o.json() if hasattr(o, "json") else None + } def getFrontURL(self, template_data, path=None): """Give front URL (i.e. URL seen by end-user) of a path