misc: prefer email from formdata over user profile (#10123)

This commit is contained in:
Frédéric Péters 2016-12-21 15:16:50 +01:00
parent 8c1d8009f9
commit 5772e64846
2 changed files with 18 additions and 25 deletions

View File

@ -290,6 +290,9 @@ def test_get_submitter(pub):
user.store()
formdata.user_id = user.id
assert formdef.get_submitter_email(formdata) == 'foo@localhost'
formdata.data = {}
assert formdef.get_submitter_email(formdata) == 'bar@localhost'
def test_get_last_update_time(pub):

View File

@ -991,34 +991,24 @@ class FormDef(StorableObject):
def get_submitter_email(self, formdata):
users_cfg = get_cfg('users', {})
field_email = users_cfg.get('field_email') or 'email'
if formdata.user:
if field_email == 'email' and formdata.user.email:
return formdata.user.email
elif formdata.user.form_data and formdata.user.form_data.get(field_email):
return formdata.user.form_data.get(field_email)
else:
# this shouldn't happen, but then data can get unsynced, so
# even if there's some user custom form with an unfilled email
# form we look at the straight email attribute, and use it if
# it exists.
if formdata.user.email:
return formdata.user.email
# if there is no user, or user has no email address, 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)
if not formdata.data:
return None
if formdata.data:
fields = formdata.formdef.fields
for field in fields:
if not hasattr(field, 'prefill'):
continue
if field.prefill and field.prefill.get('type') == 'user':
if field.prefill.get('value') == field_email:
v = formdata.data.get(field.id)
if v:
return v
# if nothing was found, get email from user profile
if formdata.user and formdata.user.email:
return formdata.user.email
fields = formdata.formdef.fields
for field in fields:
if not hasattr(field, 'prefill'):
continue
if field.prefill and field.prefill.get('type') == 'user':
if field.prefill.get('value') == field_email:
v = formdata.data.get(field.id)
if v:
return v
return None
def get_substitution_variables(self, minimal=False):