chrono: allow asking for many pre-demandes statuses (#80597)
gitea/ants-hub/pipeline/head This commit looks good Details

This commit is contained in:
Benjamin Dauvergne 2023-08-28 11:58:24 +02:00
parent 1927c358f7
commit 3c4b0b50b5
2 changed files with 72 additions and 18 deletions

View File

@ -402,7 +402,7 @@ rendez_vous_disponibles = csrf_exempt(authenticate(RendezVousDisponibleView.as_v
class PredemandesView(View):
def get_autres_demandes(self, identifiant_predemande):
def get_autres_demandes(self, identifiant_predemandes):
auth_token = Config.get(Config.REQUEST_TO_ANTS_AUTH_TOKEN)
url = Config.get(Config.REQUEST_TO_ANTS_BASE_URL, 'https://rendez-vous-api.france-identite.fr/api/')
if not auth_token:
@ -411,11 +411,14 @@ class PredemandesView(View):
self.request.raccordement,
)
return None, JsonResponse({'err': 'auth-token-not-configured'}, status=500)
params = [
('application_ids', identifiant_predemande) for identifiant_predemande in identifiant_predemandes
]
try:
response = requests.get(
f'{url}searchApplicationIds',
headers={'x-hub-rdv-auth-token': auth_token},
params={'application_ids': identifiant_predemande},
params=params,
timeout=10,
)
response.raise_for_status()
@ -425,7 +428,9 @@ class PredemandesView(View):
{'err': 1, 'err_class': 'rendez-vous-api-is-unavailable', 'err_desc': str(e)}, status=200
)
try:
data = response.json().get(identifiant_predemande, [])
data = response.json()
if not isinstance(data, dict):
raise ValueError('data is not a dict')
except (ValueError, AttributeError) as e:
logger.error(
'predemandes(%s) %s is down, result is not a JSON object: %s',
@ -437,28 +442,37 @@ class PredemandesView(View):
{'err': 1, 'err_class': 'rendez-vous-api-is-unavailable', 'err_desc': str(e)}, status=200
)
except KeyError:
data = []
for rendez_vous in data:
# fix stupid formatting of dates from ANTS, Paris time with 'Z' suffix :/
date = rendez_vous.get('datetime', None)
date = datetime.datetime.fromisoformat(rendez_vous['datetime'].rstrip('Z'))
date = date.replace(tzinfo=ANTS_TIMEZONE)
date = localtime(date)
rendez_vous['datetime'] = make_naive(date).isoformat()
data = {}
response_data = []
for identifiant_predemande in identifiant_predemandes:
meetings = data.get(identifiant_predemande, [])
for rendez_vous in meetings:
# fix stupid formatting of dates from ANTS, Paris time with 'Z' suffix :/
date = rendez_vous.get('datetime', None)
date = datetime.datetime.fromisoformat(rendez_vous['datetime'].rstrip('Z'))
date = date.replace(tzinfo=ANTS_TIMEZONE)
date = localtime(date)
rendez_vous = rendez_vous.copy()
rendez_vous['identifiant_predemande'] = identifiant_predemande
rendez_vous['datetime'] = make_naive(date).isoformat()
response_data.append(rendez_vous)
logger.info(
'predemandes(%s) returned %s demandes from %s for predemande "%s"',
self.request.raccordement,
len(data),
','.join(map(str, (rendez_vous['meeting_point'] for rendez_vous in data))),
identifiant_predemande,
len(response_data),
','.join(map(str, (rendez_vous['meeting_point'] for rendez_vous in response_data))),
','.join(identifiant_predemandes),
)
return data, None
return response_data, None
def get(self, request):
identifiant_predemande = request.GET.get('identifiant_predemande', '').strip()
if not identifiant_predemande:
identifiant_predemandes = [
part.strip() for part in request.GET.getlist('identifiant_predemande') if part.strip()
]
if not identifiant_predemandes:
return JsonResponse({'err': 'missing=identifiant-predemande'}, status=400)
autres_demandes, error_response = self.get_autres_demandes(identifiant_predemande)
autres_demandes, error_response = self.get_autres_demandes(identifiant_predemandes)
if error_response is not None:
return error_response

View File

@ -386,6 +386,7 @@ def test_predemandes(db, django_app):
'datetime': '2023-04-03T10:00:00',
'management_url': 'https://saint-didier/rdv/1/',
'cancel_url': 'https://saint-didier/rdv/1/cancel/',
'identifiant_predemande': 'xyz',
}
]
assert (
@ -393,6 +394,45 @@ def test_predemandes(db, django_app):
== 'https://rendez-vous-api.france-identite.fr/api/searchApplicationIds?application_ids=xyz'
)
# with multiple identifiant_predemande
document['1234'] = [
{
'meeting_point': 'MAIRIE DE POUETPOUET',
'datetime': '2023-04-03T10:00:00Z',
}
]
responses.add(
responses.GET,
rdv_api_url,
json=document,
status=200,
match=[responses.matchers.header_matcher({'x-hub-rdv-auth-token': 'xyz'})],
)
response = django_app.get(
'/api/chrono/predemandes/',
params=[('identifiant_predemande', 'xyz'), ('identifiant_predemande', '1234')],
)
assert response.json['err'] == 0
assert response.json['data'] == [
{
'meeting_point': 'MAIRIE DE SAINT-DIDIER',
'datetime': '2023-04-03T10:00:00',
'management_url': 'https://saint-didier/rdv/1/',
'cancel_url': 'https://saint-didier/rdv/1/cancel/',
'identifiant_predemande': 'xyz',
},
{
'datetime': '2023-04-03T10:00:00',
'identifiant_predemande': '1234',
'meeting_point': 'MAIRIE DE POUETPOUET',
},
]
assert (
responses._default_mock.calls[1].request.url
== 'https://rendez-vous-api.france-identite.fr/api/searchApplicationIds?application_ids=xyz&application_ids=1234'
)
def test_rendez_vous_disponibles_full(django_app, db, freezer):
Raccordement.objects.create(name='plateforme', apikey='abcd')