Projet

Général

Profil

0001-toulouse_axel-add-labels-to-explain-some-values-3929.patch

Lauréline Guérin, 03 février 2020 10:14

Télécharger (12,2 ko)

Voir les différences:

Subject: [PATCH] toulouse_axel: add labels to explain some values (#39291)

 functests/toulouse_axel/test_toulouse_axel.py |  8 ++-
 passerelle/contrib/toulouse_axel/models.py    | 42 ++++++++++--
 passerelle/contrib/toulouse_axel/utils.py     | 64 +++++++++++++++++++
 tests/data/toulouse_axel/family_info.xml      |  4 +-
 tests/test_toulouse_axel.py                   | 41 ++++++++++++
 5 files changed, 149 insertions(+), 10 deletions(-)
 create mode 100644 passerelle/contrib/toulouse_axel/utils.py
functests/toulouse_axel/test_toulouse_axel.py
45 45
    payload['DROITALIMAGE'] = 'NON'
46 46
    payload['REVENUS']['CHOIXREVENU'] = ''
47 47
    # remove non editable fields
48
    for key in ['SITUATIONFAMILIALE', 'NBENFANTACTIF', 'NBRLACTIF', 'IDDUI', 'CODEMISEAJOUR']:
48
    for key in ['SITUATIONFAMILIALE', 'SITUATIONFAMILIALE_label', 'NBENFANTACTIF', 'NBRLACTIF', 'IDDUI', 'CODEMISEAJOUR']:
49 49
        payload.pop(key)
50
    for key in ['IDPERSONNE', 'NOM', 'PRENOM', 'NOMJEUNEFILLE', 'DATENAISSANCE', 'CIVILITE', 'INDICATEURRL']:
50
    for key in ['IDPERSONNE', 'NOM', 'PRENOM', 'NOMJEUNEFILLE', 'DATENAISSANCE', 'CIVILITE', 'INDICATEURRL', 'CSP_label']:
51 51
        if 'RL1' in payload:
52 52
            payload['RL1'].pop(key)
53 53
        if 'RL2' in payload:
54 54
            payload['RL2'].pop(key)
55
    for key in ['MONTANTTOTAL', 'DATEVALIDITE', 'SFI', 'IREVENUS', 'RNF', 'NBENFANTSACHARGE']:
55
    for key in ['MONTANTTOTAL', 'DATEVALIDITE', 'SFI', 'IREVENUS', 'RNF', 'NBENFANTSACHARGE', 'TYPEREGIME_label']:
56 56
        payload['REVENUS'].pop(key, None)
57 57
    for enfant in payload['ENFANT']:
58 58
        for key in ['NOM', 'DATENAISSANCE', 'SEXE', 'PRENOMPERE', 'PRENOMMERE', 'NOMPERE', 'NOMMERE', 'RATTACHEAUTREDUI', 'PRENOM']:
59 59
            enfant.pop(key)
60 60
        enfant['AUTORISATIONURGENCEMEDICALE'] = 'OUI'
61
        for contact in enfant.get('CONTACT', []):
62
            contact.pop('LIENPARENTE_label')
61 63
        if 'SANITAIRE' not in enfant:
62 64
            continue
63 65
        # manage handicap data (not the same schema)
passerelle/contrib/toulouse_axel/models.py
36 36
from passerelle.utils.api import endpoint
37 37
from passerelle.utils.jsonresponse import APIError
38 38
from passerelle.utils.xml import JSONSchemaFromXMLSchema
39
from . import utils
39 40

  
40 41
logger = logging.getLogger('passerelle.contrib.toulouse_axel')
41 42

  
......
520 521
        link.delete()
521 522
        return {'link': link_id, 'deleted': True, 'dui': link.dui}
522 523

  
524
    @endpoint(
525
        description=_("Get a referential"),
526
        perm='can_access',
527
        parameters={
528
            'code': {'description': _('Referential code. Possibile values: situation_familiale, csp, lien_parente, type_regime')},
529
        })
530
    def referential(self, request, code):
531
        if code not in ['situation_familiale', 'csp', 'lien_parente', 'type_regime']:
532
            raise APIError('Referential not found', err_code='not-found')
533
        print(getattr(utils, '{}_mapping'.format(code)))
534
        return {'data': getattr(utils, '{}_mapping'.format(code))}
535

  
523 536
    def get_family_data(self, name_id):
524 537
        link = self.get_link(name_id)
525 538
        try:
......
540 553
        })
541 554
    def family_info(self, request, NameID):
542 555
        family_data = self.get_family_data(NameID)
556
        family_data['SITUATIONFAMILIALE_label'] = utils.get_label(utils.situation_familiale_mapping, family_data['SITUATIONFAMILIALE'])
