caluire-axel: child_activities_info enpoint (#53934)

This commit is contained in:
Lauréline Guérin 2021-05-18 10:50:04 +02:00
parent f2996eb9c6
commit 85e1af89db
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
7 changed files with 247 additions and 12 deletions

View File

@ -62,6 +62,19 @@ def test_link(conn, user):
assert res['err'] == 0
print('\n')
print("and GET activities info")
url = conn + '/child_activities_info?NameID=%s&idpersonne=%s&schooling_date=%s' % (
name_id,
child['IDENT'],
datetime.date.today().strftime('%Y-%m-%d'),
)
resp = requests.get(url)
resp.raise_for_status()
res = resp.json()
pprint.pprint(res)
assert res['err'] == 0
print('\n')
print("GET school list")
url = conn + '/school_list'
payload = {

View File

@ -220,7 +220,7 @@ class CaluireAxel(BaseResource):
'street': {'description': _('Address: street')},
'zipcode': {'description': _('Address: zipcode')},
'city': {'description': _('Address: city')},
'schooling_date': {'description': _('Booking date (to get reference year)')},
'schooling_date': {'description': _('Schooling date (to get reference year)')},
'school_level': {'description': _('Requested school level')},
},
)
@ -269,7 +269,7 @@ class CaluireAxel(BaseResource):
parameters={
'NameID': {'description': _('Publik ID')},
'idpersonne': {'description': _('Child ID')},
'schooling_date': {'description': _('Booking date (to get reference year)')},
'schooling_date': {'description': _('Schooling date (to get reference year)')},
},
)
def child_schooling_info(self, request, NameID, idpersonne, schooling_date):
@ -300,6 +300,49 @@ class CaluireAxel(BaseResource):
return {'data': schooling_data}
@endpoint(
display_category=_('Schooling'),
display_order=3,
description=_("Get information about activities of a child for the year"),
perm='can_access',
parameters={
'NameID': {'description': _('Publik ID')},
'idpersonne': {'description': _('Child ID')},
'schooling_date': {'description': _('Schooling date (to get reference year)')},
},
)
def child_activities_info(self, request, NameID, idpersonne, schooling_date):
link = self.get_link(NameID)
try:
schooling_date = datetime.datetime.strptime(schooling_date, axel.json_date_format)
except ValueError:
raise APIError('bad date format, should be YYYY-MM-DD', err_code='bad-request', http_status=400)
child_data = self.get_child_data(link.family_id, idpersonne)
if child_data is None:
raise APIError('Child not found', err_code='not-found')
reference_year = utils.get_reference_year_from_date(schooling_date)
try:
result = schemas.get_list_activites(
self,
{
'PORTAIL': {
'GETLISTACTIVITES': {'IDENTINDIVIDU': idpersonne, 'ANNEE': str(reference_year)}
}
},
)
except axel.AxelError as e:
raise APIError(
'Axel error: %s' % e,
err_code='error',
data={'xml_request': e.xml_request, 'xml_response': e.xml_response},
)
activities_data = result.json_response['DATA']['PORTAIL']['GETLISTACTIVITES']
return {'data': activities_data}
class Link(models.Model):
resource = models.ForeignKey(CaluireAxel, on_delete=models.CASCADE)

View File

