misc: correctly attach cards to logged errors (#36635)

This commit is contained in:
Frédéric Péters 2020-01-28 16:51:01 +01:00
parent 2ddd0a9f18
commit 08ce2ae851
3 changed files with 74 additions and 4 deletions

View File

@ -5532,3 +5532,64 @@ def test_studio_card_item_link(pub, studio):
resp = app.get('/backoffice/data/bar/1/')
with pytest.raises(IndexError):
resp.click('card plop')
def test_backoffice_cards_wscall_failure_display(http_requests, pub, studio):
LoggedError.wipe()
user = create_user(pub)
Workflow.wipe()
workflow = Workflow(name='wscall')
workflow.roles = {
'_viewer': 'Viewer',
'_editor': 'Editor',
}
st1 = workflow.add_status('Recorded', 'recorded')
wscall = WebserviceCallStatusItem()
wscall.id = '_wscall'
wscall.varname = 'xxx'
wscall.url = 'http://remote.example.net/xml'
wscall.action_on_bad_data = ':stop'
wscall.record_errors = True
st1.items.append(wscall)
wscall.parent = st1
again = ChoiceWorkflowStatusItem()
again.id = '_again'
again.label = 'Again'
again.by = ['_editor']
again.status = st1.id
st1.items.append(again)
again.parent = st1
workflow.store()
CardDef.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.fields = [
fields.StringField(id='1', label='Test', type='string', varname='foo'),
]
carddef.backoffice_submission_roles = user.roles
carddef.workflow_id = workflow.id
carddef.workflow_roles = {'_editor': user.roles[0]}
carddef.digest_template = 'card {{form_var_foo}}'
carddef.store()
carddef.data_class().wipe()
carddata = carddef.data_class()()
carddata.data = {'1': 'plop'}
carddata.just_created()
carddata.store()
app = login(get_app(pub))
resp = app.get('/backoffice/data/foo/%s/' % carddata.id)
assert 'Again' in resp.text
resp = resp.forms[0].submit('button_again')
resp = resp.follow()
assert 'Error during webservice call' in resp.text
assert LoggedError.count() == 1
assert LoggedError.select()[0].get_formdata().data == {'1': 'plop'}

View File

@ -1251,6 +1251,7 @@ class FormDef(StorableObject):
d = {
'form_name': self.name,
'form_slug': self.url_name,
'form_class_name': self.__class__.__name__, # reserved for logged errors
}
if not minimal:
from wcs.variables import LazyFormDef

View File

@ -18,6 +18,7 @@ import datetime
from .qommon.misc import simplify
from .qommon.xml_storage import XmlStorableObject
from wcs.carddef import CardDef
from wcs.formdef import FormDef
from wcs.workflows import Workflow
@ -29,6 +30,7 @@ class LoggedError(XmlStorableObject):
_hashed_indexes = ['formdef_id', 'workflow_id']
summary = None
formdef_class = 'FormDef'
formdata_id = None
formdef_id = None
workflow_id = None
@ -50,6 +52,7 @@ class LoggedError(XmlStorableObject):
('exception_class', 'str'), ('exception_message', 'str'),
('expression', 'str'), ('expression_type', 'str'),
('formdata_id', 'str'), ('formdef_id', 'str'), ('workflow_id', 'str'),
('formdef_class', 'str'),
('status_id', 'str'), ('status_item_id', 'str'),
('occurences_count', 'int'),
('first_occurence_timestamp', 'datetime'),
@ -72,11 +75,11 @@ class LoggedError(XmlStorableObject):
if formdata:
error.formdata_id = str(formdata.id)
error.formdef_id = formdata.formdef.id
error.workflow_id = formdata.formdef.workflow.id
elif formdef:
formdef = formdata.formdef
if formdef:
error.formdef_id = formdef.id
error.workflow_id = formdef.workflow.id
error.formdef_class = formdef.__class__.__name__
if not error.formdef_id:
# cannot attach error to formdef, don't record in journal, it will
@ -118,7 +121,10 @@ class LoggedError(XmlStorableObject):
# cannot attach error to formdef, don't record in journal, it will
# still be sent by email to administrators.
return
formdef = FormDef.get_by_urlname(formdef_urlname)
klass = FormDef
if context.get('form_class_name') == 'CardDef':
klass = CardDef
formdef = klass.get_by_urlname(formdef_urlname)
formdata = formdef.data_class().get(formdata_id, ignore_errors=True)
return cls.record(error_summary, plain_error_msg, formdata=formdata,
formdef=formdef, workflow=formdef.workflow)
@ -138,6 +144,8 @@ class LoggedError(XmlStorableObject):
return tech_id[:200]
def get_formdef(self):
if self.formdef_class == 'CardDef':
return CardDef.get(self.formdef_id, ignore_errors=True)
return FormDef.get(self.formdef_id, ignore_errors=True)
def get_workflow(self):