manager: display excluded periods for virtual agendas in date views (#65074)

This commit is contained in:
Valentin Deniaud 2022-05-18 16:30:30 +02:00
parent 53b4072ab9
commit 0bceea5cad
2 changed files with 61 additions and 4 deletions

View File

@ -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:

View File

@ -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('<tr') == 9
assert 'exceptions-hours' not in resp.text
def test_virtual_agenda_month_view(app, admin_user):
agenda = Agenda.objects.create(label='Virtual', kind='virtual')
@ -2038,13 +2058,29 @@ def test_virtual_agenda_month_view(app, admin_user):
)
with CaptureQueriesContext(connection) as ctx:
resp = app.get('/manage/agendas/%s/%s/%s/' % (agenda.id, today.year, today.month))
assert len(ctx.captured_queries) == 12
assert len(ctx.captured_queries) == 13
assert len(resp.pyquery.find('.exception-hours')) == 1
assert resp.pyquery.find('.exception-hours')[0].attrib == {
'class': 'exception-hours',
'style': 'height:800.0%;top:0.0%;width:48.0%;left:1.0%;',
'title': 'Exception for a December day',
}
# display excluded period
TimePeriod.objects.create(
agenda=agenda, weekday=today.weekday(), start_time=datetime.time(13, 0), end_time=datetime.time(23, 0)
)
resp = app.get('/manage/agendas/%s/%s/%s/' % (agenda.id, today.year, today.month))
assert len(resp.pyquery.find('.exception-hours')) == 11 # five occurences on two desks
assert resp.pyquery.find('.exception-hours')[0].attrib == {
'class': 'exception-hours',
'style': 'height:500.0%;top:300.0%;width:48.0%;left:1.0%;',
}
assert resp.pyquery.find('.exception-hours')[1].attrib == {
'class': 'exception-hours',
'style': 'height:500.0%;top:300.0%;width:48.0%;left:50.0%;',
}
def test_virtual_agenda_settings_empty(app, admin_user):
agenda = Agenda.objects.create(label='My Virtual agenda', kind='virtual')