Projet

Général

Profil

0001-toulouse_axel-add-clae_booking_activities_info-endpo.patch

Lauréline Guérin, 25 février 2020 18:48

Télécharger (16 ko)

Voir les différences:

Subject: [PATCH] toulouse_axel: add clae_booking_activities_info endpoint
 (#40177)

 passerelle/contrib/toulouse_axel/models.py |  77 +++++---
 tests/test_toulouse_axel.py                | 198 +++++++++++++++++++++
 2 files changed, 253 insertions(+), 22 deletions(-)
passerelle/contrib/toulouse_axel/models.py
740 740

  
741 741
        # prepare data
742 742
        post_data['IDDUI'] = link.dui
743
        post_data['DATEDEMANDE'] = datetime.date.today().strftime('%Y-%m-%d')
743
        post_data['DATEDEMANDE'] = datetime.date.today().strftime(utils.json_date_format)
744 744

  
745 745
        self.sanitize_update_family_data(dui=link.dui, post_data=post_data)
746 746

  
......
1013 1013
            return {}
1014 1014
        return {child_id: bool(child.get('ACTIVITE', [])) for child_id, child in children_activities.items()}
1015 1015

  
1016
    @endpoint(
1017
        description=_("Get information about CLAE booking"),
1018
        perm='can_access',
1019
        parameters={
1020
            'NameID': {'description': _('Publik ID')},
1021
            'idpersonne': {'description': _('Child ID')},
1022
            'booking_date': {'description': _('Booking date')},
1023
        })
1024
    def clae_booking_info(self, request, NameID, idpersonne, booking_date):
1025
        link = self.get_link(NameID)
1026
        try:
1027
            booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format)
1028
        except ValueError:
1029
            raise APIError('bad date format', err_code='bad-request', http_status=400)
1030

  
1016
    def get_booking_data(self, dui, child_id, booking_date):
1031 1017
        reference_year = utils.get_reference_year_from_date(booking_date)
1032 1018

  
1033 1019
        # first get activities information for the child
1034 1020
        child_activities = self.get_child_activities(
1035
            dui=link.dui,
1021
            dui=dui,
1036 1022
            reference_year=reference_year,
1037
            child_id=idpersonne)
1023
            child_id=child_id)
1038 1024

  
1039 1025
        # then get booking of the requested week for the child
1040 1026
        activity_ids = [act['IDACTIVITE'] for act in child_activities.get('ACTIVITE', [])]
......
1050 1036
        try:
1051 1037
            data = reservation_periode(self, {'PORTAIL': {
1052 1038
                'DUI': {
1053
                    'IDDUI': link.dui,
1039
                    'IDDUI': dui,
1054 1040
                    'ENFANT': {
1055
                        'IDPERSONNE': idpersonne,
1041
                        'IDPERSONNE': child_id,
1056 1042
                        'ACTIVITE': activity_data,
1057 1043
                    }
1058 1044
                }
......
1066 1052

  
1067 1053
        child_booking = None
1068 1054
        for child in data.json_response['DATA']['PORTAIL']['DUI'].get('ENFANT', []):
1069
            if child['IDPERSONNE'] == idpersonne:
1055
            if child['IDPERSONNE'] == child_id:
1070 1056
                child_booking = child
1071 1057
                break
1072 1058
        if child_booking is None:
......
1088 1074
            }
1089 1075

  
1090 1076
        for activity in child_activities.get('ACTIVITE', []):
1077
            activity['id'] = activity['IDACTIVITE']
1078
            start_date = datetime.datetime.strptime(activity['DATEENTREE'], utils.json_date_format)
1079
            end_date = datetime.datetime.strptime(activity['DATESORTIE'], utils.json_date_format)
1080
            activity['text'] = u'{} (inscription du {} - {})'.format(
1081
                activity['LIBELLEACTIVITE'],
1082
                start_date.strftime(utils.xml_date_format),
1083
                end_date.strftime(utils.xml_date_format))
1084
            activity['annee_reference'] = reference_year
1085
            activity['annee_reference_label'] = '{}/{}'.format(reference_year, reference_year + 1)
1091 1086
            activity['booking'] = booking_days.get(activity['IDACTIVITE'], {})
1092 1087

  
1093
        return {'data': child_activities}
1088
        return child_activities
1089

  
1090
    @endpoint(
1091
        description=_("Get information about CLAE booking"),
1092
        perm='can_access',
1093
        parameters={
1094
            'NameID': {'description': _('Publik ID')},
1095
            'idpersonne': {'description': _('Child ID')},
1096
            'booking_date': {'description': _('Booking date')},
1097
        })
1098
    def clae_booking_info(self, request, NameID, idpersonne, booking_date):
1099
        link = self.get_link(NameID)
1100
        try:
1101
            booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format)
1102
        except ValueError:
1103
            raise APIError('bad date format', err_code='bad-request', http_status=400)
1104

  
1105
        booking_data = self.get_booking_data(dui=link.dui, child_id=idpersonne, booking_date=booking_date)
1106

  
1107
        return {'data': booking_data}
1108

  
1109
    @endpoint(
1110
        description=_("Get information about CLAE booking activities"),
1111
        perm='can_access',
1112
        parameters={
1113
            'NameID': {'description': _('Publik ID')},
1114
            'idpersonne': {'description': _('Child ID')},
1115
            'booking_date': {'description': _('Booking date')},
1116
        })
1117
    def clae_booking_activities_info(self, request, NameID, idpersonne, booking_date):
1118
        link = self.get_link(NameID)
1119
        try:
1120
            booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format)
