From 9bb29e2b1809de28120450b626c4995c9c98da9e 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/agendas/models.py | 10 ++++++++++ chrono/manager/views.py | 17 +++++++++++++++-- tests/manager/test_all.py | 40 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index 016dd4aa..ffcb70ce 100644 --- a/chrono/agendas/models.py +++ b/chrono/agendas/models.py @@ -1022,6 +1022,16 @@ class Agenda(models.Model): return True + 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, '_excluded_timeperiods', []) + if period.weekday == date.weekday() + ] + class VirtualMember(models.Model): """Trough model to link virtual agendas to their real agendas. diff --git a/chrono/manager/views.py b/chrono/manager/views.py index 73e079f1..fa2702e2 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. @@ -1494,8 +1499,12 @@ class AgendaDayView(AgendaDateView, DayArchiveView): ) # and exceptions for this desk + exceptions = ( + desk.prefetched_exceptions + + self.agenda.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 +1719,11 @@ class AgendaMonthView(AgendaDateView, MonthArchiveView): 'css_left': left, } ) - for exception in desk.prefetched_exceptions: + exceptions = ( + desk.prefetched_exceptions + + self.agenda.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('