Skip to content

Commit 4feaeaa

Browse files
authored
Merge pull request #799 from NHSDigital/DTOSS-11782-other-procedures
Add and update other procedure form
2 parents c0bbbbf + 049a4a5 commit 4feaeaa

File tree

12 files changed

+854
-6
lines changed

12 files changed

+854
-6
lines changed

manage_breast_screening/mammograms/forms/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .cyst_history_form import CystHistoryForm
55
from .implanted_medical_device_history_form import ImplantedMedicalDeviceHistoryForm
66
from .mastectomy_or_lumpectomy_history_form import MastectomyOrLumpectomyHistoryForm
7+
from .other_procedure_history_form import OtherProcedureHistoryForm
78
from .record_medical_information_form import RecordMedicalInformationForm
89
from .screening_appointment_form import ScreeningAppointmentForm
910
from .special_appointment_forms import (
@@ -22,4 +23,5 @@
2223
"MarkReasonsTemporaryForm",
2324
"MastectomyOrLumpectomyHistoryForm",
2425
"ImplantedMedicalDeviceHistoryForm",
26+
"OtherProcedureHistoryForm",
2527
]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from django.forms.widgets import Textarea
2+
3+
from manage_breast_screening.core.services.auditor import Auditor
4+
from manage_breast_screening.nhsuk_forms.fields import CharField, ChoiceField
5+
from manage_breast_screening.nhsuk_forms.fields.integer_field import YearField
6+
from manage_breast_screening.nhsuk_forms.forms import FormWithConditionalFields
7+
from manage_breast_screening.participants.models.other_procedure_history_item import (
8+
OtherProcedureHistoryItem,
9+
)
10+
11+
12+
class OtherProcedureHistoryForm(FormWithConditionalFields):
13+
PROCEDURE_DETAIL_FIELDS = {
14+
OtherProcedureHistoryItem.Procedure.BREAST_REDUCTION: "breast_reduction_details",
15+
OtherProcedureHistoryItem.Procedure.BREAST_SYMMETRISATION: "breast_symmetrisation_details",
16+
OtherProcedureHistoryItem.Procedure.NIPPLE_CORRECTION: "nipple_correction_details",
17+
OtherProcedureHistoryItem.Procedure.OTHER: "other_details",
18+
}
19+
20+
def __init__(self, *args, participant, **kwargs):
21+
self.instance = kwargs.pop("instance", None)
22+
23+
if self.instance:
24+
kwargs["initial"] = self.initial_values(self.instance)
25+
26+
super().__init__(*args, **kwargs)
27+
28+
self.fields["procedure"] = ChoiceField(
29+
choices=OtherProcedureHistoryItem.Procedure,
30+
label=f"What procedure has {participant.first_name} had?",
31+
error_messages={"required": "Select the procedure"},
32+
)
33+
34+
for procedure, detail_field in self.PROCEDURE_DETAIL_FIELDS.items():
35+
self.fields[detail_field] = CharField(
36+
required=False,
37+
label="Provide details",
38+
error_messages={"required": "Provide details of the procedure"},
39+
classes="nhsuk-u-width-two-thirds",
40+
)
41+
self.given_field_value("procedure", procedure).require_field(detail_field)
42+
43+
self.fields["procedure_year"] = YearField(
44+
label="Year of procedure (optional)",
45+
label_classes="nhsuk-label--m",
46+
classes="nhsuk-input--width-4",
47+
hint="Leave blank if unknown",
48+
required=False,
49+
)
50+
self.fields["additional_details"] = CharField(
51+
hint="Include any other relevant information about the device or procedure",
52+
required=False,
53+
label="Additional details (optional)",
54+
label_classes="nhsuk-label--m",
55+
widget=Textarea(attrs={"rows": 4}),
56+
max_words=500,
57+
error_messages={
58+
"max_words": "Additional details must be 500 words or less"
59+
},
60+
)
61+
62+
def initial_values(self, instance):
63+
initial = {
64+
"procedure": instance.procedure,
65+
"procedure_year": instance.procedure_year,
66+
"additional_details": instance.additional_details,
67+
}
68+
69+
detail_field = self.PROCEDURE_DETAIL_FIELDS.get(instance.procedure)
70+
if detail_field:
71+
initial[detail_field] = instance.procedure_details
72+
73+
return initial
74+
75+
def create(self, appointment, request):
76+
auditor = Auditor.from_request(request)
77+
78+
other_procedure_history_item = OtherProcedureHistoryItem.objects.create(
79+
appointment=appointment,
80+
procedure=self.cleaned_data["procedure"],
81+
procedure_details=self._get_selected_procedure_details(),
82+
procedure_year=self.cleaned_data.get("procedure_year"),
83+
additional_details=self.cleaned_data.get("additional_details", ""),
84+
)
85+
86+
auditor.audit_create(other_procedure_history_item)
87+
88+
return other_procedure_history_item
89+
90+
def update(self, request):
91+
self.instance.procedure = self.cleaned_data["procedure"]
92+
self.instance.procedure_details = self._get_selected_procedure_details()
93+
self.instance.procedure_year = self.cleaned_data["procedure_year"]
94+
self.instance.additional_details = self.cleaned_data["additional_details"]
95+
self.instance.save()
96+
97+
Auditor.from_request(request).audit_update(self.instance)
98+
99+
return self.instance
100+
101+
@property
102+
def procedure_detail_fields(self):
103+
return tuple(self.PROCEDURE_DETAIL_FIELDS.items())
104+
105+
def _get_selected_procedure_details(self):
106+
procedure = self.cleaned_data.get("procedure")
107+
try:
108+
detail_field = self.PROCEDURE_DETAIL_FIELDS[procedure]
109+
except KeyError as exc:
110+
msg = f"Unsupported procedure '{procedure}'"
111+
raise ValueError(msg) from exc
112+
return self.cleaned_data.get(detail_field, "")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% extends "layout-form.jinja" %}
2+
{% from "nhsuk/components/button/macro.jinja" import button %}
3+
{% from "nhsuk/components/fieldset/macro.jinja" import fieldset %}
4+
5+
{% block form %}
6+
{{ form.procedure.as_field_group() }}
7+
8+
{{ form.procedure_year.as_field_group() }}
9+
10+
{{ form.additional_details.as_field_group() }}
11+
12+
<div class="nhsuk-button-group">
13+
{{ button({
14+
"text": "Save"
15+
}) }}
16+
</div>
17+
{% endblock %}
18+

