workflow: fix get_submitter_email with blockdef (#50871)
This commit is contained in:
parent
54fc8a9ec3
commit
26085c1190
|
@ -217,9 +217,17 @@ def test_file_field(pub, formdef):
|
||||||
|
|
||||||
|
|
||||||
def test_get_submitter(pub, formdef):
|
def test_get_submitter(pub, formdef):
|
||||||
|
BlockDef.wipe()
|
||||||
|
block = BlockDef()
|
||||||
|
block.name = 'foobar'
|
||||||
|
block.fields = [
|
||||||
|
fields.StringField(id='0', label='email', varname='foo', prefill={'type': 'user', 'value': 'email'})
|
||||||
|
]
|
||||||
|
block.store()
|
||||||
formdef.data_class().wipe()
|
formdef.data_class().wipe()
|
||||||
formdef.fields = [
|
formdef.fields = [
|
||||||
fields.StringField(id='0', label='email', varname='foo', prefill={'type': 'user', 'value': 'email'})
|
fields.StringField(id='0', label='email', varname='foo', prefill={'type': 'user', 'value': 'email'}),
|
||||||
|
fields.BlockField(id='1', label='test', type='block:foobar', max_items=3, varname='block'),
|
||||||
]
|
]
|
||||||
formdef.store()
|
formdef.store()
|
||||||
|
|
||||||
|
@ -228,6 +236,42 @@ def test_get_submitter(pub, formdef):
|
||||||
formdata.data = {'0': 'foo@localhost'}
|
formdata.data = {'0': 'foo@localhost'}
|
||||||
assert formdef.get_submitter_email(formdata) == 'foo@localhost'
|
assert formdef.get_submitter_email(formdata) == 'foo@localhost'
|
||||||
|
|
||||||
|
formdata.data = {
|
||||||
|
'1': {
|
||||||
|
'data': [{'0': 'baz@localhost'}, {'0': ''}],
|
||||||
|
'schema': {'0': 'string'},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert formdef.get_submitter_email(formdata) == 'baz@localhost'
|
||||||
|
|
||||||
|
formdata.data = {
|
||||||
|
'1': {
|
||||||
|
'data': [{'0': 'baz@localhost'}, {'0': 'foo@localhost'}],
|
||||||
|
'schema': {'0': 'string'},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert formdef.get_submitter_email(formdata) == 'baz@localhost'
|
||||||
|
|
||||||
|
formdata.data = {
|
||||||
|
'1': {
|
||||||
|
'data': [{'0': ''}, {'0': 'foo@localhost'}],
|
||||||
|
'schema': {'0': 'string'},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert formdef.get_submitter_email(formdata) == 'foo@localhost'
|
||||||
|
|
||||||
|
formdata.data = {'1': {}}
|
||||||
|
assert formdef.get_submitter_email(formdata) is None
|
||||||
|
|
||||||
|
formdata.data = {
|
||||||
|
'0': 'foo@localhost',
|
||||||
|
'1': {
|
||||||
|
'data': [{'0': 'baz@localhost'}, {'0': ''}],
|
||||||
|
'schema': {'0': 'string'},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert formdef.get_submitter_email(formdata) == 'foo@localhost'
|
||||||
|
|
||||||
user = pub.user_class()
|
user = pub.user_class()
|
||||||
user.email = 'bar@localhost'
|
user.email = 'bar@localhost'
|
||||||
user.store()
|
user.store()
|
||||||
|
|
|
@ -1324,16 +1324,40 @@ class FormDef(StorableObject):
|
||||||
|
|
||||||
# look up in submitted form for one that would hold the user
|
# look up in submitted form for one that would hold the user
|
||||||
# email (the one set to be prefilled by user email)
|
# email (the one set to be prefilled by user email)
|
||||||
|
|
||||||
|
def is_user_field(field):
|
||||||
|
if not getattr(field, 'prefill', None):
|
||||||
|
return False
|
||||||
|
if field.prefill.get('type') != 'user':
|
||||||
|
return False
|
||||||
|
if field.prefill.get('value') != field_email:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
if formdata.data:
|
if formdata.data:
|
||||||
fields = formdata.formdef.fields
|
# check first in "normal" fields
|
||||||
for field in fields:
|
for field in formdata.formdef.fields:
|
||||||
if not hasattr(field, 'prefill'):
|
if not is_user_field(field):
|
||||||
continue
|
continue
|
||||||
if field.prefill and field.prefill.get('type') == 'user':
|
|
||||||
if field.prefill.get('value') == field_email:
|
v = formdata.data.get(field.id)
|
||||||
v = formdata.data.get(field.id)
|
if v:
|
||||||
if v:
|
return v
|
||||||
return v
|
|
||||||
|
# then check in block fields
|
||||||
|
for field in formdata.formdef.fields:
|
||||||
|
if field.key != 'block':
|
||||||
|
continue
|
||||||
|
for subfield in field.block.fields:
|
||||||
|
if not is_user_field(subfield):
|
||||||
|
continue
|
||||||
|
v = formdata.data.get(field.id)
|
||||||
|
if not v.get('data'):
|
||||||
|
continue
|
||||||
|
for data in v.get('data'):
|
||||||
|
w = data.get(subfield.id)
|
||||||
|
if w:
|
||||||
|
return w
|
||||||
|
|
||||||
# if nothing was found, get email from user profile
|
# if nothing was found, get email from user profile
|
||||||
if formdata.user and formdata.user.email and formdata.user.is_active:
|
if formdata.user and formdata.user.email and formdata.user.is_active:
|
||||||
|
|
Loading…
Reference in New Issue