Projet

Général

Profil

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

Lauréline Guérin, 13 février 2020 15:47

Télécharger (11,9 ko)

Voir les différences:

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

 functests/toulouse_axel/test_toulouse_axel.py |  7 +--
 passerelle/contrib/toulouse_axel/models.py    | 28 +++++++++++
 passerelle/contrib/toulouse_axel/utils.py     | 50 +++++++++++++++++++
 tests/data/toulouse_axel/family_info.xml      |  4 +-
 tests/test_toulouse_axel.py                   | 47 ++++++++++++++++-
 5 files changed, 130 insertions(+), 6 deletions(-)
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 ['id', 'text', 'NOM', 'DATENAISSANCE', 'SEXE', 'PRENOMPERE', 'PRENOMMERE', 'NOMPERE', 'NOMMERE', 'RATTACHEAUTREDUI', 'PRENOM',
......
63 63
        for contact in enfant.get('CONTACT', []):
64 64
            contact.pop('id')
65 65
            contact.pop('text')
66
            contact.pop('LIENPARENTE_label')
66 67
        if 'SANITAIRE' not in enfant:
67 68
            continue
68 69
        # manage handicap data (not the same schema)
passerelle/contrib/toulouse_axel/models.py
452 452
        link.delete()
453 453
        return {'link': link_id, 'deleted': True, 'dui': link.dui}
454 454

  
455
    @endpoint(
456
        description=_("Get a referential"),
457
        perm='can_access',
458
        pattern=r'^(?P<code>[\w-]+)/?$',
459
        example_pattern='csp',
460
        parameters={
461
            'code': {'description': _('Referential code. Possible values: situation_familiale, csp, lien_parente, type_regime')},
462
        })
463
    def referential(self, request, code):
464
        if code not in ['situation_familiale', 'csp', 'lien_parente', 'type_regime']:
465
            raise APIError('Referential not found', err_code='not-found')
466
        references = getattr(utils, '{}_mapping'.format(code))
467
        if references is None:
468
            raise APIError('Referential not found', err_code='not-found', http_status=404)
469
        return {'data': [{'id': key, 'text': val} for key, val in references.items()]}
470

  
455 471
    def get_family_data(self, dui, check_registrations=False):
456 472
        try:
457 473
            result = ref_famille_dui(self, {'PORTAIL': {'DUI': {'IDDUI': dui}}})
......
480 496
                child['clae_cantine_current'] = children_registred_for_current_year.get(child['IDPERSONNE'])
481 497
                child['clae_cantine_next'] = children_registred_for_next_year.get(child['IDPERSONNE'])
482 498

  
499
        family_data['SITUATIONFAMILIALE_label'] = utils.get_label(utils.situation_familiale_mapping, family_data['SITUATIONFAMILIALE'])
500
        for key in ['RL1', 'RL2']:
501
            if key not in family_data:
502
                continue
503
            rl = family_data[key]
504
            rl['CSP_label'] = utils.get_label(utils.csp_mapping, rl['CSP'])
505

  
483 506
        for child in family_data.get('ENFANT', []):
484 507
            child['id'] = child['IDPERSONNE']
485 508
            child['text'] = '{} {}'.format(child['PRENOM'], child['NOM']).strip()
486 509
            for i, contact in enumerate(child.get('CONTACT', [])):
487 510
                contact['id'] = i
488 511
                contact['text'] = '{} {}'.format(contact['PRENOM'], contact['NOM']).strip()
512
                contact['LIENPARENTE_label'] = utils.get_label(utils.lien_parente_mapping, contact['LIENPARENTE'])
513

  
514
        if 'REVENUS' in family_data:
515
            family_data['REVENUS']['TYPEREGIME_label'] = utils.get_label(utils.type_regime_mapping, family_data['REVENUS']['TYPEREGIME'])
516

  
489 517
        return family_data
490 518

  
491 519
    @endpoint(
passerelle/contrib/toulouse_axel/utils.py
1
# -*- coding: utf-8 -*-
1 2
# passerelle - uniform access to multiple data sources and services
2 3
# Copyright (C) 2020  Entr'ouvert
3 4
#
......
14 15
# You should have received a copy of the GNU Affero General Public License
15 16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17

  
18
from __future__ import unicode_literals
19

  
20
from collections import OrderedDict
17 21
import datetime
18 22
import xml.etree.ElementTree as ET
19 23

  
......
37 41
xml_datetime_format = '%d/%m/%Y %H:%M:%S'
38 42

  
39 43

  
44
situation_familiale_mapping = OrderedDict([
45
    ('C', 'Célibataire'),
46
    ('D', 'Divorcé (e)'),
47
    ('M', 'Marié (e)'),
48
    ('S', 'Séparé (e)'),
49
    ('V', 'Veuf (ve)'),
50
    ('VM', 'Vie Maritale'),
51
    ('P', 'Pacs'),
52
])
53

  
54
csp_mapping = OrderedDict([
55
    ('OUV', 'Ouvriers'),
56
    ('EMP', 'Employés'),
57
    ('ETU', 'Etudiants'),
58
    ('RET', 'Retraités'),
59
    ('STA', 'Personnes en stage'),
60
    ('AGEX', 'Agriculteurs exploitants'),
61
    ('ARCO', 'Artisans commercants chefs entreprise'),
62
    ('CADP', 'Cadres professions intellectuelles supérieures'),
63
    ('PRIN', 'Professions intermediaires'),
64
    ('SACT', 'Autres personnes sans activité professionnelle'),
65
    ('REC', 'Recherche emploi'),
66
])
67

  
68
lien_parente_mapping = OrderedDict([
69
    ('GRP1', 'Grands-parents paternels'),
70
    ('GRP2', 'Grands-parents maternels'),
71
    ('VOI', 'Voisin'),
72
    ('FRE', "Frère de l'enfant"),
73
    ('SOE', "Soeur de l'enfant"),
74
    ('AMI', 'Ami (e)'),
75
    ('FAMI', 'Membre de la famille'),
76
    ('BABY', 'Baby sitter'),
77
])
78

  
79
type_regime_mapping = OrderedDict([
80
    ('GENE', 'Régime général'),
81
    ('ZAU', 'Autre'),
82
    ('MSA', 'MSA'),
83
])
84

  
85

  
86
def get_label(mapping, code):
87
    return mapping.get(code, code)
88

  
89

  
40 90
def indent(tree, space="  ", level=0):
41 91
    # backport from Lib/xml/etree/ElementTree.py python 3.9
42 92
    if isinstance(tree, ET.ElementTree):
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
15 15
# You should have received a copy of the GNU Affero General Public License
16 16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 17

  
18
from __future__ import unicode_literals
19

  
18 20
from contextlib import contextmanager
19 21
import copy
20 22
import datetime
......
45 47
    ref_verif_dui,
46 48
    reservation_periode,
47 49
)
48
from passerelle.contrib.toulouse_axel.utils import get_reference_year_from_date
50
from passerelle.contrib.toulouse_axel.utils import (
51
    situation_familiale_mapping,
52
    csp_mapping,
53
    lien_parente_mapping,
54
    type_regime_mapping,
55
    get_reference_year_from_date,
56
)
49 57
from passerelle.utils.jsonresponse import APIError
50 58
import utils
51 59

  
......
625 633
    assert resp.json['deleted'] is True
626 634

  
627 635

  
636
def test_refetential_endpoint_no_result(app, resource):
637
    resp = app.get('/toulouse-axel/test/referential/foo/')
638
    assert resp.json['err_desc'] == "Referential not found"
639
    assert resp.json['err'] == 'not-found'
640

  
641

  
642
@pytest.mark.parametrize('code, mapping', [
643
    ('situation_familiale', situation_familiale_mapping),
644
    ('csp', csp_mapping),
645
    ('lien_parente', lien_parente_mapping),
646
    ('type_regime', type_regime_mapping),
647
])
648
def test_refetential_endpoint(app, resource, code, mapping):
649
    resp = app.get('/toulouse-axel/test/referential/%s/' % code)
650
    expected = [{'id': k, 'text': v} for k, v in mapping.items()]
651
    assert dict(resp.json['data']) == dict(expected)
652

  
653

  
628 654
def test_family_info_endpoint_axel_error(app, resource):
629 655
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
630 656
    with mock.patch('passerelle.contrib.toulouse_axel.models.ref_famille_dui') as operation:
......
661 687
        'RL1',
662 688
        'RL2',
663 689
        'SITUATIONFAMILIALE',
690
        'SITUATIONFAMILIALE_label',
664 691
        'TELFIXE',
665 692
    ])
