diff --git a/manage_breast_screening/mammograms/forms/implanted_medical_device_history_form.py b/manage_breast_screening/mammograms/forms/implanted_medical_device_history_form.py
index 8be2d79dc..a86a928ac 100644
--- a/manage_breast_screening/mammograms/forms/implanted_medical_device_history_form.py
+++ b/manage_breast_screening/mammograms/forms/implanted_medical_device_history_form.py
@@ -14,7 +14,7 @@
)
-class ImplantedMedicalDeviceHistoryForm(FormWithConditionalFields):
+class ImplantedMedicalDeviceHistoryBaseForm(FormWithConditionalFields):
def __init__(self, *args, participant, **kwargs):
super().__init__(*args, **kwargs)
@@ -74,21 +74,6 @@ def model_values(self):
additional_details=self.cleaned_data.get("additional_details", ""),
)
- def create(self, appointment, request):
- auditor = Auditor.from_request(request)
- field_values = self.model_values()
-
- implanted_medical_device_history = (
- appointment.implanted_medical_device_history_items.create(
- appointment=appointment,
- **field_values,
- )
- )
-
- auditor.audit_create(implanted_medical_device_history)
-
- return implanted_medical_device_history
-
def full_clean(self):
# if a removal_year is provided then remove it if device_has_been_removed is False
if self.data.get("removal_year") and not self.data.get(
@@ -114,3 +99,54 @@ def clean(self):
code="required",
),
)
+
+
+class ImplantedMedicalDeviceHistoryForm(ImplantedMedicalDeviceHistoryBaseForm):
+ def create(self, appointment, request):
+ auditor = Auditor.from_request(request)
+ field_values = self.model_values()
+
+ implanted_medical_device_history = (
+ appointment.implanted_medical_device_history_items.create(
+ appointment=appointment,
+ **field_values,
+ )
+ )
+
+ auditor.audit_create(implanted_medical_device_history)
+
+ return implanted_medical_device_history
+
+
+class ImplantedMedicalDeviceHistoryUpdateForm(ImplantedMedicalDeviceHistoryBaseForm):
+ def __init__(self, instance, *args, **kwargs):
+ self.instance = instance
+
+ kwargs["participant"] = instance.participant
+ kwargs["initial"] = {
+ "device": instance.device,
+ "other_medical_device_details": instance.other_medical_device_details,
+ "device_has_been_removed": instance.device_has_been_removed,
+ "removal_year": instance.removal_year,
+ "procedure_year": instance.procedure_year,
+ "additional_details": instance.additional_details,
+ }
+
+ super().__init__(*args, **kwargs)
+
+ def update(self, request):
+ self.instance.device = self.cleaned_data["device"]
+ self.instance.other_medical_device_details = self.cleaned_data[
+ "other_medical_device_details"
+ ]
+ self.instance.device_has_been_removed = self.cleaned_data[
+ "device_has_been_removed"
+ ]
+ self.instance.removal_year = self.cleaned_data["removal_year"]
+ self.instance.procedure_year = self.cleaned_data["procedure_year"]
+ self.instance.additional_details = self.cleaned_data["additional_details"]
+ self.instance.save()
+
+ Auditor.from_request(request).audit_update(self.instance)
+
+ return self.instance
diff --git a/manage_breast_screening/mammograms/jinja2/mammograms/record_medical_information.jinja b/manage_breast_screening/mammograms/jinja2/mammograms/record_medical_information.jinja
index d0c2a41bd..d692141c2 100644
--- a/manage_breast_screening/mammograms/jinja2/mammograms/record_medical_information.jinja
+++ b/manage_breast_screening/mammograms/jinja2/mammograms/record_medical_information.jinja
@@ -49,6 +49,9 @@
{% set implanted_medical_device_history_html %}
{% for presented_item in presenter.implanted_medical_device_history %}
+
+ {{ presented_item.change_link.text }}{{ presented_item.change_link.visually_hidden_text }}
+
{{ summaryList(presented_item.summary_list_params) }}
{% endfor %}
{{ presenter.add_implanted_medical_device_history_link.text }}
diff --git a/manage_breast_screening/mammograms/presenters/implanted_medical_device_history_item_presenter.py b/manage_breast_screening/mammograms/presenters/implanted_medical_device_history_item_presenter.py
index 1eca53b8f..d9e0f7f84 100644
--- a/manage_breast_screening/mammograms/presenters/implanted_medical_device_history_item_presenter.py
+++ b/manage_breast_screening/mammograms/presenters/implanted_medical_device_history_item_presenter.py
@@ -1,12 +1,18 @@
+from django.urls import reverse
+
from manage_breast_screening.core.template_helpers import (
nl2br,
)
class ImplantedMedicalDeviceHistoryItemPresenter:
- def __init__(self, implanted_medical_device_history_item):
+ def __init__(self, implanted_medical_device_history_item, counter=None):
self._item = implanted_medical_device_history_item
+ # If there are more than one of these items, we add a counter to the
+ # visually hidden text
+ self.counter = counter
+
self.device = self._item.get_device_display()
self.other_medical_device_details = (
self._item.other_medical_device_details or "N/A"
@@ -48,3 +54,21 @@ def summary_list_params(self):
},
],
}
+
+ @property
+ def change_link(self):
+ return {
+ "href": reverse(
+ "mammograms:change_implanted_medical_device_history_item",
+ kwargs={
+ "pk": self._item.appointment_id,
+ "history_item_pk": self._item.pk,
+ },
+ ),
+ "text": "Change",
+ "visually_hidden_text": (
+ f" item {self.counter}"
+ if self.counter
+ else " implanted medical device item"
+ ),
+ }
diff --git a/manage_breast_screening/mammograms/presenters/medical_information_presenter.py b/manage_breast_screening/mammograms/presenters/medical_information_presenter.py
index 35531419d..71970f26f 100644
--- a/manage_breast_screening/mammograms/presenters/medical_information_presenter.py
+++ b/manage_breast_screening/mammograms/presenters/medical_information_presenter.py
@@ -44,10 +44,22 @@ def __init__(self, appointment):
MastectomyOrLumpectomyHistoryItemPresenter(item)
for item in appointment.mastectomy_or_lumpectomy_history_items.all()
]
- self.implanted_medical_device_history = [
- ImplantedMedicalDeviceHistoryItemPresenter(item)
- for item in appointment.implanted_medical_device_history_items.all()
- ]
+
+ implanted_medical_device_history = list(
+ appointment.implanted_medical_device_history_items.all()
+ )
+ if len(implanted_medical_device_history) == 1:
+ self.implanted_medical_device_history = [
+ ImplantedMedicalDeviceHistoryItemPresenter(
+ implanted_medical_device_history[0]
+ )
+ ]
+ else:
+ self.implanted_medical_device_history = [
+ ImplantedMedicalDeviceHistoryItemPresenter(item, counter=counter)
+ for counter, item in enumerate(implanted_medical_device_history, 1)
+ ]
+
self.breast_augmentation_history = [
BreastAugmentationHistoryItemPresenter(item)
for item in appointment.breast_augmentation_history_items.all()
diff --git a/manage_breast_screening/mammograms/tests/forms/test_implanted_medical_device_history_form.py b/manage_breast_screening/mammograms/tests/forms/test_implanted_medical_device_history_form.py
index 1f24f8668..1b6ff13ce 100644
--- a/manage_breast_screening/mammograms/tests/forms/test_implanted_medical_device_history_form.py
+++ b/manage_breast_screening/mammograms/tests/forms/test_implanted_medical_device_history_form.py
@@ -8,20 +8,28 @@
from manage_breast_screening.participants.models.implanted_medical_device_history_item import (
ImplantedMedicalDeviceHistoryItem,
)
-from manage_breast_screening.participants.tests.factories import AppointmentFactory
+from manage_breast_screening.participants.tests.factories import (
+ AppointmentFactory,
+ ImplantedMedicalDeviceHistoryItemFactory,
+)
from ...forms.implanted_medical_device_history_form import (
ImplantedMedicalDeviceHistoryForm,
+ ImplantedMedicalDeviceHistoryUpdateForm,
)
+@pytest.fixture
+def dummy_request(clinical_user):
+ request = RequestFactory().get("/test-form")
+ request.user = clinical_user
+ return request
+
+
@pytest.mark.django_db
class TestImplantedMedicalDeviceHistoryForm:
- def test_no_data(self, clinical_user):
+ def test_no_data(self):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
-
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(), participant=appointment.participant
)
@@ -29,10 +37,8 @@ def test_no_data(self, clinical_user):
assert not form.is_valid()
assert form.errors == {"device": ["Select the device type"]}
- def test_other_device_without_information(self, clinical_user):
+ def test_other_device_without_information(self):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -50,10 +56,8 @@ def test_other_device_without_information(self, clinical_user):
"other_medical_device_details": ["Provide details of the device"]
}
- def test_procedure_year_invalid_format(self, clinical_user):
+ def test_procedure_year_invalid_format(self):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -70,10 +74,8 @@ def test_procedure_year_invalid_format(self, clinical_user):
assert not form.is_valid()
assert form.errors == {"procedure_year": ["Enter a whole number."]}
- def test_removal_year_invalid_format(self, clinical_user):
+ def test_removal_year_invalid_format(self):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -105,13 +107,15 @@ def test_removal_year_invalid_format(self, clinical_user):
3000,
],
)
- def test_procedure_year_outside_range(self, clinical_user, procedure_year):
+ def test_procedure_year_outside_range(self, procedure_year):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
+ max_year = datetime.date.today().year
+ min_year = max_year - 80
year_outside_range_error_message = (
- self.create_year_outside_range_error_messsage(procedure_year)
+ (f"Year must be {max_year} or earlier")
+ if procedure_year > max_year
+ else (f"Year must be {min_year} or later")
)
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -138,13 +142,15 @@ def test_procedure_year_outside_range(self, clinical_user, procedure_year):
3000,
],
)
- def test_removal_year_outside_range(self, clinical_user, removal_year):
+ def test_removal_year_outside_range(self, removal_year):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
+ max_year = datetime.date.today().year
+ min_year = max_year - 80
year_outside_range_error_message = (
- self.create_year_outside_range_error_messsage(removal_year)
+ (f"Year must be {max_year} or earlier")
+ if removal_year > max_year
+ else (f"Year must be {min_year} or later")
)
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -174,12 +180,8 @@ def test_removal_year_outside_range(self, clinical_user, removal_year):
(datetime.date.today().year - 79, datetime.date.today().year - 80),
],
)
- def test_removal_year_before_procedure_year(
- self, clinical_user, procedure_year, removal_year
- ):
+ def test_removal_year_before_procedure_year(self, procedure_year, removal_year):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -200,10 +202,8 @@ def test_removal_year_before_procedure_year(
"removal_year": ["Year removed cannot be before year of procedure"]
}
- def test_removal_year_when_not_removed(self, clinical_user):
+ def test_removal_year_when_not_removed(self, dummy_request):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(
@@ -229,7 +229,7 @@ def test_removal_year_when_not_removed(self, clinical_user):
assert form.is_valid()
- obj = form.create(appointment=appointment, request=request)
+ obj = form.create(appointment=appointment, request=dummy_request)
obj.refresh_from_db()
assert obj.appointment == appointment
@@ -273,10 +273,8 @@ def test_removal_year_when_not_removed(self, clinical_user):
},
],
)
- def test_success(self, clinical_user, data):
+ def test_success(self, data, dummy_request):
appointment = AppointmentFactory()
- request = RequestFactory().get("/test-form")
- request.user = clinical_user
form = ImplantedMedicalDeviceHistoryForm(
QueryDict(urlencode(data, doseq=True)),
@@ -285,7 +283,7 @@ def test_success(self, clinical_user, data):
assert form.is_valid()
- obj = form.create(appointment=appointment, request=request)
+ obj = form.create(appointment=appointment, request=dummy_request)
assert obj.appointment == appointment
assert obj.device == data.get("device")
@@ -296,11 +294,52 @@ def test_success(self, clinical_user, data):
assert obj.removal_year == data.get("removal_year", None)
assert obj.additional_details == data.get("additional_details", "")
- def create_year_outside_range_error_messsage(self, request_year):
- max_year = datetime.date.today().year
- min_year = max_year - 80
- return (
- (f"Year must be {max_year} or earlier")
- if request_year > max_year
- else (f"Year must be {min_year} or later")
+
+@pytest.mark.django_db
+class TestImplantedMedicalDeviceHistoryUpdateForm:
+ @pytest.fixture
+ def instance(self):
+ return ImplantedMedicalDeviceHistoryItemFactory(
+ device=ImplantedMedicalDeviceHistoryItem.Device.CARDIAC_DEVICE
+ )
+
+ def test_no_data(self, instance):
+ form = ImplantedMedicalDeviceHistoryUpdateForm(instance, QueryDict())
+
+ assert not form.is_valid()
+ assert form.errors == {"device": ["Select the device type"]}
+
+ @pytest.mark.parametrize(
+ "data",
+ [
+ {
+ "device": ImplantedMedicalDeviceHistoryItem.Device.HICKMAN_LINE,
+ },
+ {
+ "device": ImplantedMedicalDeviceHistoryItem.Device.OTHER_MEDICAL_DEVICE,
+ "other_medical_device_details": "Some details about the device",
+ "procedure_year": 2010,
+ "device_has_been_removed": True,
+ "removal_year": 2015,
+ "additional_details": "Some additional details",
+ },
+ ],
+ )
+ def test_success(self, instance, data, dummy_request):
+ form = ImplantedMedicalDeviceHistoryUpdateForm(
+ instance,
+ QueryDict(urlencode(data, doseq=True)),
)
+
+ assert form.is_valid()
+
+ obj = form.update(request=dummy_request)
+
+ assert obj.appointment == instance.appointment
+ assert obj.device == data.get("device")
+ assert obj.other_medical_device_details == data.get(
+ "other_medical_device_details", ""
+ )
+ assert obj.procedure_year == data.get("procedure_year", None)
+ assert obj.removal_year == data.get("removal_year", None)
+ assert obj.additional_details == data.get("additional_details", "")
diff --git a/manage_breast_screening/mammograms/tests/presenters/test_implanted_medical_device_history_item_presenter.py b/manage_breast_screening/mammograms/tests/presenters/test_implanted_medical_device_history_item_presenter.py
index 5c687898e..dd06b015c 100644
--- a/manage_breast_screening/mammograms/tests/presenters/test_implanted_medical_device_history_item_presenter.py
+++ b/manage_breast_screening/mammograms/tests/presenters/test_implanted_medical_device_history_item_presenter.py
@@ -65,3 +65,29 @@ def test_single(self):
},
],
}
+
+ def test_change_link(self):
+ item = ImplantedMedicalDeviceHistoryItemFactory.build(
+ device=ImplantedMedicalDeviceHistoryItem.Device.CARDIAC_DEVICE,
+ additional_details="Some additional details",
+ )
+
+ presenter = ImplantedMedicalDeviceHistoryItemPresenter(item)
+ assert presenter.change_link == {
+ "href": f"/mammograms/{item.appointment_id}/record-medical-information/implanted-medical-device-history/{item.pk}",
+ "text": "Change",
+ "visually_hidden_text": " implanted medical device item",
+ }
+
+ def test_change_link_with_counter(self):
+ item = ImplantedMedicalDeviceHistoryItemFactory.build(
+ device=ImplantedMedicalDeviceHistoryItem.Device.CARDIAC_DEVICE,
+ additional_details="Some additional details",
+ )
+
+ presenter = ImplantedMedicalDeviceHistoryItemPresenter(item, counter=2)
+ assert presenter.change_link == {
+ "href": f"/mammograms/{item.appointment_id}/record-medical-information/implanted-medical-device-history/{item.pk}",
+ "text": "Change",
+ "visually_hidden_text": " item 2",
+ }
diff --git a/manage_breast_screening/mammograms/tests/presenters/test_medical_information_presenter.py b/manage_breast_screening/mammograms/tests/presenters/test_medical_information_presenter.py
index 8e451f706..df043e5bf 100644
--- a/manage_breast_screening/mammograms/tests/presenters/test_medical_information_presenter.py
+++ b/manage_breast_screening/mammograms/tests/presenters/test_medical_information_presenter.py
@@ -10,6 +10,7 @@
from manage_breast_screening.participants.tests.factories import (
AppointmentFactory,
CystHistoryItemFactory,
+ ImplantedMedicalDeviceHistoryItemFactory,
SymptomFactory,
)
@@ -125,6 +126,44 @@ def test_add_breast_cancer_history_link(self):
"text": "Add breast cancer history",
}
+ def test_implanted_medical_device_history_link(self):
+ appointment = AppointmentFactory()
+
+ assert MedicalInformationPresenter(
+ appointment
+ ).add_implanted_medical_device_history_link == {
+ "href": f"/mammograms/{appointment.pk}/record-medical-information/implanted-medical-device-history/",
+ "text": "Add implanted medical device history",
+ }
+
+ def test_implanted_medical_device_history_items_have_a_counter(self):
+ appointment = AppointmentFactory()
+ ImplantedMedicalDeviceHistoryItemFactory.create_batch(
+ 2, appointment=appointment
+ )
+
+ counters = [
+ item.counter
+ for item in MedicalInformationPresenter(
+ appointment
+ ).implanted_medical_device_history
+ ]
+
+ assert counters == [1, 2]
+
+ def test_single_implanted_medical_device_history_item_has_no_counter(self):
+ appointment = AppointmentFactory()
+ ImplantedMedicalDeviceHistoryItemFactory.create(appointment=appointment)
+
+ counters = [
+ item.counter
+ for item in MedicalInformationPresenter(
+ appointment
+ ).implanted_medical_device_history
+ ]
+
+ assert counters == [None]
+
def test_cyst_history_items_have_a_counter(self):
appointment = AppointmentFactory()
CystHistoryItemFactory.create_batch(2, appointment=appointment)
@@ -147,16 +186,6 @@ def test_single_cyst_history_item_has_no_counter(self):
assert counters == [None]
- def test_implanted_medical_device_history_link(self):
- appointment = AppointmentFactory()
-
- assert MedicalInformationPresenter(
- appointment
- ).add_implanted_medical_device_history_link == {
- "href": f"/mammograms/{appointment.pk}/record-medical-information/implanted-medical-device-history/",
- "text": "Add implanted medical device history",
- }
-
def test_cyst_history_link(self):
appointment = AppointmentFactory()
diff --git a/manage_breast_screening/mammograms/tests/views/test_implanted_medical_device_history_views.py b/manage_breast_screening/mammograms/tests/views/test_implanted_medical_device_history_views.py
index 09eb556be..c0b1b4bfb 100644
--- a/manage_breast_screening/mammograms/tests/views/test_implanted_medical_device_history_views.py
+++ b/manage_breast_screening/mammograms/tests/views/test_implanted_medical_device_history_views.py
@@ -8,6 +8,7 @@
)
from manage_breast_screening.participants.tests.factories import (
AppointmentFactory,
+ ImplantedMedicalDeviceHistoryItemFactory,
)
@@ -75,3 +76,60 @@ def test_invalid_post_renders_response_with_errors(self, clinical_user_client):
""",
response.text,
)
+
+
+@pytest.mark.django_db
+class TestChangeImplantedMedicalDeviceHistoryView:
+ @pytest.fixture
+ def appointment(self, clinical_user_client):
+ return AppointmentFactory.create(
+ clinic_slot__clinic__setting__provider=clinical_user_client.current_provider
+ )
+
+ @pytest.fixture
+ def history_item(self, appointment):
+ return ImplantedMedicalDeviceHistoryItemFactory.create(
+ appointment=appointment,
+ device=ImplantedMedicalDeviceHistoryItem.Device.CARDIAC_DEVICE,
+ )
+
+ def test_renders_response(self, clinical_user_client, history_item):
+ response = clinical_user_client.http.get(
+ reverse(
+ "mammograms:change_implanted_medical_device_history_item",
+ kwargs={
+ "pk": history_item.appointment.pk,
+ "history_item_pk": history_item.pk,
+ },
+ )
+ )
+ assert response.status_code == 200
+
+ def test_valid_post_redirects_to_appointment(
+ self, clinical_user_client, appointment, history_item
+ ):
+ response = clinical_user_client.http.post(
+ reverse(
+ "mammograms:change_implanted_medical_device_history_item",
+ kwargs={"pk": appointment.pk, "history_item_pk": history_item.pk},
+ ),
+ {
+ "device": ImplantedMedicalDeviceHistoryItem.Device.CARDIAC_DEVICE,
+ },
+ )
+ assertRedirects(
+ response,
+ reverse(
+ "mammograms:record_medical_information",
+ kwargs={"pk": appointment.pk},
+ ),
+ )
+ assertMessages(
+ response,
+ [
+ messages.Message(
+ level=messages.SUCCESS,
+ message="Details of implanted medical device updated",
+ )
+ ],
+ )
diff --git a/manage_breast_screening/mammograms/urls.py b/manage_breast_screening/mammograms/urls.py
index 61ab858bf..ec384c962 100644
--- a/manage_breast_screening/mammograms/urls.py
+++ b/manage_breast_screening/mammograms/urls.py
@@ -135,6 +135,11 @@
implanted_medical_device_history_view.AddImplantedMedicalDeviceHistoryView.as_view(),
name="add_implanted_medical_device_history_item",
),
+ path(
+ "/record-medical-information/implanted-medical-device-history/",
+ implanted_medical_device_history_view.ChangeImplantedMedicalDeviceHistoryView.as_view(),
+ name="change_implanted_medical_device_history_item",
+ ),
path(
"/record-medical-information/cyst-history/",
cyst_history_view.AddCystHistoryView.as_view(),
diff --git a/manage_breast_screening/mammograms/views/implanted_medical_device_history_view.py b/manage_breast_screening/mammograms/views/implanted_medical_device_history_view.py
index 8d920f399..adcca9d47 100644
--- a/manage_breast_screening/mammograms/views/implanted_medical_device_history_view.py
+++ b/manage_breast_screening/mammograms/views/implanted_medical_device_history_view.py
@@ -1,15 +1,25 @@
+import logging
+
from django.contrib import messages
+from django.http import Http404
+from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import FormView
+from manage_breast_screening.participants.models.implanted_medical_device_history_item import (
+ ImplantedMedicalDeviceHistoryItem,
+)
+
from ..forms.implanted_medical_device_history_form import (
ImplantedMedicalDeviceHistoryForm,
+ ImplantedMedicalDeviceHistoryUpdateForm,
)
from .mixins import InProgressAppointmentMixin
+logger = logging.getLogger(__name__)
-class AddImplantedMedicalDeviceHistoryView(InProgressAppointmentMixin, FormView):
- form_class = ImplantedMedicalDeviceHistoryForm
+
+class BaseImplantedMedicalDeviceHistoryView(InProgressAppointmentMixin, FormView):
template_name = "mammograms/medical_information/medical_history/forms/implanted_medical_device_history.jinja"
def get_success_url(self):
@@ -17,6 +27,35 @@ def get_success_url(self):
"mammograms:record_medical_information", kwargs={"pk": self.appointment.pk}
)
+ def get_back_link_params(self):
+ return {
+ "href": reverse(
+ "mammograms:record_medical_information",
+ kwargs={"pk": self.appointment_pk},
+ ),
+ "text": "Back to appointment",
+ }
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data()
+
+ participant = self.appointment.participant
+
+ context.update(
+ {
+ "back_link_params": self.get_back_link_params(),
+ "caption": participant.full_name,
+ "heading": "Add details of implanted medical device",
+ "page_title": "Details of the implanted medical device",
+ },
+ )
+
+ return context
+
+
+class AddImplantedMedicalDeviceHistoryView(BaseImplantedMedicalDeviceHistoryView):
+ form_class = ImplantedMedicalDeviceHistoryForm
+
def form_valid(self, form):
form.create(appointment=self.appointment, request=self.request)
@@ -57,3 +96,60 @@ def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["participant"] = self.participant
return kwargs
+
+
+class ChangeImplantedMedicalDeviceHistoryView(BaseImplantedMedicalDeviceHistoryView):
+ form_class = ImplantedMedicalDeviceHistoryUpdateForm
+
+ def get_instance(self):
+ try:
+ return ImplantedMedicalDeviceHistoryItem.objects.get(
+ pk=self.kwargs["history_item_pk"],
+ appointment_id=self.kwargs["pk"],
+ )
+ except ImplantedMedicalDeviceHistoryItem.DoesNotExist:
+ logger.exception("History item does not exist for kwargs=%s", self.kwargs)
+ return None
+
+ def get(self, *args, **kwargs):
+ self.instance = self.get_instance()
+ if not self.instance:
+ # For a GET request, if the page shouldn't exist we can
+ # safely redirect to the hub page.
+ return redirect(self.get_success_url())
+ return super().get(*args, **kwargs)
+
+ def post(self, *args, **kwargs):
+ self.instance = self.get_instance()
+ if not self.instance:
+ raise Http404
+ return super().post(*args, **kwargs)
+
+ def get_form_kwargs(self):
+ kwargs = super().get_form_kwargs()
+ kwargs["instance"] = self.instance
+ kwargs["participant"] = self.participant
+ return kwargs
+
+ def form_valid(self, form):
+ form.update(request=self.request)
+
+ messages.add_message(
+ self.request,
+ messages.SUCCESS,
+ "Details of implanted medical device updated",
+ )
+
+ return super().form_valid(form)
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data()
+
+ context.update(
+ {
+ "heading": "Edit details of implanted medical device",
+ "page_title": "Details of the implanted medical device",
+ },
+ )
+
+ return context
diff --git a/manage_breast_screening/participants/models/implanted_medical_device_history_item.py b/manage_breast_screening/participants/models/implanted_medical_device_history_item.py
index e391e76ca..819f68745 100644
--- a/manage_breast_screening/participants/models/implanted_medical_device_history_item.py
+++ b/manage_breast_screening/participants/models/implanted_medical_device_history_item.py
@@ -24,3 +24,7 @@ class Device(models.TextChoices):
device_has_been_removed = models.BooleanField()
removal_year = models.IntegerField(null=True)
additional_details = models.TextField(blank=True, null=False, default="")
+
+ @property
+ def participant(self):
+ return self.appointment.participant
diff --git a/manage_breast_screening/tests/system/clinical/test_implanted_medical_device_history.py b/manage_breast_screening/tests/system/clinical/test_implanted_medical_device_history.py
index c5340b9d7..2a8632f87 100644
--- a/manage_breast_screening/tests/system/clinical/test_implanted_medical_device_history.py
+++ b/manage_breast_screening/tests/system/clinical/test_implanted_medical_device_history.py
@@ -11,7 +11,7 @@
class TestImplantedMedicalDeviceHistory(SystemTestCase):
- def test_adding_a_device(self):
+ def test_adding_and_changing_device(self):
self.given_i_am_logged_in_as_a_clinical_user()
self.and_there_is_an_appointment()
self.and_i_am_on_the_record_medical_information_page()
@@ -30,6 +30,14 @@ def test_adding_a_device(self):
self.and_the_device_is_listed()
self.and_the_message_says_device_added()
+ self.when_i_click_change()
+ self.then_i_see_the_edit_implanted_medical_device_form()
+ self.when_i_select_cardiac_device()
+ self.and_i_click_save_device()
+ self.then_i_am_back_on_the_medical_information_page()
+ self.and_the_implanted_medical_device_is_updated()
+ self.and_the_message_says_implanted_medical_device_updated()
+
def test_accessibility(self):
self.given_i_am_logged_in_as_a_clinical_user()
self.and_there_is_an_appointment()
@@ -112,3 +120,31 @@ def and_the_message_says_device_added(self):
expect(alert).to_contain_text("Success")
expect(alert).to_contain_text("Implanted medical device added")
+
+ def when_i_click_change(self):
+ self.page.get_by_text("Change implanted medical device item").click()
+
+ def then_i_see_the_edit_implanted_medical_device_form(self):
+ expect(
+ self.page.get_by_text("Edit details of implanted medical device")
+ ).to_be_visible()
+ self.assert_page_title_contains("Details of the implanted medical device")
+
+ def when_i_select_cardiac_device(self):
+ self.page.get_by_label(
+ "Cardiac device (such as a pacemaker or ICD)", exact=True
+ ).click()
+
+ def and_the_implanted_medical_device_is_updated(self):
+ key = self.page.locator(
+ ".nhsuk-summary-list__key",
+ has=self.page.get_by_text("Implanted medical device history", exact=True),
+ )
+ row = self.page.locator(".nhsuk-summary-list__row").filter(has=key)
+ expect(row).to_contain_text("Cardiac device (such as a pacemaker or ICD)")
+
+ def and_the_message_says_implanted_medical_device_updated(self):
+ alert = self.page.get_by_role("alert")
+
+ expect(alert).to_contain_text("Success")
+ expect(alert).to_contain_text("Details of implanted medical device updated")