From 0bceea5cadcfa6be4516375327b413daa9b2dd6a Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 18 May 2022 16:30:30 +0200 Subject: [PATCH] manager: display excluded periods for virtual agendas in date views (#65074) --- chrono/manager/views.py | 25 ++++++++++++++++++++++-- tests/manager/test_all.py | 40 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/chrono/manager/views.py b/chrono/manager/views.py index 73e079f1..ebe61881 100644 --- a/chrono/manager/views.py +++ b/chrono/manager/views.py @@ -1347,6 +1347,11 @@ class AgendaDateView(DateMixin, ViewableAgendaMixin): allow_empty = True allow_future = True + def set_agenda(self, **kwargs): + super().set_agenda(**kwargs) + if self.agenda.kind == 'virtual': + self.agenda._excluded_timeperiods = self.agenda.excluded_timeperiods.all() + def dispatch(self, request, *args, **kwargs): # specify 6am time to get the expected timezone on daylight saving time # days. @@ -1404,6 +1409,16 @@ class AgendaDateView(DateMixin, ViewableAgendaMixin): ) return queryset + def get_exceptions_from_excluded_periods(self, date): + return [ + TimePeriodException( + start_datetime=make_aware(datetime.datetime.combine(date, period.start_time)), + end_datetime=make_aware(datetime.datetime.combine(date, period.end_time)), + ) + for period in getattr(self.agenda, '_excluded_timeperiods', []) + if period.weekday == date.weekday() + ] + class AgendaDayView(AgendaDateView, DayArchiveView): def get_queryset(self): @@ -1494,8 +1509,11 @@ class AgendaDayView(AgendaDateView, DayArchiveView): ) # and exceptions for this desk + exceptions = desk.prefetched_exceptions + self.get_exceptions_from_excluded_periods( + current_date.date() + ) info['exceptions'] = [] - for exception in desk.prefetched_exceptions: + for exception in exceptions: if exception.end_datetime < current_date: continue if exception.start_datetime > max_date: @@ -1710,7 +1728,10 @@ class AgendaMonthView(AgendaDateView, MonthArchiveView): 'css_left': left, } ) - for exception in desk.prefetched_exceptions: + exceptions = desk.prefetched_exceptions + self.get_exceptions_from_excluded_periods( + current_date.date() + ) + for exception in exceptions: if exception.end_datetime < current_date: continue if exception.start_datetime > max_date: diff --git a/tests/manager/test_all.py b/tests/manager/test_all.py index 419cd5d7..ad1cacda 100644 --- a/tests/manager/test_all.py +++ b/tests/manager/test_all.py @@ -1940,7 +1940,7 @@ def test_virtual_agenda_day_view(app, admin_user, manager_user): resp = app.get( '/manage/agendas/%s/%d/%d/%d/' % (agenda.id, date.year, date.month, date.day), status=200 ) - assert len(ctx.captured_queries) == 15 + assert len(ctx.captured_queries) == 16 # day is displaying rows from 10am to 6pm, # opening hours, 10am to 1pm gives top: 300% # rest of the day, 1pm to 6(+1)pm gives 600% @@ -1950,6 +1950,26 @@ def test_virtual_agenda_day_view(app, admin_user, manager_user): } assert resp.pyquery.find('.exception-hours span')[0].text == 'Exception for the afternoon' + # display excluded period + date += datetime.timedelta(days=7) + TimePeriod.objects.create( + agenda=agenda, weekday=today.weekday(), start_time=datetime.time(14, 0), end_time=datetime.time(23, 0) + ) + resp = app.get('/manage/agendas/%s/%d/%d/%d/' % (agenda.id, date.year, date.month, date.day), status=200) + assert resp.pyquery.find('.exception-hours')[0].attrib == { + 'class': 'exception-hours', + 'style': 'height: 500%; top: 400%;', + } + + # check excluded period is only displayed on relevant weekday + date += datetime.timedelta(days=1) + TimePeriod.objects.create( + desk=desk1, weekday=date.weekday(), start_time=datetime.time(10, 0), end_time=datetime.time(18, 0) + ) + resp = app.get('/manage/agendas/%s/%d/%d/%d/' % (agenda.id, date.year, date.month, date.day), status=200) + assert resp.text.count('