@ -76,6 +76,7 @@ find_individus = Operation('FindIndividus')
get_famille_individus = Operation('GetFamilleIndividus')
get_individu = Operation('GetIndividu')
get_list_ecole = Operation('GetListEcole')
get_list_activites = Operation('GetListActivites')
LINK_SCHEMA = copy.deepcopy(

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:all="urn:AllAxelTypes">
<xsd:import schemaLocation="./AllAxelTypes.xsd" namespace="urn:AllAxelTypes" />
<xsd:complexType name="PORTAILType">
<xsd:sequence>
<xsd:element ref="GETLISTACTIVITES" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="GETLISTACTIVITESType">
<xsd:sequence>
<xsd:element ref="ANNEE" />
<xsd:element ref="IDENTINDIVIDU" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="IDENTINDIVIDU" type="all:IDENTREQUIREDType"/>
<xsd:element name="ANNEE" type="all:ANNEEType"/>
<xsd:element name="GETLISTACTIVITES" type="GETLISTACTIVITESType"/>
<xsd:element name="PORTAIL" type="PORTAILType"/>
</xsd:schema>

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:all="urn:AllAxelTypes" xmlns:ind="urn:Individu" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:import schemaLocation="./AllAxelTypes.xsd" namespace="urn:AllAxelTypes" />
<xsd:redefine schemaLocation="./R_ShemaResultat.xsd">
<xsd:simpleType name="TYPEType">
<xsd:restriction base="TYPEType">
<xsd:enumeration value="GetListActivites" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="PORTAILType">
<xsd:complexContent>
<xsd:extension base="PORTAILType">
<xsd:sequence>
<xsd:element ref="GETLISTACTIVITES" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
<xsd:complexType name="ACTIVITEType">
<xsd:sequence>
<xsd:element ref="IDENTACTIVITE" />
<xsd:element ref="LIBELLEACTIVITE"/>
<xsd:element ref="ENTREE"/>
<xsd:element ref="SORTIE"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="GETLISTACTIVITESType">
<xsd:sequence>
<xsd:element ref="CODE" />
<xsd:element ref="ACTIVITE" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="CODE" type="xsd:integer"/>
<xsd:element name="IDENTACTIVITE" type="all:IDREQUIREDType"/>
<xsd:element name="LIBELLEACTIVITE" type="all:LIBELLE100Type"/>
<xsd:element name="ENTREE" type="all:DATEType"/>
<xsd:element name="SORTIE" type="all:DATEType"/>
<xsd:element name="ACTIVITE" type="ACTIVITEType"/>
<xsd:element name="GETLISTACTIVITES" type="GETLISTACTIVITESType"/>
</xsd:schema>

View File

@ -0,0 +1,11 @@
<PORTAIL>
<GETLISTACTIVITES>
<CODE>1</CODE>
<ACTIVITE>
<IDENTACTIVITE>ELEM</IDENTACTIVITE>
<LIBELLEACTIVITE>Restaurant El&#233;mentaire </LIBELLEACTIVITE>
<ENTREE>01/09/2020</ENTREE>
<SORTIE>31/07/2021</SORTIE>
</ACTIVITE>
</GETLISTACTIVITES>
</PORTAIL>

View File

@ -180,6 +180,32 @@ def test_operation_get_famille_individus(resource, content):
)
@pytest.mark.parametrize(
'content',
[
'<PORTAIL><GETLISTECOLE/></PORTAIL>',
],
)
def test_operation_get_list_ecole(resource, content):
with mock_getdata(content, 'GetListEcole'):
with pytest.raises(AxelError):
schemas.get_list_ecole(
resource,
{
'PORTAIL': {
'GETLISTECOLE': {
'NORUE': '42',
'ADRESSE1': 'rue Pasteur',
'CODEPOSTAL': '69300',
'VILLE': 'Caluire et Cuire',
'IDENTNIVEAU': '',
'ANNEE': '2021',
}
}
},
)
@pytest.mark.parametrize(
'content',
[
@ -205,22 +231,18 @@ def test_operation_get_individu(resource, content):
@pytest.mark.parametrize(
'content',
[
'<PORTAIL><GETLISTECOLE/></PORTAIL>',
'<PORTAIL><GETLISTACTIVITES/></PORTAIL>',
],
)
def test_operation_get_list_ecole(resource, content):
with mock_getdata(content, 'GetListEcole'):
def test_operation_get_list_activites(resource, content):
with mock_getdata(content, 'GetListActivites'):
with pytest.raises(AxelError):
schemas.get_list_ecole(
schemas.get_list_activites(
resource,
{
'PORTAIL': {
'GETLISTECOLE': {
'NORUE': '42',
'ADRESSE1': 'rue Pasteur',
'CODEPOSTAL': '69300',
'VILLE': 'Caluire et Cuire',
'IDENTNIVEAU': '',
'GETLISTACTIVITES': {
'IDENTINDIVIDU': 'XXX',
'ANNEE': '2021',
}
}
@ -696,3 +718,73 @@ def test_child_schooling_info(app, resource, family_data):
)
assert resp.json['err'] == 0
assert set(resp.json['data'].keys()) == set(['CODE', 'INDIVIDU', 'SCOLAIRE'])
def test_child_activities_info_endpoint_axel_error(app, resource):
Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
with mock.patch('passerelle.contrib.caluire_axel.schemas.get_famille_individus') as operation:
operation.side_effect = AxelError('FooBar')
resp = app.get(
'/caluire-axel/test/child_activities_info?NameID=yyy&idpersonne=50632&schooling_date=2021-05-10'
)
assert resp.json['err_desc'] == "Axel error: FooBar"
assert resp.json['err'] == 'error'
filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/family_info.xml')
with open(filepath) as xml:
content = xml.read()
with mock_getdata(content, 'GetFamilleIndividus'):
with mock.patch('passerelle.contrib.caluire_axel.schemas.get_list_activites') as operation:
operation.side_effect = AxelError('FooBar')
resp = app.get(
'/caluire-axel/test/child_activities_info?NameID=yyy&idpersonne=50632&schooling_date=2021-05-10'
)
assert resp.json['err_desc'] == "Axel error: FooBar"
assert resp.json['err'] == 'error'
@pytest.mark.parametrize('value', ['foo', '20/01/2020', '2020'])
def test_child_activities_info_endpoint_bad_date_format(app, resource, value):
Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
resp = app.get(
'/caluire-axel/test/child_activities_info?NameID=yyy&idpersonne=50632&schooling_date=%s' % value,
status=400,
)
assert resp.json['err_desc'] == "bad date format, should be YYYY-MM-DD"
assert resp.json['err'] == 'bad-request'
def test_child_activities_info_endpoint_no_result(app, resource):
resp = app.get(
'/caluire-axel/test/child_activities_info?NameID=yyy&idpersonne=50632&schooling_date=2021-05-10'
)
assert resp.json['err_desc'] == "Person not found"
assert resp.json['err'] == 'not-found'
Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/family_info.xml')
with open(filepath) as xml:
content = xml.read()
with mock_getdata(content, 'GetFamilleIndividus'):
resp = app.get(
'/caluire-axel/test/child_activities_info?NameID=yyy&idpersonne=zzz&schooling_date=2021-05-10'
)
assert resp.json['err_desc'] == "Child not found"
assert resp.json['err'] == 'not-found'
def test_child_activities_info(app, resource, family_data):
Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/activities_info.xml')
with open(filepath) as xml:
content = xml.read()
with mock_getdata(content, 'GetListActivites'):
with mock.patch(
'passerelle.contrib.caluire_axel.models.CaluireAxel.get_family_data',
return_value=family_data,
):
resp = app.get(
'/caluire-axel/test/child_activities_info?NameID=yyy&idpersonne=50632&schooling_date=2021-05-10'
)
assert resp.json['err'] == 0
assert set(resp.json['data'].keys()) == set(['CODE', 'ACTIVITE'])