666 693
    assert resp.json['data']['ENFANT'][0]['id'] == '4242'
......
694 721
    assert resp.json['data']['ENFANT'][1]['clae_cantine_current'] is None
695 722
    assert resp.json['data']['ENFANT'][1]['clae_cantine_next'] is True
696 723

  
724
    assert resp.json['data']['SITUATIONFAMILIALE'] == 'S'
725
    assert resp.json['data']['SITUATIONFAMILIALE_label'] == 'Séparé (e)'
726
    assert resp.json['data']['RL1']['CSP'] == 'ETU'
727
    assert resp.json['data']['RL1']['CSP_label'] == 'Etudiants'
728
    assert resp.json['data']['RL2']['CSP'] == 'EMP'
729
    assert resp.json['data']['RL2']['CSP_label'] == 'Employés'
730
    assert resp.json['data']['ENFANT'][0]['CONTACT'][0]['LIENPARENTE'] == 'GRP1'
731
    assert resp.json['data']['ENFANT'][0]['CONTACT'][0]['LIENPARENTE_label'] == 'Grands-parents paternels'
732
    assert resp.json['data']['ENFANT'][0]['CONTACT'][1]['LIENPARENTE'] is None
733
    assert resp.json['data']['ENFANT'][0]['CONTACT'][1]['LIENPARENTE_label'] is None
734
    assert resp.json['data']['REVENUS']['TYPEREGIME'] == 'GENE'
735
    assert resp.json['data']['REVENUS']['TYPEREGIME_label'] == 'Régime général'
736

  
697 737

  
698 738
def test_children_info_endpoint_axel_error(app, resource):
699 739
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
......
807 847
    assert resp.json['data']['clae_cantine_current'] is None
808 848
    assert resp.json['data']['clae_cantine_next'] is False
809 849

  
850
    assert resp.json['data']['CONTACT'][0]['LIENPARENTE'] == 'GRP1'
851
    assert resp.json['data']['CONTACT'][0]['LIENPARENTE_label'] == 'Grands-parents paternels'
852
    assert resp.json['data']['CONTACT'][1]['LIENPARENTE'] is None
853
    assert resp.json['data']['CONTACT'][1]['LIENPARENTE_label'] is None
854

  
810 855

  
811 856
def test_child_contacts_info_endpoint_axel_error(app, resource):
812 857
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
813
-