Projet

Général

Profil

0002-toulouse-maelis-sort-referentials-on-text-and-id-709.patch

Nicolas Roche (absent jusqu'au 3 avril), 08 décembre 2022 17:28

Télécharger (11,3 ko)

Voir les différences:

Subject: [PATCH 2/7] toulouse-maelis: sort referentials on text and id
 (#70982)

 passerelle/contrib/toulouse_maelis/models.py |  8 ++-
 tests/test_toulouse_maelis.py                | 58 ++++++++++----------
 2 files changed, 35 insertions(+), 31 deletions(-)
passerelle/contrib/toulouse_maelis/models.py
8 8
# This program is distributed in the hope that it will be useful,
9 9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10 10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 11
# GNU Affero General Public License for more details.
12 12
#
13 13
# You should have received a copy of the GNU Affero General Public License
14 14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 15

  
16
from operator import itemgetter
16 17
from urllib.parse import urljoin
17 18

  
18 19
import zeep
19 20
from django.core.cache import cache
20 21
from django.db import models
21 22
from zeep.helpers import serialize_object
22 23
from zeep.wsse.username import UsernameToken
23 24

  
......
71 72
        assert self.call('Invoice', 'isWSRunning')
72 73

  
73 74
    def get_referential(self, referential_name):
74 75

  
75 76
        # local referentials
76 77
        if referential_name == 'Complement':
77 78
            response = [
78 79
                {'id': 'B', 'text': 'bis'},
79
                {'id': 'T', 'text': 'ter'},
80 80
                {'id': 'Q', 'text': 'quater'},
81
                {'id': 'T', 'text': 'ter'},
81 82
            ]
82 83
            return {'list': response, 'dict': {x['id']: x['text'] for x in response}}
83 84
        elif referential_name == 'Sex':
84 85
            response = [
85
                {'id': 'M', 'text': 'Masculin'},
86 86
                {'id': 'F', 'text': 'Féminin'},
87
                {'id': 'M', 'text': 'Masculin'},
87 88
            ]
88 89
            return {'list': response, 'dict': {x['id']: x['text'] for x in response}}
89 90

  
90 91
        # remote referentials
91 92
        cache_key = 'maelis-%s-%s' % (self.pk, referential_name)
92 93
        data = cache.get(cache_key)
93 94
        if data is None:
94 95
            response = self.call('Family', 'read' + referential_name + 'List')
......
101 102
                data_list = []
102 103
                for item in serialize_object(response):
103 104
                    item['id'] = item['code']
104 105
                    item['text'] = item['label']
105 106
                    data_list.append(item)
106 107
            else:
107 108
                data_list = [{'id': x.code, 'text': x.libelle} for x in response]
108 109

  
110
            # sort list on text
111
            data_list.sort(key=itemgetter('text', 'id'))
112

  
109 113
            # remove redundant codes from list
110 114
            uniq_text = set()
111 115
            uniq_data = []
112 116
            for item in data_list:
113 117
                item['text'] = item['text'].strip()
114 118
                if item['text'] not in uniq_text:
115 119
                    uniq_data.append(item)
116 120
                    uniq_text.add(item['text'])
tests/test_toulouse_maelis.py
324 324
    mocked_post.return_value = READ_COUNTRY
325 325
    url = get_endpoint('read-country-list')
326 326

  
327 327
    resp = app.get(url)
328 328
    assert resp.json['err'] == 0
329 329
    assert len(resp.json['data']) == 3
330 330
    assert resp.json['data'] == [
331 331
        {'id': '212', 'text': 'AFGHANISTAN'},
332
        {'id': '99303', 'text': 'AFRIQUE DU SUD'},
333
        {'id': '99125', 'text': 'ALBANIE'},
332
        {'id': '303', 'text': 'AFRIQUE DU SUD'},
333
        {'id': '125', 'text': 'ALBANIE'},
334 334
    ]
335 335

  
336 336

  
337 337
@mock.patch('passerelle.utils.Request.get')
338 338
@mock.patch('passerelle.utils.Request.post')
339 339
def test_read_child_indicator_list(mocked_post, mocked_get, con, app):
340 340
    mocked_get.return_value = FAMILY_SERVICE_WSDL
341 341
    mocked_post.return_value = READ_CHILD_INDICATOR
342 342
    url = get_endpoint('read-child-indicator-list')
343 343

  
344 344
    resp = app.get(url)
345 345
    assert resp.json['err'] == 0
346 346
    assert len(resp.json['data']) == 8
347 347
    assert resp.json['data'][0:2] == [
348
        {
349
            'id': 'APPDENTAIRE',
350
            'text': 'Port appareil dentaire',
351
            'choiceList': [],
352
            'code': 'APPDENTAIRE',
353
            'label': 'Port appareil dentaire',
354
            'typeDesc': 'NONE',
355
        },
356 348
        {
357 349
            'id': 'AUTRE',
358 350
            'text': 'Autre',
359 351
            'choiceList': [],
360 352
            'code': 'AUTRE',
361 353
            'label': 'Autre',
362 354
            'typeDesc': 'NOTE',
363 355
        },
356
        {
357
            'id': 'AVL',
358
            'text': 'Auxiliaire de Vie loisirs',
359
            'choiceList': [],
360
            'code': 'AVL',
361
            'label': 'Auxiliaire de Vie loisirs',
362
            'typeDesc': 'NONE',
363
        },
364 364
    ]
365 365

  
366 366

  
367 367
@mock.patch('passerelle.utils.Request.get')
368 368
@mock.patch('passerelle.utils.Request.post')
369 369
def test_read_civility_list(mocked_post, mocked_get, con, app):
370 370
    mocked_get.return_value = FAMILY_SERVICE_WSDL
371 371
    mocked_post.return_value = READ_CIVILITIES
......
381 381

  
382 382
def test_read_complement_list(con, app):
383 383
    url = get_endpoint('read-complement-list')
384 384

  
385 385
    resp = app.get(url)
386 386
    assert resp.json['err'] == 0
387 387
    assert resp.json['data'] == [
388 388
        {'id': 'B', 'text': 'bis'},
389
        {'id': 'T', 'text': 'ter'},
390 389
        {'id': 'Q', 'text': 'quater'},
390
        {'id': 'T', 'text': 'ter'},
391 391
    ]
392 392

  
393 393

  
394 394
@mock.patch('passerelle.utils.Request.get')
395 395
@mock.patch('passerelle.utils.Request.post')
396 396
def test_read_csp_list(mocked_post, mocked_get, con, app):
397 397
    mocked_get.return_value = FAMILY_SERVICE_WSDL
398 398
    mocked_post.return_value = READ_CSP
399 399
    url = get_endpoint('read-csp-list')
400 400

  
401 401
    resp = app.get(url)
402 402
    assert resp.json['err'] == 0
403 403
    assert resp.json['data'] == [
404 404
        {'id': '14', 'text': 'AGENT DE MAITRISE'},
405 405
        {'id': '1', 'text': 'AGRICULTEUR'},
406
        {'id': 'ARTI', 'text': 'ARTISAN'},
406
        {'id': 'ART', 'text': 'ARTISAN'},
407 407
        {'id': '2', 'text': 'ARTISAN-COMMERCANT'},
408 408
        {'id': '15', 'text': 'AUTRES'},
409
        {'id': 'CADR', 'text': 'CADRE'},
409
        {'id': '4', 'text': 'CADRE'},
410 410
        {'id': '13', 'text': 'CADRE SUPERIEUR'},
411 411
        {'id': '3', 'text': "CHEF D'ENTREPRISE"},
412 412
        {'id': 'CHOM', 'text': 'CHOMEUR'},
413
        {'id': 'COM', 'text': 'COMMERCANT'},
413
        {'id': '7', 'text': 'COMMERCANT'},
414 414
        {'id': '10', 'text': "DEMANDEUR D'EMPLOI"},
415 415
        {'id': 'DIV', 'text': 'DIVERS'},
416
        {'id': 'EMP', 'text': 'EMPLOYE'},
417
        {'id': 'ENS', 'text': 'ENSEIGNANT'},
418
        {'id': 'ETU', 'text': 'ETUDIANT'},
416
        {'id': '5', 'text': 'EMPLOYE'},
417
        {'id': '17', 'text': 'ENSEIGNANT'},
418
        {'id': '8', 'text': 'ETUDIANT'},
419 419
        {'id': '11', 'text': 'FONCTIONNAIRE'},
420 420
        {'id': 'MAIR', 'text': 'MAIRIE DE NICE'},
421
        {'id': 'OUV', 'text': 'OUVRIER'},
421
        {'id': '6', 'text': 'OUVRIER'},
422 422
        {'id': 'PERENS', 'text': 'PERISCO ENSEIGNANT'},
423 423
        {'id': 'PEREXT', 'text': 'PERISCO EXTERNE'},
424 424
        {'id': 'PERMAI', 'text': 'PERISCO MAIRIE DE NICE'},
425 425
        {'id': 'PERANI', 'text': 'PERISCO S.ANIMATION'},
426 426
        {'id': '9', 'text': 'PROFESSION LIBERALE'},
427 427
        {'id': '12', 'text': 'RETRAITE'},
428 428
        {'id': 'RMI', 'text': "REVENU MINIMUM D'INSERTION"},
429 429
        {'id': '16', 'text': 'SANS PROFESSION'},
......
436 436
def test_read_dietcode_list(mocked_post, mocked_get, con, app):
437 437
    mocked_get.return_value = FAMILY_SERVICE_WSDL
438 438
    mocked_post.return_value = READ_DIETCODE
439 439
    url = get_endpoint('read-dietcode-list')
440 440

  
441 441
    resp = app.get(url)
442 442
    assert resp.json['err'] == 0
443 443
    assert resp.json['data'] == [
444
        {'id': 'BB', 'text': 'REPAS BEBE'},
445
        {'id': 'MSP', 'text': 'REPAS MOYEN SANS PORC'},
446
        {'id': 'MSV', 'text': 'REPAS MOYEN SANS VIANDE'},
447
        {'id': 'MST', 'text': 'REPAS MOYEN STANDARD'},
448 444
        {'id': 'STD', 'text': '1- REPAS STANDARD'},
449 445
        {'id': 'RSP', 'text': '2- RÉGIME SANS PORC'},
450 446
        {'id': 'RSV', 'text': '3- RÉGIME SANS VIANDE'},
451 447
        {'id': 'PAI', 'text': "4- PROTOCOLE D'ACCUEIL INDIVIDUALISÉ"},
448
        {'id': 'BB', 'text': 'REPAS BEBE'},
449
        {'id': 'MSP', 'text': 'REPAS MOYEN SANS PORC'},
450
        {'id': 'MSV', 'text': 'REPAS MOYEN SANS VIANDE'},
451
        {'id': 'MST', 'text': 'REPAS MOYEN STANDARD'},
452 452
    ]
453 453

  
454 454

  
455 455
@mock.patch('passerelle.utils.Request.get')
456 456
@mock.patch('passerelle.utils.Request.post')
457 457
def test_read_organ_list(mocked_post, mocked_get, con, app):
458 458
    mocked_get.return_value = FAMILY_SERVICE_WSDL
459 459
    mocked_post.return_value = READ_ORGAN
460 460
    url = get_endpoint('read-organ-list')
461 461

  
462 462
    resp = app.get(url)
463 463
    assert resp.json['err'] == 0
464 464
    assert len(resp.json['data']) == 92
465 465
    assert resp.json['data'][:5] == [
466
        {'id': 'A10004460232', 'text': 'LEVENS'},
467
        {'id': 'A10007752822', 'text': 'LA COLLE SUR LOUP'},
468
        {'id': 'A10001133770', 'text': 'ASSIM'},
469
        {'id': 'A10007751483', 'text': 'EZE'},
470
        {'id': 'A10008152785', 'text': 'LUCERAM'},
466
        {'id': 'A10000979374', 'text': 'ALC L OLIVIER'},
467
        {'id': 'A10000979372', 'text': 'ALVA 06'},
468
        {'id': 'A10000980566', 'text': 'ANTIBES'},
469
        {'id': 'A10000980388', 'text': 'APAJH'},
470
        {'id': 'A10016401771', 'text': 'ASCROS'},
471 471
    ]
472 472

  
473 473

  
474 474
@mock.patch('passerelle.utils.Request.get')
475 475
@mock.patch('passerelle.utils.Request.post')
476 476
def test_read_pai_list(mocked_post, mocked_get, con, app):
477 477
    mocked_get.return_value = FAMILY_SERVICE_WSDL
478 478
    mocked_post.return_value = READ_PAI
......
568 568

  
569 569

  
570 570
def test_read_sex_list(con, app):
571 571
    url = get_endpoint('read-sex-list')
572 572

  
573 573
    resp = app.get(url)
574 574
    assert resp.json['err'] == 0
575 575
    assert resp.json['data'] == [
576
        {'id': 'M', 'text': 'Masculin'},
577 576
        {'id': 'F', 'text': 'Féminin'},
577
        {'id': 'M', 'text': 'Masculin'},
578 578
    ]
579 579

  
580 580

  
581 581
@mock.patch('passerelle.utils.Request.get')
582 582
@mock.patch('passerelle.utils.Request.post')
583 583
def test_read_situation_list(mocked_post, mocked_get, con, app):
584 584
    mocked_get.return_value = FAMILY_SERVICE_WSDL
585 585
    mocked_post.return_value = READ_SITUATIONS
......
606 606
    mocked_get.return_value = FAMILY_SERVICE_WSDL
607 607
    mocked_post.return_value = READ_STREET
608 608
    url = get_endpoint('read-street-list')
609 609

  
610 610
    resp = app.get(url)
611 611
    assert resp.json['err'] == 0
612 612
    assert len(resp.json['data']) == 3
613 613
    assert resp.json['data'] == [
614
        {'id': 'AS0264', 'text': 'RUE SAINT FRANCOIS DE PAULE'},
615 614
        {'id': 'AP0594', 'text': 'AVENUE PAULIANI'},
615
        {'id': 'AS0264', 'text': 'RUE SAINT FRANCOIS DE PAULE'},
616 616
        {'id': 'AM0330', 'text': 'TRAVERSE DES MARAICHERS'},
617 617
    ]
618 618

  
619 619

  
620 620
@mock.patch('passerelle.utils.Request.get')
621 621
@mock.patch('passerelle.utils.Request.post')
622 622
def test_read_vaccin_list(mocked_post, mocked_get, con, app):
623 623
    mocked_get.return_value = FAMILY_SERVICE_WSDL
624
-