comparison sat/tools/common/template.py @ 3306:3af0909629a2

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.
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 15:45:57 +0200
parents 430204a3cc10
children 29f8122f00f3
comparison
equal deleted inserted replaced
3305:04a7d77ed86f 3306:3af0909629a2
449 self.env.tests["in_the_past"] = self._in_the_past 449 self.env.tests["in_the_past"] = self._in_the_past
450 self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg") 450 self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg")
451 451
452 # policies 452 # policies
453 self.env.policies["ext.i18n.trimmed"] = True 453 self.env.policies["ext.i18n.trimmed"] = True
454 self.env.policies["json.dumps_kwargs"] = {
455 "sort_keys": True,
456 # if object can't be serialised, we use None
457 "default": lambda o: o.json() if hasattr(o, "json") else None
458 }
454 459
455 def getFrontURL(self, template_data, path=None): 460 def getFrontURL(self, template_data, path=None):
456 """Give front URL (i.e. URL seen by end-user) of a path 461 """Give front URL (i.e. URL seen by end-user) of a path
457 462
458 @param template_data[TemplateData]: data of current template 463 @param template_data[TemplateData]: data of current template