generated from NHSDigital/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathappointment_presenters.py
More file actions
213 lines (171 loc) · 6.65 KB
/
appointment_presenters.py
File metadata and controls
213 lines (171 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from functools import cached_property
from django.urls import reverse
from manage_breast_screening.auth.models import Permission
from manage_breast_screening.mammograms.services.appointment_services import (
AppointmentStatusUpdater,
)
from ...core.utils.date_formatting import format_date, format_relative_date, format_time
from ...participants.models import AppointmentNote, AppointmentStatus, SupportReasons
from ...participants.presenters import ParticipantPresenter, status_colour
class AppointmentPresenter:
def __init__(self, appointment, tab_description="Appointment details"):
self._appointment = appointment
self.tab_description = tab_description
self.allStatuses = AppointmentStatus
self.pk = appointment.pk
self.clinic_slot = ClinicSlotPresenter(appointment.clinic_slot)
self.participant = ParticipantPresenter(
appointment.screening_episode.participant
)
self.screening_protocol = appointment.screening_episode.get_protocol_display()
self.special_appointment = (
SpecialAppointmentPresenter(self.participant.extra_needs, self.pk)
if self.is_special_appointment
else None
)
@cached_property
def participant_url(self):
return self.participant.url
@cached_property
def clinic_url(self):
return self.clinic_slot.clinic_url
@cached_property
def special_appointment_url(self):
return reverse(
"mammograms:provide_special_appointment_details",
kwargs={"pk": self._appointment.pk},
)
@cached_property
def caption(self):
return f"{self.clinic_slot.clinic_type} appointment"
@cached_property
def page_title(self):
return f"{self.caption}: {self.tab_description}"
@cached_property
def start_time(self):
return self.clinic_slot.starts_at
@cached_property
def is_special_appointment(self):
return bool(self.participant.extra_needs)
@cached_property
def can_be_made_special(self):
return not self.is_special_appointment and self._appointment.active
@cached_property
def can_be_checked_in(self):
return self._appointment.current_status.name == AppointmentStatus.CONFIRMED
@cached_property
def active(self):
return self._appointment.active
def can_be_started_by(self, user):
return user.has_perm(
Permission.START_MAMMOGRAM_APPOINTMENT, self._appointment
) and AppointmentStatusUpdater.is_startable(self._appointment)
@cached_property
def special_appointment_tag_properties(self):
return {
"classes": "nhsuk-tag--yellow nhsuk-u-margin-top-2",
"text": "Special appointment",
}
@cached_property
def current_status(self):
current_status = self._appointment.current_status
colour = status_colour(current_status)
display_text = (
"In progress"
if current_status.is_in_progress()
else current_status.get_name_display()
)
return {
"classes": (
f"nhsuk-tag--{colour} app-u-nowrap" if colour else "app-u-nowrap"
),
"text": display_text,
"key": current_status.name,
"is_confirmed": current_status.name == AppointmentStatus.CONFIRMED,
"is_screened": current_status.name == AppointmentStatus.SCREENED,
}
@cached_property
def status_attribution(self):
if self._appointment.current_status.is_in_progress():
return (
"with " + self._appointment.current_status.created_by.get_short_name()
)
elif self._appointment.current_status.is_final_status():
return "by " + self._appointment.current_status.created_by.get_short_name()
else:
return None
@cached_property
def note(self):
try:
return self._appointment.note
except AppointmentNote.DoesNotExist:
return None
@cached_property
def status_bar(self):
return StatusBarPresenter(self)
class StatusBarPresenter:
def __init__(self, appointment):
self.appointment = appointment
self.clinic_slot = appointment.clinic_slot
self.participant = appointment.participant
def show_status_bar_for(self, user):
# The appointment status bar should only display if the current user is the one that has the appointment 'in progress'
current_status = self.appointment._appointment.current_status
return (
current_status.is_in_progress()
and user.nhs_uid == current_status.created_by.nhs_uid
)
@property
def tag_properties(self):
return {
"classes": "nhsuk-tag--yellow nhsuk-u-margin-left-1",
"text": "Special appointment",
}
class ClinicSlotPresenter:
def __init__(self, clinic_slot):
self._clinic_slot = clinic_slot
self._clinic = clinic_slot.clinic
self.clinic_id = self._clinic.id
@cached_property
def clinic_type(self):
return self._clinic.get_type_display().capitalize()
@cached_property
def clinic_url(self):
return reverse("clinics:show", kwargs={"pk": self._clinic.pk})
@cached_property
def slot_time_and_clinic_date(self):
clinic_slot = self._clinic_slot
clinic = self._clinic
return f"{format_time(clinic_slot.starts_at)} ({clinic_slot.duration_in_minutes} minutes) - {format_date(clinic.starts_at)} ({format_relative_date(clinic.starts_at)})"
@cached_property
def clinic_date_and_slot_time(self):
clinic_slot = self._clinic_slot
clinic = self._clinic
return (
f"{format_date(clinic.starts_at)} at {format_time(clinic_slot.starts_at)}"
)
@cached_property
def starts_at(self):
return format_time(self._clinic_slot.starts_at)
class SpecialAppointmentPresenter:
def __init__(self, extra_needs, appointment_pk):
self._extra_needs = extra_needs
self._appointment_pk = appointment_pk
@cached_property
def reasons(self):
result = []
for reason, reason_details in self._extra_needs.items():
result.append(
{
"label": SupportReasons[reason].label,
"temporary": reason_details.get("temporary"),
"details": reason_details.get("details"),
}
)
return result
@cached_property
def change_url(self):
return reverse(
"mammograms:provide_special_appointment_details",
kwargs={"pk": self._appointment_pk},
)