From f28031f5a2a632b727fa5ca0280f1f3c8883216e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Tue, 4 Apr 2023 16:14:53 +0200 Subject: [PATCH] api: fix datetimes for multiple agendas when no subscriptions & overlaps (#76217) --- chrono/api/views.py | 53 ++++++++++--------- .../datetimes/test_events_multiple_agendas.py | 7 +++ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/chrono/api/views.py b/chrono/api/views.py index af498ff0..d00ef7a6 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -934,35 +934,36 @@ class MultipleAgendasDatetimes(APIView): check_overlaps = bool(payload.get('check_overlaps')) entries = Event.objects.none() - for agenda in agendas: - if show_past_events: - entries |= agenda.get_past_events( + if agendas: + for agenda in agendas: + if show_past_events: + entries |= agenda.get_past_events( + min_start=payload.get('date_start'), + max_start=payload.get('date_end'), + ) + entries |= agenda.get_open_events( + min_start=payload.get('date_start'), + max_start=payload.get('date_end'), + bypass_delays=payload.get('bypass_delays'), + show_out_of_minimal_delay=show_past_events, + ) + entries = Event.annotate_queryset_for_user(entries, user_external_id, with_status=with_status) + if check_overlaps: + entries = Event.annotate_queryset_with_overlaps(entries) + if show_only_subscribed: + entries = entries.filter( + agenda__subscriptions__user_external_id=user_external_id, + agenda__subscriptions__date_start__lte=F('start_datetime'), + agenda__subscriptions__date_end__gt=F('start_datetime'), + ) + if guardian_external_id: + entries = Agenda.filter_for_guardian( + entries, + guardian_external_id, + user_external_id, min_start=payload.get('date_start'), max_start=payload.get('date_end'), ) - entries |= agenda.get_open_events( - min_start=payload.get('date_start'), - max_start=payload.get('date_end'), - bypass_delays=payload.get('bypass_delays'), - show_out_of_minimal_delay=show_past_events, - ) - entries = Event.annotate_queryset_for_user(entries, user_external_id, with_status=with_status) - if check_overlaps: - entries = Event.annotate_queryset_with_overlaps(entries) - if show_only_subscribed: - entries = entries.filter( - agenda__subscriptions__user_external_id=user_external_id, - agenda__subscriptions__date_start__lte=F('start_datetime'), - agenda__subscriptions__date_end__gt=F('start_datetime'), - ) - if guardian_external_id: - entries = Agenda.filter_for_guardian( - entries, - guardian_external_id, - user_external_id, - min_start=payload.get('date_start'), - max_start=payload.get('date_end'), - ) entries = list(entries) if 'agendas' in request.query_params: diff --git a/tests/api/datetimes/test_events_multiple_agendas.py b/tests/api/datetimes/test_events_multiple_agendas.py index 43d4a2c7..5a3ac405 100644 --- a/tests/api/datetimes/test_events_multiple_agendas.py +++ b/tests/api/datetimes/test_events_multiple_agendas.py @@ -1695,3 +1695,10 @@ def test_datetimes_multiple_agendas_overlapping_events(app): resp = app.get('/api/agendas/datetimes/', params={'agendas': 'foo-bar,foo-bar-2'}) assert ['overlaps' not in x for x in resp.json['data']] + + # no subscription found for this user + resp = app.get( + '/api/agendas/datetimes/', + params={'subscribed': 'all', 'user_external_id': 'xxx', 'check_overlaps': True}, + ) + assert len(resp.json['data']) == 0