workflows: better form error message for complex actions (#50657)

This commit is contained in:
Lauréline Guérin 2021-02-08 14:45:22 +01:00
parent 554d90467e
commit 08893a339c
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 167 additions and 1 deletions

View File

@ -25,9 +25,12 @@ from wcs.workflows import (
ChoiceWorkflowStatusItem,
JumpOnSubmitWorkflowStatusItem,
)
from wcs.wf.create_carddata import CreateCarddataWorkflowStatusItem
from wcs.wf.create_formdata import CreateFormdataWorkflowStatusItem, Mapping
from wcs.wf.dispatch import DispatchWorkflowStatusItem
from wcs.wf.edit_carddata import EditCarddataWorkflowStatusItem
from wcs.wf.export_to_model import ExportToModel
from wcs.wf.external_workflow import ExternalWorkflowGlobalAction
from wcs.wf.jump import JumpWorkflowStatusItem
from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
from wcs.wf.register_comment import RegisterCommenterWorkflowStatusItem
@ -1749,6 +1752,49 @@ def test_workflows_global_actions_external_workflow_action(pub):
resp = resp.click(href=re.compile(r'^items/1/$'), index=0)
def test_workflows_external_workflow_action_config(pub):
create_superuser(pub)
Workflow.wipe()
external_wf = Workflow(name='external')
action = external_wf.add_global_action('Global action')
trigger = action.append_trigger('webservice')
trigger.identifier = 'test'
action.append_item('remove')
external_wf.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'external'
formdef.workflow = external_wf
formdef.store()
wf = Workflow(name='foo')
st = wf.add_status('New')
external = ExternalWorkflowGlobalAction()
external.parent = st
st.items.append(external)
wf.store()
app = login(get_app(pub))
resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
# only action error: custom error message
resp.forms[0]['slug'] = 'formdef:external'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' not in resp
assert 'This action is configured in two steps. See below for details.' in resp
assert "required field" in resp
# multiple errors: do as usual
resp.forms[0]['slug'] = 'formdef:external'
resp.forms[0]['condition$type'] = 'django'
resp.forms[0]['condition$value_django'] = '{{ 42 }}'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' in resp
assert 'This action is configured in two steps. See below for details.' not in resp
assert "required field" in resp
assert "syntax error: Could not parse the remainder: '{{' from '{{'" in resp
def test_workflows_create_formdata(pub):
create_superuser(pub)
create_role()
@ -1858,6 +1904,114 @@ def test_workflows_create_formdata(pub):
resp.form.submit('submit') # no error
def test_workflows_create_formdata_action_config(pub):
create_superuser(pub)
FormDef.wipe()
target_formdef = FormDef()
target_formdef.name = 'target form'
target_formdef.fields = []
target_formdef.store()
Workflow.wipe()
wf = Workflow(name='create-formdata')
st = wf.add_status('New')
create_formdata = CreateFormdataWorkflowStatusItem()
create_formdata.parent = st
st.items.append(create_formdata)
wf.store()
app = login(get_app(pub))
resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
# only mapping error: custom error message
resp.forms[0]['formdef_slug'] = 'target-form'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' not in resp
assert 'This action is configured in two steps. See below for details.' in resp
assert 'Please define new mappings' in resp
# multiple errors: do as usual
resp.forms[0]['formdef_slug'] = 'target-form'
resp.forms[0]['condition$type'] = 'django'
resp.forms[0]['condition$value_django'] = '{{ 42 }}'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' in resp
assert 'This action is configured in two steps. See below for details.' not in resp
assert 'Please define new mappings' in resp
assert "syntax error: Could not parse the remainder: '{{' from '{{'" in resp
def test_workflows_create_carddata_action_config(pub):
create_superuser(pub)
CardDef.wipe()
carddef = CardDef()
carddef.name = 'My card'
carddef.fields = []
carddef.store()
Workflow.wipe()
wf = Workflow(name='create-carddata')
st = wf.add_status('New')
create_carddata = CreateCarddataWorkflowStatusItem()
create_carddata.parent = st
st.items.append(create_carddata)
wf.store()
app = login(get_app(pub))
resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
# only mapping error: custom error message
resp.forms[0]['formdef_slug'] = 'my-card'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' not in resp
assert 'This action is configured in two steps. See below for details.' in resp
assert 'Please define new mappings' in resp
# multiple errors: do as usual
resp.forms[0]['formdef_slug'] = 'my-card'
resp.forms[0]['condition$type'] = 'django'
resp.forms[0]['condition$value_django'] = '{{ 42 }}'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' in resp
assert 'This action is configured in two steps. See below for details.' not in resp
assert 'Please define new mappings' in resp
assert "syntax error: Could not parse the remainder: '{{' from '{{'" in resp
def test_workflows_edit_carddata_action_config(pub):
create_superuser(pub)
CardDef.wipe()
carddef = CardDef()
carddef.name = 'My card'
carddef.fields = []
carddef.store()
Workflow.wipe()
wf = Workflow(name='edit-carddata')
st = wf.add_status('New')
edit_carddata = EditCarddataWorkflowStatusItem()
edit_carddata.parent = st
st.items.append(edit_carddata)
wf.store()
app = login(get_app(pub))
resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
# only mapping error: custom error message
resp.forms[0]['formdef_slug'] = 'my-card'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' not in resp
assert 'This action is configured in two steps. See below for details.' in resp
assert 'Please define new mappings' in resp
# multiple errors: do as usual
resp.forms[0]['formdef_slug'] = 'my-card'
resp.forms[0]['condition$type'] = 'django'
resp.forms[0]['condition$value_django'] = '{{ 42 }}'
resp = resp.forms[0].submit('submit')
assert 'There were errors processing your form. See below for details.' in resp
assert 'This action is configured in two steps. See below for details.' not in resp
assert 'Please define new mappings' in resp
assert "syntax error: Could not parse the remainder: '{{' from '{{'" in resp
def test_workflows_edit_carddata_action(pub):
create_superuser(pub)
Workflow.wipe()

View File

@ -24,6 +24,7 @@ from django.utils.functional import cached_property
from wcs.qommon import _, N_
from wcs.qommon.form import (
Form,
WidgetListAsTable,
CompositeWidget,
SingleSelectWidget,
@ -337,6 +338,11 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
title=_('Include new form in the form history'),
value=self.attach_to_history,
)
errors = [w.name for w in form.get_all_widgets() if w.has_error()]
if set(errors) == set(['%smappings' % prefix]):
form.ERROR_NOTICE = _('This action is configured in two steps. See below for details.')
else:
form.ERROR_NOTICE = Form.ERROR_NOTICE
def get_mappings_parameter_view_value(self):
to_id_fields = {str(field.id): field for field in self.formdef.get_widget_fields()}

View File

@ -17,7 +17,7 @@
from quixote import get_publisher
from wcs.qommon import _
from wcs.qommon.form import SingleSelectWidget, ComputedExpressionWidget
from wcs.qommon.form import Form, SingleSelectWidget, ComputedExpressionWidget
from wcs.workflows import WorkflowStatusItem, perform_items, register_item_class
from wcs.workflows import WorkflowGlobalActionWebserviceTrigger, Workflow
@ -131,6 +131,12 @@ class ExternalWorkflowGlobalAction(WorkflowStatusItem):
options=triggers_names,
)
errors = [w.name for w in form.get_all_widgets() if w.has_error()]
if set(errors) == set(['%strigger_id' % prefix]):
form.ERROR_NOTICE = _('This action is configured in two steps. See below for details.')
else:
form.ERROR_NOTICE = Form.ERROR_NOTICE
def get_line_details(self):
if self.slug and self.trigger_id:
objectdef = self.get_object_def()