manage_breast_screening/mammograms/jinja2/mammograms/record_medical_information.jinja

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@
7575

7676
{% set other_procedure_history_html %}
7777
{% for presented_item in presenter.other_procedure_history %}
78+
<a style="float: right" class="nhsuk-link nhsuk-link--no-visited-state" href="{{ presented_item.change_link.href}}">
79+
{{ presented_item.change_link.text }}<span class="nhsuk-u-visually-hidden">{{ presented_item.change_link.visually_hidden_text }}</span>
80+
</a><br>
7881
{{ summaryList(presented_item.summary_list_params) }}
7982
{% endfor %}
83+
<a href="{{ presenter.add_other_procedure_history_link.href }}" class="nhsuk-link nhsuk-link--no-visited-state">{{ presenter.add_other_procedure_history_link.text }}</a><br>
8084
{% endset %}
8185

8286
{% set cyst_history_html %}

manage_breast_screening/mammograms/presenters/medical_information_presenter.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ def __init__(self, appointment):
5555
appointment.breast_augmentation_history_items.all(),
5656
BreastAugmentationHistoryItemPresenter,
5757
)
58-
self.other_procedure_history = [
59-
OtherProcedureHistoryItemPresenter(item)
60-
for item in appointment.other_procedure_history_items.all()
61-
]
58+
59+
self.other_procedure_history = self._present_items(
60+
appointment.other_procedure_history_items.all(),
61+
OtherProcedureHistoryItemPresenter,
62+
)
63+
6264
self.benign_lump_history = [
6365
BenignLumpHistoryItemPresenter(item)
6466
for item in appointment.benign_lump_history_items.all()
@@ -226,6 +228,18 @@ def add_mastectomy_or_lumpectomy_history_link(self):
226228
"text": "Add mastectomy or lumpectomy history",
227229
}
228230

231+
@property
232+
def add_other_procedure_history_link(self):
233+
url = reverse(
234+
"mammograms:add_other_procedure_history_item",
235+
kwargs={"pk": self.appointment.pk},
236+
)
237+
238+
return {
239+
"href": url,
240+
"text": "Add other procedure history",
241+
}
242+
229243
def _present_items(self, items, presenter_class):
230244
items = list(items)
231245
if len(items) == 1:

manage_breast_screening/mammograms/presenters/other_procedure_history_item_presenter.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from django.urls import reverse
2+
13
from manage_breast_screening.core.template_helpers import nl2br
24

35

46
class OtherProcedureHistoryItemPresenter:
5-
def __init__(self, implanted_medical_device_history_item):
6-
self._item = implanted_medical_device_history_item
7+
def __init__(self, other_procedure_history_item, counter=None):
8+
self._item = other_procedure_history_item
9+
self.counter = counter
710

811
self.procedure = self._item.get_procedure_display()
912
self.procedure_details = self._item.procedure_details or "N/A"
@@ -33,3 +36,21 @@ def summary_list_params(self):
3336
},
3437
],
3538
}
39+
40+
@property
41+
def change_link(self):
42+
return {
43+
"href": reverse(
44+
"mammograms:change_other_procedure_history_item",
45+
kwargs={
46+
"pk": self._item.appointment_id,
47+
"history_item_pk": self._item.pk,
48+
},
49+
),
50+
"text": "Change",
51+
"visually_hidden_text": (
52+
f" other procedure item {self.counter}"
53+
if self.counter
54+
else " other procedure item"
55+
),
56+
}

0 commit comments

Comments
 (0)