Projet

Général

Profil

0001-plone_restapi-allow-to-pass-simple-list-to-plone-647.patch

Nicolas Roche, 02 mai 2022 18:37

Télécharger (3,31 ko)

Voir les différences:

Subject: [PATCH] plone_restapi: allow to pass simple list to plone (#64760)

 passerelle/apps/plone_restapi/models.py | 11 +++++++----
 tests/test_plone_restapi.py             |  2 ++
 2 files changed, 9 insertions(+), 4 deletions(-)
passerelle/apps/plone_restapi/models.py
114 114
    def remove_unvaluated_dict_from_list(self, payload):
115 115
        """Remove from lists, not empty dicts having all empty string values"""
116 116

  
117 117
        data = {}
118 118
        for field_key, field_value in payload.items():
119 119
            if isinstance(field_value, list):
120 120
                data_list = []
121 121
                for dict_value in field_value:
122
                    for value in dict_value.values():
123
                        if value or value is False:
124
                            data_list.append(dict_value)
125
                            break
122
                    if not isinstance(dict_value, dict):
123
                        data_list = field_value
124
                    else:
125
                        for value in dict_value.values():
126
                            if value or value is False:
127
                                data_list.append(dict_value)
128
                                break
126 129
                data[field_key] = data_list
127 130
            else:
128 131
                data[field_key] = field_value
129 132

  
130 133
        return data
131 134

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