api: the last day of a subscription is not bookable (#61573)

This commit is contained in:
Lauréline Guérin 2022-02-14 16:05:23 +01:00
parent 282da0a2de
commit 9d53004c8c
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 32 additions and 13 deletions

View File

@ -943,8 +943,8 @@ class Agenda(models.Model):
if show_only_subscribed:
event_queryset = event_queryset.filter(
agenda__subscriptions__user_external_id=user_external_id,
agenda__subscriptions__date_start__lt=F('start_datetime'),
agenda__subscriptions__date_end__gt=F('start_datetime') - datetime.timedelta(days=1),
agenda__subscriptions__date_start__lte=F('start_datetime'),
agenda__subscriptions__date_end__gt=F('start_datetime'),
)
exceptions_desk = Desk.objects.filter(slug='_exceptions_holder').prefetch_related(

View File

@ -1727,9 +1727,8 @@ class RecurringFillslots(APIView):
lookups.update(
{
'agenda__subscriptions__user_external_id': user_external_id,
'agenda__subscriptions__date_start__lt': F('start_datetime'),
'agenda__subscriptions__date_end__gt': F('start_datetime')
- datetime.timedelta(days=1),
'agenda__subscriptions__date_start__lte': F('start_datetime'),
'agenda__subscriptions__date_end__gt': F('start_datetime'),
}
)
event_filter |= Q(**lookups)
@ -1917,8 +1916,8 @@ class MultipleAgendasEventsFillslots(EventsFillslots):
events_outside_subscriptions = events.difference(
events.filter(
agenda__subscriptions__user_external_id=payload['user_external_id'],
agenda__subscriptions__date_start__lt=F('start_datetime'),
agenda__subscriptions__date_end__gt=F('start_datetime') - datetime.timedelta(days=1),
agenda__subscriptions__date_start__lte=F('start_datetime'),
agenda__subscriptions__date_end__gt=F('start_datetime'),
)
) # workaround exclude method bug https://code.djangoproject.com/ticket/29697
if events_outside_subscriptions.exists():

View File

@ -2058,13 +2058,17 @@ def test_datetimes_multiple_agendas_subscribed(app):
assert len(resp.json['data']) == 0
# add subscription to first agenda
Subscription.objects.create(
subscription = Subscription.objects.create(
agenda=first_agenda,
user_external_id='xxx',
date_start=now(),
date_end=now() + datetime.timedelta(days=5), # first event on subscription's last day
date_end=now() + datetime.timedelta(days=5), # first event after last day
)
resp = app.get('/api/agendas/datetimes/', params={'subscribed': 'all', 'user_external_id': 'xxx'})
assert len(resp.json['data']) == 0
subscription.date_end = now() + datetime.timedelta(days=6) # first event on subscription's last day
subscription.save()
resp = app.get('/api/agendas/datetimes/', params={'subscribed': 'all', 'user_external_id': 'xxx'})
assert len(resp.json['data']) == 1
assert resp.json['data'][0]['id'] == 'first-agenda@event'

View File

@ -3207,7 +3207,7 @@ def test_recurring_events_api_fillslots_subscribed(app, user):
)
sunday_event.create_all_recurrences()
Subscription.objects.create(
subscription = Subscription.objects.create(
agenda=first_agenda,
user_external_id='xxx',
date_start=now() + datetime.timedelta(days=14), # Monday 20/09
@ -3220,7 +3220,17 @@ def test_recurring_events_api_fillslots_subscribed(app, user):
# book Monday and Thursday of first event, in subscription range
params['slots'] = 'first-agenda@event:0,first-agenda@event:3'
resp = app.post_json(fillslots_url % 'category-a', params=params)
assert resp.json['booking_count'] == 10
assert resp.json['booking_count'] == 9
assert Booking.objects.count() == 9
assert Booking.objects.filter(event__primary_event=event).count() == 9
assert (
Booking.objects.first().event.start_datetime.strftime('%d/%m') == '20/09'
) # first subscription's day
assert Booking.objects.last().event.start_datetime.strftime('%d/%m') == '18/10' # last subscription's day
subscription.date_end = now() + datetime.timedelta(days=46) # Friday 22/10
subscription.save()
resp = app.post_json(fillslots_url % 'category-a', params=params)
assert resp.json['booking_count'] == 1
assert Booking.objects.count() == 10
assert Booking.objects.filter(event__primary_event=event).count() == 10
assert (
@ -4084,12 +4094,15 @@ def test_api_events_fillslots_multiple_agendas_subscribed(app, user):
agenda=agenda,
user_external_id='xxx',
date_start=now(),
date_end=now() + datetime.timedelta(days=10), # first event on the last subscription's day
date_end=now() + datetime.timedelta(days=10), # too soon
)
# book events
app.authorization = ('Basic', ('john.doe', 'password'))
params = {'user_external_id': 'xxx', 'slots': 'first-agenda@event,second-agenda@event'}
resp = app.post_json('/api/agendas/events/fillslots/?subscribed=category-a', params=params, status=400)
# first event on the last subscription's day
Subscription.objects.all().update(date_end=now() + datetime.timedelta(days=11))
resp = app.post_json('/api/agendas/events/fillslots/?subscribed=category-a', params=params)
assert resp.json['booking_count'] == 2
assert (
@ -4135,13 +4148,16 @@ def test_api_events_fillslots_multiple_agendas_subscribed(app, user):
)
# add subscription to third agenda
Subscription.objects.create(
subscription = Subscription.objects.create(
agenda=third_agenda,
user_external_id='xxx',
date_start=now(),
date_end=now() + datetime.timedelta(days=10),
)
params = {'user_external_id': 'xxx', 'slots': 'third-agenda@event'}
resp = app.post_json('/api/agendas/events/fillslots/?subscribed=category-b', params=params, status=400)
subscription.date_end = now() + datetime.timedelta(days=11)
subscription.save()
resp = app.post_json('/api/agendas/events/fillslots/?subscribed=category-b', params=params)
assert resp.json['booking_count'] == 1
assert (