UI - Add extra validation help for notification body with Jinja2 markup (#1810)

pull/1817/head
dgtlmoon 1 year ago committed by GitHub
parent 8887459462
commit 51e2e8a226
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -229,16 +229,19 @@ class ValidateJinja2Template(object):
def __call__(self, form, field): def __call__(self, form, field):
from changedetectionio import notification from changedetectionio import notification
from jinja2 import Environment, BaseLoader, TemplateSyntaxError from jinja2 import Environment, BaseLoader, TemplateSyntaxError, UndefinedError
from jinja2.meta import find_undeclared_variables from jinja2.meta import find_undeclared_variables
try: try:
jinja2_env = Environment(loader=BaseLoader) jinja2_env = Environment(loader=BaseLoader)
jinja2_env.globals.update(notification.valid_tokens) jinja2_env.globals.update(notification.valid_tokens)
rendered = jinja2_env.from_string(field.data).render() rendered = jinja2_env.from_string(field.data).render()
except TemplateSyntaxError as e: except TemplateSyntaxError as e:
raise ValidationError(f"This is not a valid Jinja2 template: {e}") from e raise ValidationError(f"This is not a valid Jinja2 template: {e}") from e
except UndefinedError as e:
raise ValidationError(f"A variable or function is not defined: {e}") from e
ast = jinja2_env.parse(field.data) ast = jinja2_env.parse(field.data)
undefined = ", ".join(find_undeclared_variables(ast)) undefined = ", ".join(find_undeclared_variables(ast))

Loading…
Cancel
Save