From 1a85d14ff5115cc8615be0a3bb67de0f7bb470ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 2 Aug 2022 20:14:12 +0200 Subject: [PATCH] backoffice: log lateral template errors (#67853) --- tests/backoffice_pages/test_all.py | 13 +++++++++++++ tests/backoffice_pages/test_submission.py | 13 +++++++++++++ wcs/formdata.py | 12 +++++++++++- wcs/formdef.py | 11 ++++++++++- wcs/variables.py | 4 ++-- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/tests/backoffice_pages/test_all.py b/tests/backoffice_pages/test_all.py index 426c241a4..97d60a417 100644 --- a/tests/backoffice_pages/test_all.py +++ b/tests/backoffice_pages/test_all.py @@ -2562,6 +2562,19 @@ def test_backoffice_sidebar_lateral_block(pub): partial_resp = app.get(lateral_block_url) assert partial_resp.text == '
XXbouhXX
' + # error in lateral template + formdef.lateral_template = 'XX{{ form|first }}XX' + formdef.store() + + pub.loggederror_class.wipe() + partial_resp = app.get(lateral_block_url) + assert partial_resp.text == '' + assert pub.loggederror_class.count() == 1 + assert ( + pub.loggederror_class.select()[0].summary + == "Could not render lateral template ('LazyFormData' object has no attribute '0')" + ) + def test_count_open(pub): create_user(pub) diff --git a/tests/backoffice_pages/test_submission.py b/tests/backoffice_pages/test_submission.py index e2139e4bf..a8b954be6 100644 --- a/tests/backoffice_pages/test_submission.py +++ b/tests/backoffice_pages/test_submission.py @@ -1751,6 +1751,19 @@ def test_backoffice_submission_sidebar_lateral_block(pub): partial_resp = app.get(lateral_block_url) assert partial_resp.text == '
foo bar blah
' + # error in lateral template + formdef.submission_lateral_template = 'XX{{today|list}}XX' + formdef.store() + + pub.loggederror_class.wipe() + partial_resp = app.get(lateral_block_url) + assert partial_resp.text == '' + assert pub.loggederror_class.count() == 1 + assert ( + pub.loggederror_class.select()[0].summary + == "Could not render submission lateral template ('datetime.date' object is not iterable)" + ) + def test_backoffice_submission_computed_field(pub): user = create_user(pub) diff --git a/wcs/formdata.py b/wcs/formdata.py index dbbdecc72..109109671 100644 --- a/wcs/formdata.py +++ b/wcs/formdata.py @@ -579,7 +579,17 @@ class FormData(StorableObject): if self.formdef.lateral_template is None: new_value = None else: - new_value = Template(self.formdef.lateral_template, autoescape=False).render(context) + try: + new_value = Template(self.formdef.lateral_template, autoescape=False, raises=True).render( + context + ) + except Exception as e: + get_publisher().record_error( + _('Could not render lateral template (%s)') % e, + formdata=self, + exception=e, + ) + return None return new_value # criticality levels are stored as [0, 101, 102, 103...], this makes it diff --git a/wcs/formdef.py b/wcs/formdef.py index be9e15e90..3489514e8 100644 --- a/wcs/formdef.py +++ b/wcs/formdef.py @@ -801,7 +801,16 @@ class FormDef(StorableObject): if self.submission_lateral_template is None: new_value = None else: - new_value = Template(self.submission_lateral_template, autoescape=False).render(context) + try: + new_value = Template(self.submission_lateral_template, autoescape=False).render(context) + except Exception as e: + get_publisher().record_error( + _('Could not render submission lateral template (%s)' % e), + formdef=self, + exception=e, + ) + return None + return new_value def create_form(self, page=None, displayed_fields=None, transient_formdata=None): diff --git a/wcs/variables.py b/wcs/variables.py index a34c636ce..0eb299898 100644 --- a/wcs/variables.py +++ b/wcs/variables.py @@ -706,9 +706,9 @@ class LazyFormData(LazyFormDef): def __getitem__(self, key): try: - return getattr(self, key) + return getattr(self, str(key)) except AttributeError: - if key.startswith('f'): + if isinstance(key, str) and key.startswith('f'): for field in self._formdef.get_all_fields(): if str(field.id).replace('-', '_') == str(key[1:]): return self._formdata.data.get(field.id)