Skip to content

Commit a2493e0

Browse files
committed
SPIKE: Try to use django FormView to simplify view code
1 parent 5fff754 commit a2493e0

7 files changed

Lines changed: 43 additions & 45 deletions

File tree

features/asbestos_exposure.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@AsbestosExposure
12
Feature: Asbestos exposure page
23
Scenario: The page is accessible
34
Given I am logged in
@@ -28,6 +29,7 @@ Feature: Asbestos exposure page
2829
And I see "/asbestos-exposure?change=True" as a link to change "Have you ever worked in a job where you were exposed to asbestos?" under "Your health"
2930
When I click the link to change "Have you ever worked in a job where you were exposed to asbestos?" under "Your health"
3031
Then I am on "/asbestos-exposure?change=True"
32+
And I see "No" selected
3133
When I fill in and submit my asbestos exposure with "Yes"
3234
Then I am on "/check-your-answers"
3335
And I see "Yes" as a response to "Have you ever worked in a job where you were exposed to asbestos?" under "Your health"

features/steps/form_steps.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ def when_i_fill_in_and_submit_my_cancer_diagnosis(context, relatives_age_when_di
108108
@when('I submit the form')
109109
def when_i_submit_the_form(context):
110110
when_i_click(context, "Continue")
111+
112+
@then(u'I see "{value}" selected')
113+
def then_i_see_value_selected(context, value):
114+
assert context.page.get_by_label(value, exact=True).is_checked()

lung_cancer_screening/questions/tests/unit/forms/test_asbestos_exposure_form.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from django.test import TestCase
1+
from django.test import TestCase, tag
22

33
from ...factories.response_set_factory import ResponseSetFactory
44
from ....models.asbestos_exposure_response import AsbestosExposureResponse
55
from ....forms.asbestos_exposure_form import AsbestosExposureForm
66

77

8+
@tag("AsbestosExposure")
89
class TestAsbestosExposureForm(TestCase):
910
def setUp(self):
1011
self.response_set = ResponseSetFactory()

lung_cancer_screening/questions/tests/unit/models/test_asbestos_exposure_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from django.test import TestCase
1+
from django.test import TestCase, tag
22

33
from ...factories.response_set_factory import ResponseSetFactory
44
from ...factories.asbestos_exposure_response_factory import AsbestosExposureResponseFactory
55

66
from ....models.asbestos_exposure_response import AsbestosExposureResponse
77

88

9+
@tag("AsbestosExposure")
910
class TestAsbestosExposureResponse(TestCase):
1011
def setUp(self):
1112
self.response_set = ResponseSetFactory()

lung_cancer_screening/questions/tests/unit/views/test_asbestos_exposure.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.test import TestCase
1+
from django.test import TestCase, tag
22
from django.urls import reverse
33
from dateutil.relativedelta import relativedelta
44
from django.utils import timezone
@@ -7,6 +7,7 @@
77
from lung_cancer_screening.questions.models.asbestos_exposure_response import AsbestosExposureResponse
88

99

10+
@tag("AsbestosExposure")
1011
class TestGetAsbestosExposure(TestCase):
1112
def setUp(self):
1213
self.user = login_user(self.client)
@@ -46,6 +47,7 @@ def test_get_contains_the_correct_form_fields(self):
4647
self.assertContains(response, "Have you ever worked in a job where you might have been exposed to asbestos?")
4748

4849

50+
@tag("AsbestosExposure")
4951
class TestPostAsbestosExposure(TestCase):
5052
def setUp(self):
5153
self.user = login_user(self.client)
Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
1-
from django.shortcuts import render
2-
from django.urls import reverse
1+
from django.urls import reverse_lazy
32

43
from .question_base_view import QuestionBaseView
54
from ..forms.asbestos_exposure_form import AsbestosExposureForm
65
from ..models.asbestos_exposure_response import AsbestosExposureResponse
76

87
class AsbestosExposureView(QuestionBaseView):
9-
def get(self, request):
10-
response, _ = AsbestosExposureResponse.objects.get_or_build(
11-
response_set=request.response_set
12-
)
13-
return render_template(
14-
request,
15-
AsbestosExposureForm(instance=response)
16-
)
17-
18-
def post(self, request):
19-
response, _ = AsbestosExposureResponse.objects.get_or_build(
20-
response_set=request.response_set
21-
)
22-
form = AsbestosExposureForm(
23-
instance=response,
24-
data=request.POST
25-
)
26-
27-
if form.is_valid():
28-
response.value = form.cleaned_data["value"]
29-
response.save()
30-
return self.redirect_to_response_or_next_question(
31-
request,
32-
"questions:cancer_diagnosis"
33-
)
34-
else:
35-
return render_template(request, form, 422)
36-
37-
def render_template(request, form, status=200):
38-
return render(
39-
request,
40-
"asbestos_exposure.jinja",
41-
{
42-
"form": form,
43-
"back_link_url": reverse("questions:respiratory_conditions")
44-
},
45-
status=status
46-
)
8+
template_name = "asbestos_exposure.jinja"
9+
form_class = AsbestosExposureForm
10+
model = AsbestosExposureResponse
11+
success_url = reverse_lazy("questions:cancer_diagnosis")
12+
back_link_url = reverse_lazy("questions:respiratory_conditions")
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from django.shortcuts import redirect
22
from django.urls import reverse
33
from django.contrib.auth.mixins import LoginRequiredMixin
4-
from django.views import View
4+
from django.views.generic.edit import UpdateView
55

66
from .mixins.ensure_response_set import EnsureResponseSet
77

88

9-
class QuestionBaseView(LoginRequiredMixin, EnsureResponseSet, View):
9+
class QuestionBaseView(LoginRequiredMixin, EnsureResponseSet, UpdateView):
1010

1111
def should_redirect_to_responses(self, request):
1212
return bool(request.POST.get("change"))
@@ -16,3 +16,25 @@ def redirect_to_response_or_next_question(self, request, next_question_url, quer
1616
return redirect(reverse("questions:responses", query=query))
1717
else:
1818
return redirect(reverse(next_question_url))
19+
20+
def get_context_data(self, **kwargs):
21+
context = super().get_context_data(**kwargs)
22+
context["back_link_url"] = self.back_link_url
23+
return context
24+
25+
def get_success_url(self):
26+
if self.should_redirect_to_responses(self.request):
27+
return reverse("questions:responses")
28+
else:
29+
return super().get_success_url()
30+
31+
def get_object(self):
32+
return self.model.objects.get_or_build(
33+
response_set=self.request.response_set
34+
)[0]
35+
36+
def form_invalid(self, form):
37+
return self.render_to_response(
38+
self.get_context_data(form=form),
39+
status=422
40+
)

0 commit comments

Comments
 (0)