|
| 1 | +from abc import ABC, abstractmethod |
1 | 2 | from datetime import date, timedelta |
2 | 3 | from typing import Any |
3 | 4 |
|
4 | 5 | from django.db.models import Q |
| 6 | +from django.urls import reverse |
5 | 7 | from django.utils.translation import gettext_lazy as _ |
6 | 8 |
|
7 | 9 | from shiftings.cal.views.calendar_base import CalendarBaseView |
8 | 10 | from shiftings.shifts.forms.participant import AddSelfParticipantForm |
9 | | -from shiftings.shifts.models import Shift |
| 11 | +from shiftings.shifts.models import Shift, ShiftType |
10 | 12 |
|
11 | 13 |
|
12 | | -class DayView(CalendarBaseView): |
| 14 | +class DayView(CalendarBaseView, ABC): |
13 | 15 | template_name = 'cal/day_calendar.html' |
14 | 16 | save_path_in_session = True |
| 17 | + url_name_suffix = '' |
15 | 18 |
|
16 | 19 | def get_title(self) -> str: |
17 | 20 | if 'theday' in self.kwargs: |
18 | 21 | return _('Day {date}').format(date=self.kwargs.get('theday')) |
19 | 22 | return _('Day Overview') |
20 | 23 |
|
| 24 | + @abstractmethod |
| 25 | + def get_shifts(self, theday: date) -> Any: |
| 26 | + raise NotImplementedError('get_shifts needs to be implemented') |
| 27 | + |
21 | 28 | def get_context_data(self, **kwargs: Any) -> dict[str, Any]: |
22 | 29 | context = super().get_context_data(**kwargs) |
23 | 30 | theday = date.fromisoformat(self.kwargs.get('theday')) if 'theday' in self.kwargs else date.today() |
24 | | - shift_filter = (Q(start__date=theday) | Q(end__date=theday, end__gt=theday) | |
25 | | - Q(start__lt=theday, end__gt=theday)) |
26 | | - shift_filter &= self.get_filters() |
27 | | - shifts = Shift.objects.filter(shift_filter).order_by('start', 'end', 'shift_type') |
28 | 31 | context.update({ |
29 | 32 | 'theday': theday, |
30 | | - 'nextday': theday + timedelta(days=1), |
31 | | - 'prevday': theday - timedelta(days=1), |
| 33 | + 'next_url': reverse('overview_day' + self.url_name_suffix, args=[theday + timedelta(days=1)]), |
| 34 | + 'prev_url': reverse('overview_day' + self.url_name_suffix, args=[theday - timedelta(days=1)]), |
| 35 | + 'today_url': reverse('overview_today' + self.url_name_suffix), |
32 | 36 | 'day_hours': list(range(24)), |
33 | | - 'shifts': [shift for shift in shifts if shift.can_see(self.request.user)], |
| 37 | + 'shifts': self.get_shifts(theday), |
34 | 38 | 'add_self_form': AddSelfParticipantForm(self.object, initial={'user': self.request.user}), |
35 | 39 | }) |
36 | 40 | return context |
| 41 | + |
| 42 | + |
| 43 | +class DetailDayView(DayView): |
| 44 | + extra_context = { |
| 45 | + 'day_calendar_template': 'cal/template/day_calendar_xs.html' |
| 46 | + } |
| 47 | + |
| 48 | + def get_shifts(self, theday: date) -> Any: |
| 49 | + shift_filter = (Q(start__date=theday) | Q(end__date=theday, end__gt=theday) | |
| 50 | + Q(start__lt=theday, end__gt=theday)) |
| 51 | + shift_filter &= self.get_filters() |
| 52 | + shifts = Shift.objects.filter(shift_filter).order_by('start', 'end', 'shift_type') |
| 53 | + return [shift for shift in shifts if shift.can_see(self.request.user)] |
| 54 | + |
| 55 | + |
| 56 | +class ShiftTypesDayView(DayView): |
| 57 | + extra_context = { |
| 58 | + 'day_calendar_template': 'cal/template/day_calendar_shift_types.html' |
| 59 | + } |
| 60 | + url_name_suffix = '_shift_types' |
| 61 | + |
| 62 | + def get_shifts(self, theday: date): |
| 63 | + shift_filter = Q(start__date=theday) & self.get_filters() |
| 64 | + shifts = Shift.objects.filter(shift_filter).order_by('start', 'shift_type') |
| 65 | + shifts = [shift for shift in shifts if shift.can_see(self.request.user)] |
| 66 | + types = ShiftType.objects.filter(shift__in=shifts).distinct() |
| 67 | + shift_idx_type = { |
| 68 | + 'types': types, |
| 69 | + 'time_containers': {} |
| 70 | + } |
| 71 | + for shift in shifts: |
| 72 | + if shift.can_see(self.request.user): |
| 73 | + shift_idx_type['time_containers'].setdefault(shift.start.hour, {}).setdefault(shift.shift_type.name, []).append( |
| 74 | + shift |
| 75 | + ) |
| 76 | + return shift_idx_type |
0 commit comments