Skip to content

Commit 1468d89

Browse files
committed
Make Weight use simpler view pattern
1 parent 44d415b commit 1468d89

6 files changed

Lines changed: 39 additions & 66 deletions

File tree

features/weight.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@Weight
12
Feature: Weight page
23
Scenario: The page is accessible
34
Given I am logged in
@@ -62,8 +63,17 @@ Feature: Weight page
6263
And I see "/weight?change=True" as a link to change "Weight" under "About you"
6364
When I click the link to change "Weight" under "About you"
6465
Then I am on "/weight?change=True"
66+
And I see "70.0" filled in for "Kilograms"
6567
When I click "Switch to stone and pounds"
6668
And I fill in and submit my weight with "5" stone and "10" pounds
6769
Then I am on "/check-your-answers"
6870
And I see "5 stone 10 pounds" as a response to "Weight" under "About you"
71+
When I click the link to change "Weight" under "About you"
72+
Then I am on "/weight?change=True"
73+
And I see "5" filled in for "Stone"
74+
And I see "10" filled in for "Pounds"
75+
When I click "Switch to kilograms"
76+
And I fill in and submit my weight with "70.4"
77+
Then I am on "/check-your-answers"
78+
And I see "70.4 kg" as a response to "Weight" under "About you"
6979

lung_cancer_screening/questions/tests/unit/forms/test_imperial_weight_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.weight_response import WeightResponse
55
from ....forms.imperial_weight_form import ImperialWeightForm
66

77

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

lung_cancer_screening/questions/tests/unit/forms/test_metric_weight_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.weight_response import WeightResponse
55
from ....forms.metric_weight_form import MetricWeightForm
66

77

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

lung_cancer_screening/questions/tests/unit/models/test_weight_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.weight_response_factory import WeightResponseFactory
55

66
from ....models.weight_response import WeightResponse
77

88

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

lung_cancer_screening/questions/tests/unit/views/test_weight.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.weight_response import WeightResponse
88

99

10+
@tag("Weight")
1011
class TestGetWeight(TestCase):
1112
def setUp(self):
1213
self.user = login_user(self.client)
@@ -72,6 +73,7 @@ def test_get_renders_the_imperial_form_if_specified(self):
7273
self.assertContains(response, "Pounds")
7374

7475

76+
@tag("Weight")
7577
class TestPostWeight(TestCase):
7678
def setUp(self):
7779
self.user = login_user(self.client)
Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.shortcuts import render
1+
from django.urls import reverse_lazy
22

33
from .question_base_view import QuestionBaseView
44
from lung_cancer_screening.questions.forms.metric_weight_form import (
@@ -11,69 +11,27 @@
1111

1212

1313
class WeightView(QuestionBaseView):
14-
def get(self, request):
15-
response, _ = WeightResponse.objects.get_or_build(
16-
response_set=request.response_set
14+
template_name = "weight.jinja"
15+
model = WeightResponse
16+
success_url = reverse_lazy("questions:sex_at_birth")
17+
back_link_url = reverse_lazy("questions:height")
18+
19+
def get_form_class(self):
20+
unit = self.get_unit()
21+
return ImperialWeightForm if unit == "imperial" else MetricWeightForm
22+
23+
def get_context_data(self, **kwargs):
24+
context = super().get_context_data(**kwargs)
25+
unit = self.get_unit()
26+
context["unit"] = unit
27+
context["switch_to_unit"] = (
28+
"metric" if unit == "imperial" else "imperial"
1729
)
30+
return context
1831

19-
unit = self.get_unit(request, response)
20-
21-
form_klass = (
22-
ImperialWeightForm if unit == "imperial" else MetricWeightForm
23-
)
24-
25-
return render(
26-
request,
27-
"weight.jinja",
28-
{
29-
"form": form_klass(instance=response),
30-
"unit": unit,
31-
"switch_to_unit": (
32-
"metric" if unit == "imperial" else "imperial"
33-
)
34-
}
35-
)
36-
37-
def post(self, request):
38-
response, _ = WeightResponse.objects.get_or_build(
39-
response_set=request.response_set
40-
)
41-
unit = self.get_unit(request, response)
42-
43-
44-
form_klass = (
45-
ImperialWeightForm if unit == "imperial" else MetricWeightForm
46-
)
47-
48-
form = form_klass(
49-
instance=response,
50-
data=request.POST
51-
)
52-
53-
if form.is_valid():
54-
form.save()
55-
return self.redirect_to_response_or_next_question(
56-
request,
57-
"questions:sex_at_birth"
58-
)
59-
else:
60-
return render(
61-
request,
62-
"weight.jinja",
63-
{
64-
"form": form,
65-
"unit": unit,
66-
"switch_to_unit": (
67-
"metric" if unit == "imperial" else "imperial"
68-
)
69-
},
70-
status=422
71-
)
72-
73-
def get_unit(self, request, response):
74-
unit = request.GET.get('unit')
75-
32+
def get_unit(self):
33+
unit = self.request.GET.get('unit')
34+
response = self.get_object()
7635
if response.imperial and unit != "metric":
7736
unit = "imperial"
78-
7937
return unit

0 commit comments

Comments
 (0)