workflows/create_formdata: add tracking-code before workflow execution (#49913)

This commit is contained in:
Emmanuel Cazenave 2021-01-06 15:59:21 +01:00
parent 770acecd23
commit 2152451bcc
2 changed files with 66 additions and 4 deletions

View File

@ -5159,6 +5159,65 @@ def test_create_formdata(two_pubs):
assert target_formdef.data_class().count() == 0
def test_create_formdata_tracking_code(two_pubs, emails):
FormDef.wipe()
if two_pubs.is_using_postgresql():
two_pubs.loggederror_class.wipe()
two_pubs.tracking_code_class.wipe()
target_wf = Workflow(name='send-mail')
st1 = target_wf.add_status('Status1', 'st1')
item = SendmailWorkflowStatusItem()
item.to = ['bar@localhost']
item.subject = 'Foobar'
item.body = '{{ form_tracking_code }}'
st1.items.append(item)
item.parent = st1
target_wf.store()
target_formdef = FormDef()
target_formdef.name = 'target-form'
target_formdef.fields = [
EmailField(id='0', label='Email', type='email'),
]
target_formdef.workflow_id = target_wf.id
target_formdef.enable_tracking_codes = True
target_formdef.store()
wf = Workflow(name='create-formdata')
st1 = wf.add_status('Status1', 'st1')
create = CreateFormdataWorkflowStatusItem()
create.formdef_slug = target_formdef.url_name
create.mappings = [
Mapping(field_id='0', expression='=form_var_email_string'),
]
st1.items.append(create)
item.parent = st1
wf.store()
formdef = FormDef()
formdef.name = 'source form'
formdef.fields = [
EmailField(id='0', label='Email', type='email'),
]
formdef.workflow_id = wf.id
formdef.store()
formdata = formdef.data_class()()
formdata.data = {}
formdata.just_created()
assert target_formdef.data_class().count() == 0
assert emails.count() == 0
formdata.perform_workflow()
get_response().process_after_jobs()
assert target_formdef.data_class().count() == 1
assert emails.count() == 1
tracking_code = target_formdef.data_class().select()[0].tracking_code
assert tracking_code in emails.get('Foobar')['payload']
def test_create_carddata(two_pubs):
CardDef.wipe()
FormDef.wipe()

View File

@ -351,21 +351,24 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
self.apply_mappings(dest=new_formdata, src=formdata)
if formdef.enable_tracking_codes:
code = get_publisher().tracking_code_class()
if self.draft:
new_formdata.status = 'draft'
new_formdata.store()
if formdef.enable_tracking_codes:
code.formdata = new_formdata # this will .store() the code
else:
# freeze substitutions during submission, as it has side effects
with get_publisher().substitutions.freeze():
new_formdata.just_created()
new_formdata.store()
if formdef.enable_tracking_codes:
code.formdata = new_formdata # this will .store() the code
new_formdata.perform_workflow()
new_formdata.store()
if formdef.enable_tracking_codes:
code = get_publisher().tracking_code_class()
code.formdata = new_formdata # this will .store() the code
if new_formdata.user_id is None and not new_formdata.backoffice_submission and get_session():
get_session().mark_anonymous_formdata(new_formdata)