atal: return comments in infos endpoint (#37194)

This commit is contained in:
Emmanuel Cazenave 2019-11-12 09:56:28 +01:00
parent 8ba12d190e
commit 0d7a92ef2f
3 changed files with 92 additions and 25 deletions

View File

@ -17,6 +17,7 @@
import base64
from django.db import models
from django.utils import dateformat
from django.utils.six.moves import urllib
from django.utils.translation import ugettext_lazy as _
import lxml.etree
@ -29,6 +30,9 @@ from passerelle.utils.jsonresponse import APIError
from . import schemas
DATE_FORMAT = 'l d F Y, G:i'
def process_response(demande_number):
if not demande_number.startswith('DIT'):
raise APIError(demande_number)
@ -188,42 +192,74 @@ class ATALConnector(BaseResource):
return {'data': helpers.serialize_object(soap_res)}
@endpoint(
methods=['get'], perm='can_access', example_pattern='{demande_number}/',
pattern='^(?P<demande_number>\w+)/$',
methods=['get'], perm='can_access', example_pattern='{demand_number}/',
pattern='^(?P<demand_number>\w+)/$',
parameters={
'demande_number': {
'demand_number': {
'description': _('Demande number'), 'example_value': 'DIT18050001'
}
}
)
def infos(self, request, demande_number):
soap_res = self._soap_call(
wsdl='DemandeService', method='retrieveDetailsDemande',
demandeNumberParam=demande_number)
status = helpers.serialize_object(soap_res).get('etatDemande', {}).get('description')
if not status:
raise APIError('Could not get a status')
if status != 'PRISE EN COMPTE':
return {
'data': {
'status': status
}
}
soap_res = self._soap_call(
wsdl='DemandeService', method='retrieveEtatTravaux',
numero=demande_number
def infos(self, request, demand_number):
demand_details = helpers.serialize_object(
self._soap_call(
wsdl='DemandeService', method='retrieveDetailsDemande',
demandeNumberParam=demand_number)
)
status = helpers.serialize_object(soap_res).get('libelle')
if not demand_details:
raise APIError('Could not get a status')
status = (demand_details.get('etatDemande') or {}).get('description')
if not status:
raise APIError('Could not get a status')
return {
'data': {
'status': status
responses = (demand_details.get('reponses') or {}).get('Reponse') or []
comments = []
if responses:
for response in responses:
comment = {
'text': response.get('commentaires'),
'date': None
}
if 'dateReponse' in response:
comment['date'] = dateformat.format(response['dateReponse'], DATE_FORMAT)
comments.append(comment)
last_comment = {
'text': None,
'date': None
}
if comments:
last_comment = comments[-1]
data = {
'status': status,
'demand_details': demand_details,
'comments': comments,
'last_comment': last_comment,
}
if status not in ('PRISE EN COMPTE', 'ARCHIVEE'):
return {
'data': data
}
works_status = helpers.serialize_object(
self._soap_call(
wsdl='DemandeService', method='retrieveEtatTravaux',
numero=demand_number
)
)
status = works_status.get('libelle')
if not status:
raise APIError('Could not get a status')
data['works_status'] = works_status
data['status'] = status
return {
'data': data
}
@endpoint(
perm='can_access',
post={

File diff suppressed because one or more lines are too long

View File

@ -253,3 +253,33 @@ def test_infos(app, connector, monkeypatch):
response = app.get('/atal/slug-atal/infos/DIT18050001/')
assert response.json['err'] == 0
assert response.json['data']['status'] == u'travaux pas commencés'
# User comments in response
api_response1 = mock.Mock(
content=get_file('details_demande_response_with_comments.xml'), status_code=200,
headers={'Content-Type': 'text/xml'}
)
api_response2 = mock.Mock(
content=get_file('etat_travaux_response.xml') % u'travaux pas commencés', status_code=200,
headers={'Content-Type': 'text/xml'}
)
monkeypatch.setattr(
passerelle.utils.Request, 'post', mock.Mock(side_effect=[api_response1, api_response2])
)
response = app.get('/atal/slug-atal/infos/DIT18050001/')
assert response.json['err'] == 0
data = response.json['data']
assert data['status'] == u'travaux pas commencés'
assert len(data['comments']) == 2
assert data['comments'][0] == {
'text': 'OK',
'date': 'Thursday 24 October 2019, 16:48'
}
last_comment = {
'text': 'bonjour atal',
'date': 'Thursday 24 October 2019, 16:51'
}
assert data['comments'][1] == last_comment
assert data['last_comment'] == last_comment