backoffice: log lateral template errors (#67853)

This commit is contained in:
Frédéric Péters 2022-08-02 20:14:12 +02:00
parent 05374eee52
commit 1a85d14ff5
5 changed files with 49 additions and 4 deletions

View File

@ -2562,6 +2562,19 @@ def test_backoffice_sidebar_lateral_block(pub):
partial_resp = app.get(lateral_block_url)
assert partial_resp.text == '<div class="lateral-block">XXbouhXX</div>'
# 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)

View File

@ -1751,6 +1751,19 @@ def test_backoffice_submission_sidebar_lateral_block(pub):
partial_resp = app.get(lateral_block_url)
assert partial_resp.text == '<div class="lateral-block">foo bar blah</div>'
# 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)

View File

@ -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

View File

@ -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):

View File

@ -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)