Projet

Général

Profil

0001-workflow-fix-get_submitter_email-with-blockdef-50871.patch

Lauréline Guérin, 12 avril 2021 14:43

Télécharger (4,86 ko)

Voir les différences:

Subject: [PATCH] workflow: fix get_submitter_email with blockdef (#50871)

 tests/test_formdata.py | 46 ++++++++++++++++++++++++++++++++++++++++-
 wcs/formdef.py         | 47 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 84 insertions(+), 9 deletions(-)
tests/test_formdata.py
217 217

  
218 218

  
219 219
def test_get_submitter(pub, formdef):
220
    BlockDef.wipe()
221
    block = BlockDef()
222
    block.name = 'foobar'
223
    block.fields = [
224
        fields.StringField(id='0', label='email', varname='foo', prefill={'type': 'user', 'value': 'email'})
225
    ]
226
    block.store()
220 227
    formdef.data_class().wipe()
221 228
    formdef.fields = [
222
        fields.StringField(id='0', label='email', varname='foo', prefill={'type': 'user', 'value': 'email'})
229
        fields.StringField(id='0', label='email', varname='foo', prefill={'type': 'user', 'value': 'email'}),
230
        fields.BlockField(id='1', label='test', type='block:foobar', max_items=3, varname='block'),
223 231
    ]
224 232
    formdef.store()
225 233

  
......
228 236
    formdata.data = {'0': 'foo@localhost'}
229 237
    assert formdef.get_submitter_email(formdata) == 'foo@localhost'
230 238

  
239
    formdata.data = {
240
        '1': {
241
            'data': [{'0': 'baz@localhost'}, {'0': ''}],
242
            'schema': {'0': 'string'},
243
        }
244
    }
245
    assert formdef.get_submitter_email(formdata) == 'baz@localhost'
246

  
247
    formdata.data = {
248
        '1': {
249
            'data': [{'0': 'baz@localhost'}, {'0': 'foo@localhost'}],
250
            'schema': {'0': 'string'},
251
        }
252
    }
253
    assert formdef.get_submitter_email(formdata) == 'baz@localhost'
254

  
255
    formdata.data = {
256
        '1': {
257
            'data': [{'0': ''}, {'0': 'foo@localhost'}],
258
            'schema': {'0': 'string'},
259
        }
260
    }
261
    assert formdef.get_submitter_email(formdata) == 'foo@localhost'
262

  
263
    formdata.data = {'1': {}}
264
    assert formdef.get_submitter_email(formdata) is None
265

  
266
    formdata.data = {
267
        '0': 'foo@localhost',
268
        '1': {
269
            'data': [{'0': 'baz@localhost'}, {'0': ''}],
270
            'schema': {'0': 'string'},
271
        },
272
    }
273
    assert formdef.get_submitter_email(formdata) == 'foo@localhost'
274

  
231 275
    user = pub.user_class()
232 276
    user.email = 'bar@localhost'
233 277
    user.store()
wcs/formdef.py
1324 1324

  
1325 1325
        # look up in submitted form for one that would hold the user
1326 1326
        # email (the one set to be prefilled by user email)
1327

  
1328
        def get_all_fields():
1329
            for field in formdata.formdef.fields:
1330
                yield field
1331
                if field.key == 'block':
1332
                    for subfield in field.block.fields:
1333
                        yield subfield
1334

  
1335
        def is_user_field(field):
1336
            if not getattr(field, 'prefill', None):
1337
                return False
1338
            if field.prefill.get('type') != 'user':
1339
                return False
1340
            if field.prefill.get('value') != field_email:
1341
                return False
1342
            return True
1343

  
1327 1344
        if formdata.data:
1328
            fields = formdata.formdef.fields
1329
            for field in fields:
1330
                if not hasattr(field, 'prefill'):
1345
            # check first in "normal" fields
1346
            for field in formdata.formdef.fields:
1347
                if not is_user_field(field):
1331 1348
                    continue
1332
                if field.prefill and field.prefill.get('type') == 'user':
1333
                    if field.prefill.get('value') == field_email:
1334
                        v = formdata.data.get(field.id)
1335
                        if v:
1336
                            return v
1349

  
1350
                v = formdata.data.get(field.id)
1351
                if v:
1352
                    return v
1353

  
1354
            # then check in block fields
1355
            for field in formdata.formdef.fields:
1356
                if field.key != 'block':
1357
                    continue
1358
                for subfield in field.block.fields:
1359
                    if not is_user_field(subfield):
1360
                        continue
1361
                    v = formdata.data.get(field.id)
1362
                    if not v.get('data'):
1363
                        continue
1364
                    for data in v.get('data'):
1365
                        w = data.get(subfield.id)
1366
                        if w:
1367
                            return w
1337 1368

  
1338 1369
        # if nothing was found, get email from user profile
1339 1370
        if formdata.user and formdata.user.email and formdata.user.is_active:
1340
-