toulouse-maelis: get towns, completed with ban if available (#76444)
gitea/passerelle/pipeline/head This commit looks good
Details
gitea/passerelle/pipeline/head This commit looks good
Details
This commit is contained in:
parent
d4d5bcbf4c
commit
42d08159db
|
@ -16,6 +16,7 @@
|
|||
import base64
|
||||
import copy
|
||||
import datetime
|
||||
import re
|
||||
from operator import itemgetter
|
||||
from urllib.parse import urljoin
|
||||
|
||||
|
@ -33,6 +34,7 @@ from requests import RequestException
|
|||
from zeep.helpers import serialize_object
|
||||
from zeep.wsse.username import UsernameToken
|
||||
|
||||
from passerelle.apps.base_adresse.models import CityModel
|
||||
from passerelle.base.models import BaseResource, HTTPResource
|
||||
from passerelle.utils.api import endpoint
|
||||
from passerelle.utils.conversion import simplify
|
||||
|
@ -172,6 +174,7 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
'RLIndicator',
|
||||
'Situation',
|
||||
'Street',
|
||||
'Town',
|
||||
'Vaccin',
|
||||
):
|
||||
id_key, text_key = 'code', 'libelle'
|
||||
|
@ -232,6 +235,25 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
data = self.get_referential_data('Invoice', 'Regie')
|
||||
self.update_referential('Regie', data, 'code', 'libelle')
|
||||
|
||||
def merge_zipcodes(self):
|
||||
city = CityModel.objects.first()
|
||||
if not city:
|
||||
return
|
||||
cities_by_insee = {x.code: x for x in CityModel.objects.filter(resource=city.resource)}
|
||||
for town in self.referential.filter(referential_name='Town').all():
|
||||
zipcode = None
|
||||
if 'ARRONDISSEMENT' in town.item_text:
|
||||
city, district = re.match(r'([A-Z]+).*(\d+)', town.item_text).groups()
|
||||
zipcode = {'PARIS': '75', 'LYON': '69', 'MARSEILLE': '13'}.get(city) + district.zfill(3)
|
||||
else:
|
||||
city = cities_by_insee.get(town.item_id)
|
||||
if city:
|
||||
zipcode = city.zipcode
|
||||
if zipcode:
|
||||
town.item_data['zipcode'] = zipcode
|
||||
town.item_data['zip_and_text'] = '%s %s' % (zipcode, town.item_text)
|
||||
town.save()
|
||||
|
||||
def daily(self):
|
||||
self.update_referentials()
|
||||
|
||||
|
@ -242,7 +264,8 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
self.update_activity_referentials()
|
||||
self.update_ape_referentials()
|
||||
self.update_invoice_referentials()
|
||||
|
||||
# merge zip codes from base adresse into town referential
|
||||
self.merge_zipcodes()
|
||||
except UpdateError as e:
|
||||
self.logger.warning('Erreur sur la mise à jour: %s' % e)
|
||||
else:
|
||||
|
@ -769,6 +792,21 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
def read_country_list(self, request, id=None, q=None, limit=None, distinct=True):
|
||||
return {'data': self.get_referential('Country', id, q, limit, distinct)}
|
||||
|
||||
@endpoint(
|
||||
display_category='Famille',
|
||||
description='Lister les communes',
|
||||
name='read-town-list',
|
||||
perm='can_access',
|
||||
parameters={
|
||||
'id': {'description': 'Identifiant de la commune (code INSEE)'},
|
||||
'q': {'description': 'Recherche en texte intégral'},
|
||||
'limit': {'description': 'Nombre maximal de résultats; doit être inférieur à 20.'},
|
||||
'distinct': {'description': 'Ne pas inclure les valeurs dupliquées'},
|
||||
},
|
||||
)
|
||||
def read_town_list(self, request, id=None, q=None, limit=None, distinct=True):
|
||||
return {'data': self.get_referential('Town', id, q, limit, distinct)}
|
||||
|
||||
@endpoint(
|
||||
display_category='Famille',
|
||||
description='Lister les catégories socio-professionnelles',
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body>
|
||||
<ns2:readTownListResponse xmlns:ns2="family.ws.maelis.sigec.com">
|
||||
<itemList>
|
||||
<code>69391</code>
|
||||
<libelle>LYON 1ER ARRONDISSEMENT</libelle>
|
||||
</itemList>
|
||||
<itemList>
|
||||
<code>07120</code>
|
||||
<libelle>LACHAMP-RAPHAEL</libelle>
|
||||
</itemList>
|
||||
<itemList>
|
||||
<code>64001</code>
|
||||
<libelle>AAST</libelle>
|
||||
</itemList>
|
||||
</ns2:readTownListResponse>
|
||||
</soap:Body>
|
||||
</soap:Envelope>
|
|
@ -27,6 +27,7 @@ from requests.exceptions import ConnectionError, ReadTimeout
|
|||
from zeep import Settings
|
||||
from zeep.helpers import serialize_object
|
||||
|
||||
from passerelle.apps.base_adresse.models import BaseAdresse, CityModel
|
||||
from passerelle.contrib.toulouse_maelis.models import Link, Referential, ToulouseMaelis
|
||||
from passerelle.contrib.toulouse_maelis.utils import get_public_criterias
|
||||
from passerelle.utils.jsonresponse import APIError
|
||||
|
@ -205,6 +206,7 @@ def django_db_setup(django_db_setup, django_db_blocker):
|
|||
'readSituationList', get_xml_file('R_read_situation_list.xml')
|
||||
)
|
||||
soap_mock.add_soap_response('readStreetList', get_xml_file('R_read_street_list.xml'))
|
||||
soap_mock.add_soap_response('readTownList', get_xml_file('R_read_town_list.xml'))
|
||||
soap_mock.add_soap_response('readVaccinList', get_xml_file('R_read_vaccin_list.xml'))
|
||||
con.update_family_referentials()
|
||||
with site_service() as site_mock:
|
||||
|
@ -520,6 +522,7 @@ def test_cron(db):
|
|||
'Sex',
|
||||
'Situation',
|
||||
'Street',
|
||||
'Town',
|
||||
'Vaccin',
|
||||
'YearSchool',
|
||||
]
|
||||
|
@ -660,6 +663,50 @@ def test_read_country_list(con, app):
|
|||
]
|
||||
|
||||
|
||||
def test_read_town_list(con, app):
|
||||
url = get_endpoint('read-town-list')
|
||||
resp = app.get(url)
|
||||
assert resp.json['err'] == 0
|
||||
assert len(resp.json['data']) == 3
|
||||
assert resp.json['data'] == [
|
||||
{'code': '64001', 'id': '64001', 'libelle': 'AAST', 'text': 'AAST'},
|
||||
{'code': '07120', 'id': '07120', 'libelle': 'LACHAMP-RAPHAEL', 'text': 'LACHAMP-RAPHAEL'},
|
||||
{
|
||||
'code': '69391',
|
||||
'id': '69391',
|
||||
'libelle': 'LYON 1ER ARRONDISSEMENT',
|
||||
'text': 'LYON 1ER ARRONDISSEMENT',
|
||||
},
|
||||
]
|
||||
# check merging zipcode
|
||||
ban = BaseAdresse.objects.create(slug='ban')
|
||||
CityModel.objects.create(
|
||||
name='Lachamp-Raphaël', code='07120', zipcode='07530', population=68, resource=ban
|
||||
)
|
||||
con.merge_zipcodes()
|
||||
|
||||
resp = app.get(url)
|
||||
assert resp.json['data'] == [
|
||||
{'code': '64001', 'id': '64001', 'libelle': 'AAST', 'text': 'AAST'},
|
||||
{
|
||||
'code': '07120',
|
||||
'id': '07120',
|
||||
'libelle': 'LACHAMP-RAPHAEL',
|
||||
'text': 'LACHAMP-RAPHAEL',
|
||||
'zip_and_text': '07530 LACHAMP-RAPHAEL',
|
||||
'zipcode': '07530',
|
||||
},
|
||||
{
|
||||
'code': '69391',
|
||||
'id': '69391',
|
||||
'libelle': 'LYON 1ER ARRONDISSEMENT',
|
||||
'text': 'LYON 1ER ARRONDISSEMENT',
|
||||
'zip_and_text': '69001 LYON 1ER ARRONDISSEMENT',
|
||||
'zipcode': '69001',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def test_read_child_indicator_list(con, app):
|
||||
url = get_endpoint('read-child-indicator-list')
|
||||
resp = app.get(url)
|
||||
|
|
Loading…
Reference in New Issue