workflows: deal with varname-less fields used as attachments (#33366)

This commit is contained in:
Frédéric Péters 2019-05-23 11:21:17 +02:00
parent 0bd83cf752
commit f561773ccb
4 changed files with 29 additions and 12 deletions

View File

@ -2394,9 +2394,9 @@ def test_workflows_edit_email_action(pub):
workflow = Workflow.get(workflow.id)
workflow.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(workflow)
workflow.backoffice_fields_formdef.fields = [
fields.FileField(id='bo1', label='bo field 1', type='file', varname='upload'),
fields.FileField(id='bo2', label='bo field 2', type='file', varname='upload2'),
fields.FileField(id='bo3', label='bo field varnameless', type='file'),
fields.FileField(id='bo1-1x', label='bo field 1', type='file', varname='upload'),
fields.FileField(id='bo2-2x', label='bo field 2', type='file', varname='upload2'),
fields.FileField(id='bo3-3x', label='bo field varnameless', type='file'),
]
workflow.store()
resp = app.get(item_url)
@ -2411,11 +2411,11 @@ def test_workflows_edit_email_action(pub):
assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw']
resp = app.get(item_url)
resp.form['attachments$element2$choice'] = 'form_fbo3'
resp.form['attachments$element2$choice'] = 'form_fbo3_3x'
resp = resp.form.submit('submit')
assert resp.location
sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw', 'form_fbo3']
assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw', 'form_fbo3_3x']
resp = app.get(item_url)
resp.form['attachments$element3$choice'] = '__other'
@ -2423,7 +2423,7 @@ def test_workflows_edit_email_action(pub):
resp = resp.form.submit('submit')
assert resp.location
sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw', 'form_fbo3',
assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw', 'form_fbo3_3x',
'{"content":"foo", "filename":"bar.txt"}']
# remove some backoffice fields: varnameless fbo3 disapear

View File

@ -1189,7 +1189,7 @@ def test_email_attachments(pub, emails):
wf = Workflow(name='email with attachments')
wf.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(wf)
wf.backoffice_fields_formdef.fields = [
FileField(id='bo1', label='bo field 1', type='file', varname='backoffice_file1'),
FileField(id='bo1-1x', label='bo field 1', type='file', varname='backoffice_file1'),
FileField(id='bo2', label='bo field 2', type='file', varname='backoffice_file2'),
]
st1 = wf.add_status('Status1')
@ -1206,14 +1206,15 @@ def test_email_attachments(pub, emails):
formdata.just_created()
formdata.store()
pub.substitutions.feed(formdata)
# store file in backoffice field form_fbo1 / form_var_backoffice_file_raw
# store file in backoffice field form_fbo1_1x / form_var_backoffice_file_raw
setbo = SetBackofficeFieldsWorkflowStatusItem()
setbo.parent = st1
setbo.fields = [{'field_id': 'bo1', 'value': '=form_var_frontoffice_file_raw'}]
setbo.fields = [{'field_id': 'bo1-1x', 'value': '=form_var_frontoffice_file_raw'}]
setbo.perform(formdata)
# check compatibility with actions defined before #33366 was fixed
emails.empty()
sendmail.attachments = ['form_fbo1']
sendmail.attachments = ['form_fbo1-1x']
sendmail.perform(formdata)
get_response().process_after_jobs()
assert emails.count() == 1
@ -1222,6 +1223,18 @@ def test_email_attachments(pub, emails):
assert emails.emails['foobar']['msg'].get_payload()[0].get_content_type() == 'text/html'
assert emails.emails['foobar']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
# check with correct varname-less field
emails.empty()
sendmail.attachments = ['form_fbo1_1x']
sendmail.perform(formdata)
get_response().process_after_jobs()
assert emails.count() == 1
assert emails.emails['foobar']['msg'].is_multipart()
assert emails.emails['foobar']['msg'].get_content_subtype() == 'mixed'
assert emails.emails['foobar']['msg'].get_payload()[0].get_content_type() == 'text/html'
assert emails.emails['foobar']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
# check with variable
emails.empty()
sendmail.attachments = ['form_var_backoffice_file1_raw']
sendmail.perform(formdata)

View File

@ -54,7 +54,7 @@ def get_dict_with_varnames(fields, data, formdata=None, varnames_only=False):
if not varnames_only:
# add it as f$n$
new_data['f%s' % field.id] = value
new_data['f%s' % field.id.replace('-', '_')] = value
# also add it as 'field_' + normalized(field label)
identifier_name = qommon.misc.simplify(field.label, space = '_')

View File

@ -2296,7 +2296,7 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem):
if field.varname:
codename = 'form_var_%s_raw' % field.varname
else:
codename = 'form_f%s' % field.id # = form_fbo<n>
codename = 'form_f%s' % field.id.replace('-', '_') # = form_fbo<...>
varnameless.append(codename)
attachments_options.append((codename, field.label, codename))
# filter: do not consider removed fields without varname
@ -2434,6 +2434,10 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem):
global_eval_dict = get_publisher().get_global_eval_dict()
local_eval_dict = get_publisher().substitutions.get_context_variables()
for attachment in self.attachments:
if attachment.startswith('form_fbo') and '-' in attachment:
# detect varname-less backoffice fields that were set
# before #33366 was fixed, and fix them.
attachment = attachment.replace('-', '_')
try:
picklableupload = eval(attachment, global_eval_dict, local_eval_dict)
except: