Projet

Général

Profil

0004-plone_restapi-add-endpoint-to-get-field-choices-5790.patch

Nicolas Roche, 16 octobre 2021 22:45

Télécharger (5,21 ko)

Voir les différences:

Subject: [PATCH 4/5] plone_restapi: add endpoint to get field choices (#57904)

 passerelle/apps/plone_restapi/models.py       | 14 ++++
 .../data/plone_restapi/get_field_choices.json | 74 +++++++++++++++++++
 tests/test_plone_restapi.py                   | 13 ++++
 3 files changed, 101 insertions(+)
 create mode 100644 tests/data/plone_restapi/get_field_choices.json
passerelle/apps/plone_restapi/models.py
220 220
            'id': {'description': _('Content type identifier'), 'example_value': 'imio.directory.Contact'}
221 221
        },
222 222
        display_order=2,
223 223
    )
224 224
    def get_content_type(self, request, id):
225 225
        response = self.request(uri='@types', uid=id, method='GET')
226 226
        return {'data': response}
227 227

  
228
    @endpoint(
229
        perm='can_access',
230
        description=_('Get field choices'),
231
        parameters={
232
            'id': {'description': _('Field identifier'), 'example_value': 'imio.smartweb.vocabulary.Topics'}
233
        },
234
        display_order=3,
235
    )
236
    def get_field_choices(self, request, id):
237
        response = self.request(uri='@vocabularies', uid=id, method='GET')
238
        for record in response.get('items') or []:
239
            self.adapt_record(record, '{{ title }}', id_key='token')
240
        return {'data': response.get('items') or []}
241

  
228 242
    @endpoint(
229 243
        perm='can_access',
230 244
        description=_('Fetch'),
231 245
        parameters={
232 246
            'uri': {'description': _('Uri')},
233 247
            'uid': {'description': _('Uid')},
234 248
            'text_template': {'description': _('Text template')},
235 249
        },
tests/data/plone_restapi/get_field_choices.json
1
{
2
  "@id": "https://annuaire.preprod.imio.be/@vocabularies/imio.smartweb.vocabulary.Topics",
3
  "items": [
4
    {
5
      "title": "Activit\u00e9s et divertissement",
6
      "token": "entertainment"
7
    },
8
    {
9
      "title": "Agriculture",
10
      "token": "agriculture"
11
    },
12
    {
13
      "title": "Citoyennet\u00e9",
14
      "token": "citizenship"
15
    },
16
    {
17
      "title": "Culture",
18
      "token": "culture"
19
    },
20
    {
21
      "title": "\u00c9conomie",
22
      "token": "economics"
23
    },
24
    {
25
      "title": "\u00c9ducation",
26
      "token": "education"
27
    },
28
    {
29
      "title": "Environnement",
30
      "token": "environment"
31
    },
32
    {
33
      "title": "Habitat et urbanisme",
34
      "token": "habitat_town_planning"
35
    },
36
    {
37
      "title": "Mobilit\u00e9",
38
      "token": "mobility"
39
    },
40
    {
41
      "title": "Participation citoyenne",
42
      "token": "citizen_participation"
43
    },
44
    {
45
      "title": "Politique",
46
      "token": "politics"
47
    },
48
    {
49
      "title": "Sant\u00e9",
50
      "token": "health"
51
    },
52
    {
53
      "title": "S\u00e9curit\u00e9 et pr\u00e9vention",
54
      "token": "safety_prevention"
55
    },
56
    {
57
      "title": "Social",
58
      "token": "social"
59
    },
60
    {
61
      "title": "Sports",
62
      "token": "sports"
63
    },
64
    {
65
      "title": "Territoire et espace public",
66
      "token": "territory_public_space"
67
    },
68
    {
69
      "title": "Tourisme",
70
      "token": "tourism"
71
    }
72
  ],
73
  "items_total": 17
74
}
tests/test_plone_restapi.py
308 308
    assert resp.json['data']['required'] == ['title', 'type']
309 309
    assert len(resp.json['data']['properties']) == 28
310 310
    assert (
311 311
        resp.json['data']['properties']['topics']['items']['vocabulary']['@id']
312 312
        == 'https://annuaire.preprod.imio.be/@vocabularies/imio.smartweb.vocabulary.Topics'
313 313
    )
314 314

  
315 315

  
316
def test_get_field_choices(app, connector, token):
317
    endpoint = utils.generic_endpoint_url('plone-restapi', 'get_field_choices', slug=connector.slug)
318
    assert endpoint == '/plone-restapi/my_connector/get_field_choices'
319
    url = connector.service_url + '/@vocabularies/imio.smartweb.vocabulary.Topics'
320
    params = {'id': 'imio.smartweb.vocabulary.Topics'}
321
    with utils.mock_url(url=url, response=json_get_data('get_field_choices')):
322
        resp = app.get(endpoint, params=params)
323
    assert not resp.json['err']
324
    assert len(resp.json['data']) == 17
325
    assert resp.json['data'][16]['id'] == 'tourism'
326
    assert resp.json['data'][16]['text'] == 'Tourisme'
327

  
328

  
316 329
def test_create(app, connector, token):
317 330
    endpoint = utils.generic_endpoint_url('plone-restapi', 'create', slug=connector.slug)
318 331
    assert endpoint == '/plone-restapi/my_connector/create'
319 332
    url = connector.service_url + '/braine-l-alleud'
320 333
    payload = {
321 334
        '@type': 'imio.directory.Contact',
322 335
        'title': "Test Entr'ouvert",
323 336
        'type': 'organization',
324
-