1121
        except ValueError:
1122
            raise APIError('bad date format', err_code='bad-request', http_status=400)
1123

  
1124
        booking_data = self.get_booking_data(dui=link.dui, child_id=idpersonne, booking_date=booking_date)
1125

  
1126
        return {'data': booking_data.get('ACTIVITE', [])}
1094 1127

  
1095 1128

  
1096 1129
class Link(models.Model):
tests/test_toulouse_axel.py
2008 2008
    assert resp.json['data'] == {
2009 2009
        'ACTIVITE': [
2010 2010
            {
2011
                'id': 'A19P1M1',
2012
                'text': 'Temps du matin (inscription du 01/08/2019 - 31/07/2020)',
2013
                'annee_reference': 2019,
2014
                'annee_reference_label': '2019/2020',
2011 2015
                'COUTREVIENT': '99999',
2012 2016
                'DATEDEBUT': '2019-08-01',
2013 2017
                'DATEENTREE': '2019-08-01',
......
2030 2034
                }
2031 2035
            },
2032 2036
            {
2037
                'id': 'A19P1M2',
2038
                'text': 'Temps du midi (inscription du 01/08/2019 - 31/07/2020)',
2039
                'annee_reference': 2019,
2040
                'annee_reference_label': '2019/2020',
2033 2041
                'COUTREVIENT': '99999',
2034 2042
                'DATEDEBUT': '2019-08-01',
2035 2043
                'DATEENTREE': '2019-08-01',
......
2052 2060
                }
2053 2061
            },
2054 2062
            {
2063
                'id': 'A19P1M3',
2064
                'text': 'Temps du soir (inscription du 01/08/2019 - 31/07/2020)',
2065
                'annee_reference': 2019,
2066
                'annee_reference_label': '2019/2020',
2055 2067
                'COUTREVIENT': '99999',
2056 2068
                'DATEDEBUT': '2019-08-01',
2057 2069
                'DATEENTREE': '2019-08-01',
......
2074 2086
                }
2075 2087
            },
