toulouse-axel: invalidate caches after boookings (#43815)

This commit is contained in:
Lauréline Guérin 2020-06-09 17:22:23 +02:00
parent f67455c390
commit e2c4447266
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 45 additions and 3 deletions

View File

@ -1176,10 +1176,12 @@ class ToulouseAxel(BaseResource):
# find the first week from start_date
week_start_date, week_end_date = utils.get_week_dates_from_date(start_date)
# cross all weeks until the last week of the posted period
booking_dates = set()
while week_start_date <= end_date:
for activity_type, activity in activities_by_type.items():
real_start_date = max(start_date, week_start_date)
real_end_date = min(end_date, week_end_date)
booking_dates.add(real_start_date)
activity_id = activity['IDACTIVITE']
week_pattern = get_week_pattern(real_start_date, real_end_date, activity_type, activity_id)
activity['PERIODE'].append({
@ -1216,6 +1218,16 @@ class ToulouseAxel(BaseResource):
data={'xml_request': e.xml_request,
'xml_response': e.xml_response})
# invalidate caches
# invalidate get_children_activities cache
cache_key = 'toulouse-axel-%s-children-activities-%s-%s' % (self.pk, link.dui, reference_year)
cache.delete(cache_key)
for booking_date in sorted(booking_dates):
# invalidate get_booking_data cache for each week crossed
start_date, end_date = utils.get_week_dates_from_date(booking_date)
cache_key = 'toulouse-axel-%s-booking-data-%s-%s-%s' % (self.pk, link.dui, post_data['child_id'], start_date.isoformat())
cache.delete(cache_key)
return {
'updated': True,
'data': {
@ -1303,6 +1315,17 @@ class ToulouseAxel(BaseResource):
data={'xml_request': e.xml_request,
'xml_response': e.xml_response})
# invalidate cache
# invalidate get_children_activities cache
cache_key = 'toulouse-axel-%s-children-activities-%s-%s' % (self.pk, link.dui, reference_year)
cache.delete(cache_key)
booking_date = utils.get_week_dates_from_date(start_date)[0]
while booking_date <= end_date:
# invalidate get_booking_data cache for each monday from now to the end of the reference year
cache_key = 'toulouse-axel-%s-booking-data-%s-%s-%s' % (self.pk, link.dui, post_data['child_id'], booking_date.isoformat())
cache.delete(cache_key)
booking_date += datetime.timedelta(days=7)
return {
'updated': True,
'data': {

View File

@ -3231,7 +3231,12 @@ def test_clae_booking_endpoint(app, resource, booking_params, child_activities_d
with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
content = "<PORTAIL/>"
with mock_getdata(content, 'ReservationAnnuelle'):
resp = app.post_json('/toulouse-axel/test/clae_booking?NameID=yyy', params=booking_params)
with mock.patch('django.core.cache.cache.delete') as mock_cache_delete:
resp = app.post_json('/toulouse-axel/test/clae_booking?NameID=yyy', params=booking_params)
assert mock_cache_delete.call_args_list == [
mock.call('toulouse-axel-%s-children-activities-XXX-2019' % resource.pk),
mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-04-13' % resource.pk),
]
assert resp.json['err'] == 0
assert resp.json['updated'] is True
assert 'data' in resp.json
@ -3357,7 +3362,16 @@ def test_clae_booking_endpoint(app, resource, booking_params, child_activities_d
new_booking_params['booking_list_GARD'] = None
with mock.patch('passerelle.contrib.toulouse_axel.schemas.reservation_annuelle') as operation:
operation.return_value = schemas.OperationResult(json_response={}, xml_request='', xml_response='')
resp = app.post_json('/toulouse-axel/test/clae_booking?NameID=yyy', params=new_booking_params)
with mock.patch('django.core.cache.cache.delete') as mock_cache_delete:
resp = app.post_json('/toulouse-axel/test/clae_booking?NameID=yyy', params=new_booking_params)
assert mock_cache_delete.call_args_list == [
mock.call('toulouse-axel-%s-children-activities-XXX-2019' % resource.pk),
mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-03-30' % resource.pk),
mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-04-06' % resource.pk),
mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-04-13' % resource.pk),
mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-04-20' % resource.pk),
mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-04-27' % resource.pk),
]
payload = operation.call_args_list[0][0][1]['PORTAIL']['DUI']
assert len(payload['ENFANT']) == 1
assert payload['ENFANT'][0]['IDPERSONNE'] == '3535'
@ -3440,7 +3454,12 @@ def test_clae_booking_annual_endpoint(app, resource, annual_booking_params, chil
with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
content = "<PORTAIL/>"
with mock_getdata(content, 'ReservationAnnuelle'):
resp = app.post_json('/toulouse-axel/test/clae_booking_annual?NameID=yyy', params=annual_booking_params)
with mock.patch('django.core.cache.cache.delete') as mock_cache_delete:
resp = app.post_json('/toulouse-axel/test/clae_booking_annual?NameID=yyy', params=annual_booking_params)
assert len(mock_cache_delete.call_args_list) == 48
assert mock_cache_delete.call_args_list[0] == mock.call('toulouse-axel-%s-children-activities-XXX-2019' % resource.pk)
assert mock_cache_delete.call_args_list[1] == mock.call('toulouse-axel-%s-booking-data-XXX-3535-2019-09-09' % resource.pk)
assert mock_cache_delete.call_args_list[-1] == mock.call('toulouse-axel-%s-booking-data-XXX-3535-2020-07-27' % resource.pk)
assert resp.json['err'] == 0
assert resp.json['updated'] is True
assert 'data' in resp.json