From dab09c22bf7ed23e6392c1f1f8ffd5d12fd78005 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 20 Jan 2022 16:35:06 +0100 Subject: [PATCH 5/5] manager: account for time period weekday indexes in calendar views (#45159) --- chrono/agendas/models.py | 3 +++ chrono/manager/views.py | 8 ++++++- tests/manager/test_all.py | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index 8ee5d8be..f93f02cb 100644 --- a/chrono/agendas/models.py +++ b/chrono/agendas/models.py @@ -2176,7 +2176,10 @@ class Desk(models.Model): def get_opening_hours(self, date): openslots = IntervalSet() + weekday_index = get_weekday_index(date) for timeperiod in self.timeperiod_set.all(): + if timeperiod.weekday_indexes and weekday_index not in timeperiod.weekday_indexes: + continue # timeperiod_set.all() are prefetched, do not filter in queryset if timeperiod.weekday != date.weekday(): continue diff --git a/chrono/manager/views.py b/chrono/manager/views.py index 4b0f0bf2..7fd6a85a 100644 --- a/chrono/manager/views.py +++ b/chrono/manager/views.py @@ -78,6 +78,7 @@ from chrono.agendas.models import ( UnavailabilityCalendar, VirtualMember, ) +from chrono.utils.date import get_weekday_index from .forms import ( AbsenceReasonForm, @@ -1214,7 +1215,12 @@ class AgendaDayView(AgendaDateView, DayArchiveView): def get_timetable_infos(self): timeperiods = itertools.chain(*(d.timeperiod_set.all() for d in self.agenda.prefetched_desks)) - timeperiods = [t for t in timeperiods if t.weekday == self.date.weekday()] + timeperiods = [ + t + for t in timeperiods + if t.weekday == self.date.weekday() + and (not t.weekday_indexes or get_weekday_index(self.date) in t.weekday_indexes) + ] timeperiods = sorted(timeperiods, key=lambda t: t.start_time) interval = datetime.timedelta(minutes=60) diff --git a/tests/manager/test_all.py b/tests/manager/test_all.py index 2c5e2492..c5186e61 100644 --- a/tests/manager/test_all.py +++ b/tests/manager/test_all.py @@ -3037,3 +3037,52 @@ def test_agenda_booking_colors(app, admin_user, api_user, view): assert resp.text.count('Swimming') == 2 # 1 booking + legend assert 'Booking colors:' in resp.text assert len(resp.pyquery.find('div.booking-colors span.booking-color-label')) == 2 + + +@freezegun.freeze_time('2022-03-01 14:00') +def test_agenda_day_and_month_views_weekday_indexes(app, admin_user): + agenda = Agenda.objects.create(label='New Example', kind='meetings') + desk = Desk.objects.create(agenda=agenda, label='New Desk') + MeetingType.objects.create(agenda=agenda, label='Bar', duration=30) + today = datetime.date.today() + TimePeriod.objects.create( + desk=desk, + weekday=today.weekday(), + start_time=datetime.time(10, 0), + end_time=datetime.time(14, 0), + weekday_indexes=[1, 3], + ) + TimePeriod.objects.create( + desk=desk, + weekday=today.weekday(), + start_time=datetime.time(14, 0), + end_time=datetime.time(17, 0), + weekday_indexes=[3, 5], + ) + login(app) + + # check day view + resp = app.get('/manage/agendas/%s/%s/%s/%s/' % (agenda.pk, today.year, today.month, today.day)) + assert resp.text.count('14 + assert 'style="height: 400%; top: 0%;"' in resp.text + + resp = app.get('/manage/agendas/%s/%s/%s/%s/' % (agenda.pk, today.year, today.month, today.day + 7)) + assert 'No opening hours this day.' in resp.text + + resp = app.get('/manage/agendas/%s/%s/%s/%s/' % (agenda.pk, today.year, today.month, today.day + 14)) + assert resp.text.count('14, 14->17 + assert 'style="height: 700%; top: 0%;"' in resp.text + + resp = app.get('/manage/agendas/%s/%s/%s/%s/' % (agenda.pk, today.year, today.month, today.day + 21)) + assert 'No opening hours this day.' in resp.text + + resp = app.get('/manage/agendas/%s/%s/%s/%s/' % (agenda.pk, today.year, today.month, today.day + 28)) + assert resp.text.count('17 + assert 'style="height: 300%; top: 0%;"' in resp.text + + # check month view + resp = app.get('/manage/agendas/%s/%s/%s/' % (agenda.pk, today.year, today.month)) + assert resp.text.count('height:') == 3 + assert resp.text.count('height:400.0%') == 1 + assert resp.text.count('height:700.0%') == 1 + assert resp.text.count('height:300.0%') == 1 -- 2.30.2