557
        for key in ['RL1', 'RL2']:
558
            if key not in family_data:
559
                continue
560
            rl = family_data[key]
561
            rl['CSP_label'] = utils.get_label(utils.csp_mapping, rl['CSP'])
562
        for child in family_data.get('ENFANT', []):
563
            for contact in child.get('CONTACT', []):
564
                contact['LIENPARENTE_label'] = utils.get_label(utils.lien_parente_mapping, contact['LIENPARENTE'])
565
        if 'REVENUS' in family_data:
566
            family_data['REVENUS']['TYPEREGIME_label'] = utils.get_label(utils.type_regime_mapping, family_data['REVENUS']['TYPEREGIME'])
543 567
        return {'data': family_data}
544 568

  
569
    def get_child_data(self, name_id, child_id):
570
        family_data = self.get_family_data(name_id)
571

  
572
        for child in family_data['ENFANT']:
573
            if child['IDPERSONNE'] == child_id:
574
                return child
575

  
576
        raise APIError('Child not found', err_code='not-found')
577

  
545 578
    @endpoint(
546 579
        description=_("Get information about a child"),
547 580
        perm='can_access',
......
550 583
            'idpersonne': {'description': _('Child ID')},
551 584
        })
552 585
    def child_info(self, request, idpersonne, NameID):
553
        family_data = self.get_family_data(NameID)
586
        child_data = self.get_child_data(NameID, idpersonne)
554 587

  
555
        for child in family_data['ENFANT']:
556
            if child['IDPERSONNE'] == idpersonne:
557
                return {'data': child}
588
        for contact in child_data.get('CONTACT', []):
589
            contact['LIENPARENTE_label'] = utils.get_label(utils.lien_parente_mapping, contact['LIENPARENTE'])
558 590

  
559
        raise APIError('Child not found', err_code='not-found')
591
        return {'data': child_data}
560 592

  
561 593
    def pre_sanitize_update_family_data(self, post_data):
562 594
        # before json payload validation, check maj fields and remove empty blocks
passerelle/contrib/toulouse_axel/utils.py
1
# -*- coding: utf-8 -*-
2
# passerelle - uniform access to multiple data sources and services
3
# Copyright (C) 2020  Entr'ouvert
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License as published
7
# by the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17

  
18
from collections import OrderedDict
19

  
20

  
21
situation_familiale_mapping = OrderedDict([
22
    ('C', u'Célibataire'),
23
    ('D', u'Divorcé (e)'),
24
    ('M', u'Marié (e)'),
25
    ('S', u'Séparé (e)'),
26
    ('V', 'Veuf (ve)'),
27
    ('VM', 'Vie Maritale'),
28
    ('P', 'Pacs'),
29
])
30

  
31
csp_mapping = OrderedDict([
32
    ('OUV', 'Ouvrier'),
33
    ('EMP', u'Employés'),
34
    ('ETU', 'Etudiant'),
35
    ('RET', u'Retraités'),
36
    ('STA', 'Personne en stage'),
37
    ('AGEX', 'Agriculteurs exploitants'),
38
    ('ARCO', 'Artisans commercants chefs entreprise'),
39
    ('CADP', u'Cadres professions intellectuelles supérieures'),
40
    ('PRIN', 'Professions intermediaires'),
41
    ('SACT', u'Autres personnes sans activité professionnelle'),
42
    ('REC', 'Recherche emploi'),
43
])
44

  
45
lien_parente_mapping = OrderedDict([
46
    ('GRP1', 'Grands-parents paternels'),
47
    ('GRP2', 'Grands-parents maternels'),
48
    ('VOI', 'Voisin'),
49
    ('FRE', "Frère de l'enfant"),
50
    ('SOE', "Soeur de l'enfant"),
51
    ('AMI', 'Ami (e)'),
52
    ('FAMI', 'Membre de la famille'),
53
    ('BABY', 'Baby sitter'),
54
])
55

  
56
type_regime_mapping = OrderedDict([
57
    ('GENE', u'Régime général'),
58
    ('ZAU', 'Autre'),
59
    ('MSA', 'MSA'),
60
])
61

  
62

  
63
def get_label(mapping, code):
64
    return mapping.get(code, code)
tests/data/toulouse_axel/family_info.xml
4 4
    <CODEMISEAJOUR>19</CODEMISEAJOUR>
5 5
    <NBRLACTIF>2</NBRLACTIF>
6 6
    <NBENFANTACTIF>2</NBENFANTACTIF>
7
    <SITUATIONFAMILIALE>P</SITUATIONFAMILIALE>
