|
| 1 | +import re |
| 2 | + |
| 3 | +from django.urls import reverse |
| 4 | + |
| 5 | +from ..clinics.models import Appointment |
| 6 | +from ..utils.date_formatting import format_date, format_relative_date, format_time |
| 7 | +from ..utils.string_formatting import format_age, format_nhs_number, sentence_case |
| 8 | + |
| 9 | +Status = Appointment.Status |
| 10 | + |
| 11 | + |
| 12 | +def status_colour(status): |
| 13 | + """ |
| 14 | + Color to render the status tag |
| 15 | + """ |
| 16 | + match status: |
| 17 | + case Status.CHECKED_IN: |
| 18 | + return "" # no colour will get solid dark blue |
| 19 | + case Status.SCREENED: |
| 20 | + return "green" |
| 21 | + case Status.DID_NOT_ATTEND | Status.CANCELLED: |
| 22 | + return "red" |
| 23 | + case Status.ATTENDED_NOT_SCREENED | Status.PARTIALLY_SCREENED: |
| 24 | + return "orange" |
| 25 | + case _: |
| 26 | + return "blue" # default blue |
| 27 | + |
| 28 | + |
| 29 | +def present_secondary_nav(id): |
| 30 | + """ |
| 31 | + Build a secondary nav for reviewing the information of screened/partially screened appointments. |
| 32 | + """ |
| 33 | + return [ |
| 34 | + { |
| 35 | + "id": "all", |
| 36 | + "text": "Appointment details", |
| 37 | + "href": reverse("record_a_mammogram:start_screening", kwargs={"id": id}), |
| 38 | + "current": True, |
| 39 | + }, |
| 40 | + {"id": "medical_information", "text": "Medical information", "href": "#"}, |
| 41 | + {"id": "images", "text": "Images", "href": "#"}, |
| 42 | + ] |
| 43 | + |
| 44 | + |
| 45 | +class AppointmentPresenter: |
| 46 | + def __init__(self, appointment): |
| 47 | + self._appointment = appointment |
| 48 | + self._last_known_screening = appointment.screening_episode.previous() |
| 49 | + |
| 50 | + self.allStatuses = Status |
| 51 | + self.id = appointment.id |
| 52 | + self.clinic_slot = ClinicSlotPresenter(appointment.clinic_slot) |
| 53 | + self.participant = ParticipantPresenter( |
| 54 | + appointment.screening_episode.participant |
| 55 | + ) |
| 56 | + |
| 57 | + @property |
| 58 | + def status(self): |
| 59 | + colour = status_colour(self._appointment.status) |
| 60 | + |
| 61 | + return { |
| 62 | + "classes": f"nhsuk-tag--{colour} app-nowrap" if colour else "app-nowrap", |
| 63 | + "text": self._appointment.get_status_display(), |
| 64 | + "key": self._appointment.status, |
| 65 | + "is_confirmed": self._appointment.status == Status.CONFIRMED, |
| 66 | + } |
| 67 | + |
| 68 | + @property |
| 69 | + def last_known_screening(self): |
| 70 | + return ( |
| 71 | + { |
| 72 | + "date": format_date(self._last_known_screening.created_at), |
| 73 | + "relative_date": format_relative_date( |
| 74 | + self._last_known_screening.created_at |
| 75 | + ), |
| 76 | + # TODO: the current model doesn't allow for knowing the type and location of a historical screening |
| 77 | + # if it is not tied to one of our clinic slots. |
| 78 | + "location": None, |
| 79 | + "type": None, |
| 80 | + } |
| 81 | + if self._last_known_screening |
| 82 | + else {} |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +class ClinicSlotPresenter: |
| 87 | + def __init__(self, clinic_slot): |
| 88 | + self._clinic_slot = clinic_slot |
| 89 | + self._clinic = clinic_slot.clinic |
| 90 | + |
| 91 | + self.clinic_id = self._clinic.id |
| 92 | + |
| 93 | + @property |
| 94 | + def clinic_type(self): |
| 95 | + return self._clinic.get_type_display().capitalize() |
| 96 | + |
| 97 | + @property |
| 98 | + def slot_time_and_clinic_date(self): |
| 99 | + clinic_slot = self._clinic_slot |
| 100 | + clinic = self._clinic |
| 101 | + |
| 102 | + 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) })" |
| 103 | + |
| 104 | + |
| 105 | +class ParticipantPresenter: |
| 106 | + def __init__(self, participant): |
| 107 | + self._participant = participant |
| 108 | + |
| 109 | + self.extra_needs = participant.extra_needs |
| 110 | + self.ethnic_group = participant.ethnic_group |
| 111 | + self.full_name = participant.full_name |
| 112 | + self.nhs_number = format_nhs_number(participant.nhs_number) |
| 113 | + self.date_of_birth = format_date(participant.date_of_birth) |
| 114 | + self.age = format_age(participant.age()) |
| 115 | + self.risk_level = sentence_case(participant.risk_level) |
| 116 | + |
| 117 | + @property |
| 118 | + def ethnic_group_category(self): |
| 119 | + category = self._participant.ethnic_group_category() |
| 120 | + if category: |
| 121 | + return category.replace("Any other", "any other") |
| 122 | + else: |
| 123 | + return None |
| 124 | + |
| 125 | + @property |
| 126 | + def address(self): |
| 127 | + address = self._participant.address |
| 128 | + if not address: |
| 129 | + return {} |
| 130 | + |
| 131 | + return {"lines": address.lines, "postcode": address.postcode} |
0 commit comments