2076 2088
            {
2089
                'id': 'A19P1M4',
2090
                'text': u'Temps mercredi après midi (inscription du 01/08/2019 - 31/07/2020)',
2091
                'annee_reference': 2019,
2092
                'annee_reference_label': '2019/2020',
2077 2093
                'COUTREVIENT': '99999',
2078 2094
                'DATEDEBUT': '2019-08-01',
2079 2095
                'DATEENTREE': '2019-08-01',
......
2133 2149
    assert resp.json['data']['ACTIVITE'][3]['booking'] == {}
2134 2150

  
2135 2151

  
2152
def test_clae_booking_activities_info_endpoint_axel_error(app, resource):
2153
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
2154
    with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
2155
        operation.side_effect = AxelError('FooBar')
2156
        resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=3535&booking_date=2020-01-20')
2157
    assert resp.json['err_desc'] == "Axel error: FooBar"
2158
    assert resp.json['err'] == 'error'
2159

  
2160
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
2161
    with open(filepath) as xml:
2162
        content = xml.read()
2163
    with mock_getdata(content, 'EnfantsActivites'):
2164
        with mock.patch('passerelle.contrib.toulouse_axel.models.reservation_periode') as operation:
2165
            operation.side_effect = AxelError('FooBar')
2166
            resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=3535&booking_date=2020-01-20')
2167
    assert resp.json['err_desc'] == "Axel error: FooBar"
2168
    assert resp.json['err'] == 'error'
2169

  
2170

  
2171
@pytest.mark.parametrize('value', ['foo', '20/01/2020', '2020'])
2172
def test_clae_booking_activities_info_endpoint_bad_date_format(app, resource, value):
2173
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
2174
    resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=3535&booking_date=%s' % value, status=400)
2175
    assert resp.json['err_desc'] == "bad date format"
2176
    assert resp.json['err'] == 'bad-request'
2177

  
2178

  
2179
def test_clae_booking_activities_info_endpoint_no_result(app, resource, child_activities_data):
2180
    resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=3535&booking_date=2020-01-20')
2181
    assert resp.json['err_desc'] == "Person not found"
2182
    assert resp.json['err'] == 'not-found'
2183

  
2184
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
2185
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
2186
    with open(filepath) as xml:
2187
        content = xml.read()
2188
    with mock_getdata(content, 'EnfantsActivites'):
2189
        with mock.patch('passerelle.contrib.toulouse_axel.models.reservation_periode') as operation:
2190
            operation.side_effect = AxelError('FooBar')
2191
            resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=4242&booking_date=2020-01-20')
2192
    assert resp.json['err_desc'] == "Child not found"
2193
    assert resp.json['err'] == 'not-found'
2194

  
2195
    content = """<PORTAIL>
2196
  <DUI>
2197
    <IDDUI>XXX</IDDUI>
2198
    <ENFANT>
2199
      <IDPERSONNE>4242</IDPERSONNE>
2200
      <ACTIVITE>
2201
        <ANNEEREFERENCE>2019</ANNEEREFERENCE>
2202
        <IDACTIVITE>A19P1M1</IDACTIVITE>
2203
        <JOUR>00000</JOUR>
2204
      </ACTIVITE>
2205
    </ENFANT>
2206
  </DUI>
2207
</PORTAIL>"""
2208
    activities = child_activities_data['ENFANT'][0]
2209
    with mock_getdata(content, 'ReservationPeriode'):
2210
        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
2211
            resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=3535&booking_date=2020-01-20')
2212
    assert resp.json['err_desc'] == "Child not found"
2213
    assert resp.json['err'] == 'not-found'
2214

  
2215

  
2216
def test_clae_booking_activities_info_endpoint(app, resource, child_activities_data):
2217
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
2218
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/booking_info.xml')
2219
    with open(filepath) as xml:
2220
        content = xml.read()
2221
    activities = child_activities_data['ENFANT'][0]
2222
    with mock_getdata(content, 'ReservationPeriode'):
2223
        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
2224
            resp = app.get('/toulouse-axel/test/clae_booking_activities_info?NameID=yyy&idpersonne=3535&booking_date=2020-01-20')
2225
    assert resp.json['err'] == 0
2226
    assert resp.json['data'] == [
2227
        {
2228
            'id': 'A19P1M1',
2229
            'text': 'Temps du matin (inscription du 01/08/2019 - 31/07/2020)',
2230
            'annee_reference': 2019,
2231
            'annee_reference_label': '2019/2020',
2232
            'COUTREVIENT': '99999',
2233
            'DATEDEBUT': '2019-08-01',
2234
            'DATEENTREE': '2019-08-01',
2235
            'DATEFIN': '2020-07-31',
2236
            'DATESORTIE': '2020-07-31',
2237
            'IDACTIVITE': 'A19P1M1',
2238
            'ISPAI': False,
2239
            'LIBELLEACTIVITE': 'Temps du matin',
2240
            'TARIF': '0.42',
2241
            'TYPEACTIVITE': 'MAT',
2242
            'booking': {
2243
                'days': {
2244
                    'friday': False,
2245
                    'monday': False,
2246
                    'thursday': False,
2247
                    'tuesday': False,
2248
                    'wednesday': False
2249
                },
2250
                'raw_value': '00000'
2251
            }
2252
        },
2253
        {
2254
            'id': 'A19P1M2',
2255
            'text': 'Temps du midi (inscription du 01/08/2019 - 31/07/2020)',
2256
            'annee_reference': 2019,
2257
            'annee_reference_label': '2019/2020',
2258
            'COUTREVIENT': '99999',
2259
            'DATEDEBUT': '2019-08-01',
2260
            'DATEENTREE': '2019-08-01',
2261
            'DATEFIN': '2020-07-31',
2262
            'DATESORTIE': '2020-07-31',
2263
            'IDACTIVITE': 'A19P1M2',
2264
            'ISPAI': False,
2265
            'LIBELLEACTIVITE': 'Temps du midi',
2266
            'TARIF': '0.43',
2267
            'TYPEACTIVITE': 'MIDI',
2268
            'booking': {
2269
                'days': {
2270
                    'friday': True,
2271
                    'monday': True,
2272
                    'thursday': True,
2273
                    'tuesday': True,
2274
                    'wednesday': True
2275
                },
2276
                'raw_value': '11111'
2277
            }
2278
        },
2279
        {
2280
            'id': 'A19P1M3',
2281
            'text': 'Temps du soir (inscription du 01/08/2019 - 31/07/2020)',
2282
            'annee_reference': 2019,
2283
            'annee_reference_label': '2019/2020',
2284
            'COUTREVIENT': '99999',
2285
            'DATEDEBUT': '2019-08-01',
2286
            'DATEENTREE': '2019-08-01',
2287
            'DATEFIN': '2020-07-31',
2288
            'DATESORTIE': '2020-07-31',
2289
            'IDACTIVITE': 'A19P1M3',
2290
            'ISPAI': False,
2291
            'LIBELLEACTIVITE': 'Temps du soir',
2292
            'TARIF': '0.44',
2293
            'TYPEACTIVITE': 'SOIR',
2294
            'booking': {
2295
                'days': {
2296
                    'friday': True,
2297
                    'monday': True,
2298
                    'thursday': True,
2299
                    'tuesday': True,
2300
                    'wednesday': None
2301
                },
2302
                'raw_value': '11211'
2303
            }
2304
        },
2305
        {
2306
            'id': 'A19P1M4',
2307
            'text': u'Temps mercredi après midi (inscription du 01/08/2019 - 31/07/2020)',
2308
            'annee_reference': 2019,
2309
            'annee_reference_label': '2019/2020',
2310
            'COUTREVIENT': '99999',
2311
            'DATEDEBUT': '2019-08-01',
2312
            'DATEENTREE': '2019-08-01',
2313
            'DATEFIN': '2020-07-31',
2314
            'DATESORTIE': '2020-07-31',
2315
            'IDACTIVITE': 'A19P1M4',
2316
            'ISPAI': False,
2317
            'LIBELLEACTIVITE': u'Temps mercredi après midi',
2318
            'TARIF': '0.45',
2319
            'TYPEACTIVITE': 'GARD',
2320
            'booking': {
2321
                'days': {
2322
                    'friday': None,
2323
                    'monday': None,
2324
                    'thursday': None,
2325
                    'tuesday': None,
2326
                    'wednesday': True
2327
                },
2328
                'raw_value': '22122'
2329
            }
2330
        }
2331
    ]
2332

  
2333

  
2136 2334
def test_are_children_registered_axel_error(resource):
2137 2335
    with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
2138 2336
        operation.side_effect = AxelError('FooBar')
2139
-