Projet

Général

Profil

0002-plone_restapi-adapt-list-content-sent-to-Plone-62943.patch

Nicolas Roche, 28 mars 2022 14:27

Télécharger (5,49 ko)

Voir les différences:

Subject: [PATCH 2/2] plone_restapi: adapt list content sent to Plone (#62943)

 passerelle/apps/plone_restapi/models.py | 26 +++++++++++++++++++++++++
 tests/test_plone_restapi.py             | 11 +++++++++++
 2 files changed, 37 insertions(+)
passerelle/apps/plone_restapi/models.py
106 106
    def convert_image_format(self, payload):
107 107
        for file_field in payload.values():
108 108
            if isinstance(file_field, dict) and file_field.get('filename'):
109 109
                file_field['encoding'] = 'base64'
110 110
                file_field['data'] = file_field['content']
111 111
                file_field['content-type'] = file_field['content_type']
112 112
                del file_field['content']
113 113

  
114
    def remove_unvaluated_dict_from_list(self, payload):
115
        """Remove from lists, not empty dicts having all empty string values"""
116

  
117
        def is_not_empty_dict_having_all_empty_values(data):
118
            if not isinstance(data, dict):
119
                return False
120
            if len(data) == 0:
121
                return False
122
            for value in data.values():
123
                if value != '':
124
                    return False
125
            return True
126

  
127
        data = {}
128
        for field_key, field_value in payload.items():
129
            if isinstance(field_value, list):
130
                data_list = []
131
                for dict_value in field_value:
132
                    if not is_not_empty_dict_having_all_empty_values(dict_value):
133
                        data_list.append(dict_value)
134
                data[field_key] = data_list
135
            else:
136
                data[field_key] = field_value
137
        return data
138

  
114 139
    def adapt_payload(self, payload):
115 140
        self.convert_image_format(payload)
141
        payload = self.remove_unvaluated_dict_from_list(payload)
116 142
        return payload
117 143

  
118 144
    def adapt_record(
119 145
        self,
120 146
        record,
121 147
        text_template='{{ id }}',
122 148
        id_key='UID',
123 149
    ):
tests/test_plone_restapi.py
391 391
    payload = {
392 392
        '@type': 'imio.directory.Contact',
393 393
        'title': "Test Entr'ouvert",
394 394
        'type': 'organization',
395 395
        'schedule': {},
396 396
        'topics/0/title': 'Tourisme',
397 397
        'topics/0/token': 'tourism',
398 398
        'image': {'filename': 'foo.jpg', 'content_type': 'image/jpeg', 'content': '...'},
399
        'phones': [
400
            {'label': 'numéro principal', 'number': '0123456789', 'type': 'work'},
401
            {'label': '', 'number': '', 'type': ''},
402
        ],
399 403
    }
400 404
    with utils.mock_url(url=url, response=json_get_data('fetch'), status_code=201) as mocked:
401 405
        resp = app.post_json(endpoint + '?uri=braine-l-alleud&publish=false', params=payload)
402 406
        body = json.loads(mocked.handlers[0].call['requests'][1].body)
403 407
        assert body['topics'] == [{'title': 'Tourisme', 'token': 'tourism'}]
404 408
        assert body['image'] == {
405 409
            'filename': 'foo.jpg',
406 410
            'content_type': 'image/jpeg',
407 411
            'encoding': 'base64',
408 412
            'data': '...',
409 413
            'content-type': 'image/jpeg',
410 414
        }
415
        assert body['phones'] == [{'label': 'numéro principal', 'number': '0123456789', 'type': 'work'}]
411 416
    assert not resp.json['err']
412 417
    assert resp.json['data'] == {
413 418
        'uid': 'dccd85d12cf54b6899dff41e5a56ee7f',
414 419
        'created': True,
415 420
        'review_state': None,
416 421
    }
417 422

  
418 423

  
......
466 471
    endpoint = utils.generic_endpoint_url('plone-restapi', 'update', slug=connector.slug)
467 472
    assert endpoint == '/plone-restapi/my_connector/update'
468 473
    url = connector.service_url + '/braine-l-alleud/dccd85d12cf54b6899dff41e5a56ee7f'
469 474
    query_string = '?uri=braine-l-alleud&uid=dccd85d12cf54b6899dff41e5a56ee7f'
470 475
    payload = {
471 476
        'title': 'Test update',
472 477
        'topics/0/token': 'social',
473 478
        'image': {'filename': 'foo.jpg', 'content_type': 'image/jpeg', 'content': '...'},
479
        'phones': [
480
            {'label': '', 'number': '', 'type': ''},
481
            {'label': '', 'number': '', 'type': ''},
482
        ],
474 483
    }
475 484
    with utils.mock_url(url=url, response='', status_code=204) as mocked:
476 485
        resp = app.post_json(endpoint + query_string, params=payload)
477 486
        body = json.loads(mocked.handlers[0].call['requests'][1].body)
478 487
        assert body['topics'] == [{'token': 'social'}]
479 488
        assert body['image'] == {
480 489
            'filename': 'foo.jpg',
481 490
            'content_type': 'image/jpeg',
482 491
            'encoding': 'base64',
483 492
            'data': '...',
484 493
            'content-type': 'image/jpeg',
485 494
        }
495
        assert body['phones'] == []
496

  
486 497
    assert not resp.json['err']
487 498
    assert resp.json['data'] == {'uid': 'dccd85d12cf54b6899dff41e5a56ee7f', 'updated': True}
488 499

  
489 500

  
490 501
def test_update_wrong_payload(app, connector, token):
491 502
    endpoint = utils.generic_endpoint_url('plone-restapi', 'update', slug=connector.slug)
492 503
    assert endpoint == '/plone-restapi/my_connector/update'
493 504
    url = connector.service_url + '/braine-l-alleud/dccd85d12cf54b6899dff41e5a56ee7f'
494
-