Skip to content

Commit 44d415b

Browse files
committed
Make SexAtBirth use simpler view pattern
1 parent 0e7f142 commit 44d415b

5 files changed

Lines changed: 16 additions & 48 deletions

File tree

features/sex_at_birth.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@SexAtBirth
12
Feature: Sex at birth page
23
Scenario: The page is accessible
34
Given I am logged in
@@ -28,6 +29,7 @@ Feature: Sex at birth page
2829
And I see "/sex-at-birth?change=True" as a link to change "Sex at birth" under "About you"
2930
When I click the link to change "Sex at birth" under "About you"
3031
Then I am on "/sex-at-birth?change=True"
32+
And I see "Male" selected
3133
When I fill in and submit my sex at birth with "Female"
3234
Then I am on "/check-your-answers"
3335
And I see "Female" as a response to "Sex at birth" under "About you"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +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.sex_at_birth_response import SexAtBirthResponse, SexAtBirthValues
55
from ....forms.sex_at_birth_form import SexAtBirthForm
66

7+
8+
@tag("SexAtBirth")
79
class TestSexAtBirthForm(TestCase):
810
def setUp(self):
911
self.response_set = ResponseSetFactory()

lung_cancer_screening/questions/tests/unit/models/test_sex_at_birth_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.sex_at_birth_response_factory import SexAtBirthResponseFactory
55

66
from ....models.sex_at_birth_response import SexAtBirthResponse, SexAtBirthValues
77

88

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

lung_cancer_screening/questions/tests/unit/views/test_sex_at_birth.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.sex_at_birth_response import SexAtBirthResponse, SexAtBirthValues
88

99

10+
@tag("SexAtBirth")
1011
class TestGetSexAtBirth(TestCase):
1112
def setUp(self):
1213
self.user = login_user(self.client)
@@ -48,6 +49,7 @@ def test_get_contains_the_correct_form_fields(self):
4849
self.assertContains(response, "What was your sex at birth?")
4950

5051

52+
@tag("SexAtBirth")
5153
class TestPostSexAtBirth(TestCase):
5254
def setUp(self):
5355
self.user = login_user(self.client)
Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,13 @@
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.sex_at_birth_form import SexAtBirthForm
65
from ..models.sex_at_birth_response import SexAtBirthResponse
76

87

98
class SexAtBirthView(QuestionBaseView):
10-
def get(self, request):
11-
response, _ = SexAtBirthResponse.objects.get_or_build(
12-
response_set=request.response_set
13-
)
14-
return render_template(
15-
request,
16-
SexAtBirthForm(instance=response)
17-
)
18-
19-
def post(self, request):
20-
response, _ = SexAtBirthResponse.objects.get_or_build(
21-
response_set=request.response_set
22-
)
23-
form = SexAtBirthForm(
24-
instance=response,
25-
data=request.POST
26-
)
27-
28-
if form.is_valid():
29-
response.value = form.cleaned_data["value"]
30-
response.save()
31-
return self.redirect_to_response_or_next_question(
32-
request,
33-
"questions:gender"
34-
)
35-
else:
36-
return render_template(
37-
request,
38-
form,
39-
status=422
40-
)
41-
42-
43-
def render_template(request, form, status=200):
44-
return render(
45-
request,
46-
"question_form.jinja",
47-
{
48-
"form": form,
49-
"back_link_url": reverse("questions:weight")
50-
},
51-
status=status
52-
)
9+
template_name = "question_form.jinja"
10+
form_class = SexAtBirthForm
11+
model = SexAtBirthResponse
12+
success_url = reverse_lazy("questions:gender")
13+
back_link_url = reverse_lazy("questions:weight")

0 commit comments

Comments
 (0)