Add jinja fromjson custom filter

pull/2378/head
Delena Malan 7 months ago
parent f0ed4f64e8
commit 4892b28af3

@ -0,0 +1,6 @@
import json
def fromjson(value):
if value is None or not value:
return ""
return json.loads(value)

@ -32,6 +32,7 @@ from changedetectionio import html_tools, content_fetchers
from changedetectionio.notification import ( from changedetectionio.notification import (
valid_notification_formats, valid_notification_formats,
) )
from changedetectionio.filters import fromjson
from wtforms.fields import FormField from wtforms.fields import FormField
@ -246,6 +247,7 @@ class ValidateJinja2Template(object):
try: try:
jinja2_env = ImmutableSandboxedEnvironment(loader=BaseLoader) jinja2_env = ImmutableSandboxedEnvironment(loader=BaseLoader)
jinja2_env.filters['fromjson'] = fromjson
jinja2_env.globals.update(notification.valid_tokens) jinja2_env.globals.update(notification.valid_tokens)
jinja2_env.from_string(joined_data).render() jinja2_env.from_string(joined_data).render()
except TemplateSyntaxError as e: except TemplateSyntaxError as e:

@ -8,11 +8,14 @@ import jinja2.sandbox
import typing as t import typing as t
import os import os
from changedetectionio.filters import fromjson
JINJA2_MAX_RETURN_PAYLOAD_SIZE = 1024 * int(os.getenv("JINJA2_MAX_RETURN_PAYLOAD_SIZE_KB", 1024 * 10)) JINJA2_MAX_RETURN_PAYLOAD_SIZE = 1024 * int(os.getenv("JINJA2_MAX_RETURN_PAYLOAD_SIZE_KB", 1024 * 10))
def render(template_str, **args: t.Any) -> str: def render(template_str, **args: t.Any) -> str:
jinja2_env = jinja2.sandbox.ImmutableSandboxedEnvironment(extensions=['jinja2_time.TimeExtension']) jinja2_env = jinja2.sandbox.ImmutableSandboxedEnvironment(extensions=['jinja2_time.TimeExtension'])
jinja2_env.filters['fromjson'] = fromjson
output = jinja2_env.from_string(template_str).render(args) output = jinja2_env.from_string(template_str).render(args)
return output[:JINJA2_MAX_RETURN_PAYLOAD_SIZE] return output[:JINJA2_MAX_RETURN_PAYLOAD_SIZE]

Loading…
Cancel
Save