7
    <SITUATIONFAMILIALE>S</SITUATIONFAMILIALE>
8 8
    <REACTUALISATIONENLIGNE>NON</REACTUALISATIONENLIGNE>
9 9
    <DEMATFACTURES>NON</DEMATFACTURES>
10 10
    <ADRESSE>
......
131 131
      <CONTACT>
132 132
        <NOM>foo</NOM>
133 133
        <PRENOM>foo</PRENOM>
134
        <LIENPARENTE/>
134
        <LIENPARENTE>GRP1</LIENPARENTE>
135 135
        <ENCASURGENCE>OUI</ENCASURGENCE>
136 136
        <CHERCHERLENFANT>OUI</CHERCHERLENFANT>
137 137
        <TELFIXE>0505050505</TELFIXE>
tests/test_toulouse_axel.py
1
# -*- coding: utf-8 -*-
1 2
# passerelle - uniform access to multiple data sources and services
2 3
# Copyright (C) 2019 Entr'ouvert
3 4
#
......
41 42
    ref_facture_pdf,
42 43
    ref_verif_dui,
43 44
)
45
from passerelle.contrib.toulouse_axel.utils import (
46
    situation_familiale_mapping,
47
    csp_mapping,
48
    lien_parente_mapping,
49
    type_regime_mapping,
50
)
44 51
from passerelle.utils.jsonresponse import APIError
45 52
import utils
46 53

  
......
568 575
    assert resp.json['deleted'] is True
569 576

  
570 577

  
578
def test_refetential_endpoint_no_result(app, resource):
579
    resp = app.post('/toulouse-axel/test/referential?code=foo')
580
    assert resp.json['err_desc'] == "Referential not found"
581
    assert resp.json['err'] == 'not-found'
582

  
583

  
584
@pytest.mark.parametrize('value, expected', [
585
    ('situation_familiale', situation_familiale_mapping),
586
    ('csp', csp_mapping),
587
    ('lien_parente', lien_parente_mapping),
588
    ('type_regime', type_regime_mapping),
589
])
590
def test_refetential_endpoint(app, resource, value, expected):
591
    resp = app.post('/toulouse-axel/test/referential?code=%s' % value)
592
    assert resp.json['data'] == expected
593

  
594

  
571 595
def test_family_info_endpoint_axel_error(app, resource):
572 596
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
573 597
    with mock.patch('passerelle.contrib.toulouse_axel.models.ref_famille_dui') as operation:
......
604 628
        'RL1',
605 629
        'RL2',
606 630
        'SITUATIONFAMILIALE',
631
        'SITUATIONFAMILIALE_label',
607 632
        'TELFIXE',
608 633
    ])
634
    assert resp.json['data']['SITUATIONFAMILIALE'] == 'S'
635
    assert resp.json['data']['SITUATIONFAMILIALE_label'] == u'Séparé (e)'
636
    assert resp.json['data']['RL1']['CSP'] == 'ETU'
637
    assert resp.json['data']['RL1']['CSP_label'] == 'Etudiant'
638
    assert resp.json['data']['RL2']['CSP'] == 'EMP'
639
    assert resp.json['data']['RL2']['CSP_label'] == u'Employés'
640
    assert resp.json['data']['ENFANT'][0]['CONTACT'][0]['LIENPARENTE'] == 'GRP1'
641
    assert resp.json['data']['ENFANT'][0]['CONTACT'][0]['LIENPARENTE_label'] == 'Grands-parents paternels'
642
    assert resp.json['data']['ENFANT'][0]['CONTACT'][1]['LIENPARENTE'] is None
643
    assert resp.json['data']['ENFANT'][0]['CONTACT'][1]['LIENPARENTE_label'] is None
644
    assert resp.json['data']['REVENUS']['TYPEREGIME'] == 'GENE'
645
    assert resp.json['data']['REVENUS']['TYPEREGIME_label'] == u'Régime général'
609 646

  
610 647

  
611 648
def test_child_info_endpoint_axel_error(app, resource):
......
655 692
        'SANITAIRE',
656 693
        'SEXE',
657 694
    ])
695
    assert resp.json['data']['CONTACT'][0]['LIENPARENTE'] == 'GRP1'
696
    assert resp.json['data']['CONTACT'][0]['LIENPARENTE_label'] == 'Grands-parents paternels'
697
    assert resp.json['data']['CONTACT'][1]['LIENPARENTE'] is None
698
    assert resp.json['data']['CONTACT'][1]['LIENPARENTE_label'] is None
658 699

  
659 700

  
660 701
def test_update_family_info_endpoint_axel_error(app, resource, update_params, family_data):
661
-