Skip to content

Commit 50a3e11

Browse files
committed
Move handling of defaulting years smoked and current from view to form.
1 parent 7681230 commit 50a3e11

6 files changed

Lines changed: 118 additions & 30 deletions

File tree

lung_cancer_screening/questions/forms/types_tobacco_smoking_form.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from django import forms
22

3+
from lung_cancer_screening.questions.models.smoked_total_years_response import SmokedTotalYearsResponse
4+
from lung_cancer_screening.questions.models.smoking_current_response import SmokingCurrentResponse
5+
36
from ...nhsuk_forms.choice_field import MultipleChoiceField
47
from ..models.tobacco_smoking_history import (
58
TobaccoSmokingHistory,
@@ -81,4 +84,26 @@ def _create_types_selected(self):
8184

8285
TobaccoSmokingHistory.objects.bulk_create(instances)
8386

87+
self._create_default_responses_for_single_selection()
88+
8489
return instances
90+
91+
def _create_default_responses_for_single_selection(self):
92+
response_set = self.response_set
93+
instances = response_set.tobacco_smoking_history
94+
if instances.count() == 1:
95+
instance = instances.first()
96+
97+
SmokingCurrentResponse.objects.update_or_create(
98+
tobacco_smoking_history=instance,
99+
defaults={
100+
"value": response_set.current_smoker()
101+
},
102+
)
103+
104+
SmokedTotalYearsResponse.objects.update_or_create(
105+
tobacco_smoking_history=instance,
106+
defaults={
107+
"value": response_set.age_when_started_smoking_response.years_smoked_including_stopped()
108+
}
109+
)

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

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

3+
from ...factories.age_when_started_smoking_response_factory import AgeWhenStartedSmokingResponseFactory
4+
35
from ...factories.response_set_factory import ResponseSetFactory
46
from ...factories.tobacco_smoking_history_factory import TobaccoSmokingHistoryFactory
7+
from ...factories.have_you_ever_smoked_response_factory import HaveYouEverSmokedResponseFactory
58
from ....models.tobacco_smoking_history import (
69
TobaccoSmokingHistoryTypes,
710
)
@@ -102,6 +105,9 @@ def test_saves_an_tobacco_smoking_type_for_each_value_selected(self):
102105
def test_does_not_create_a_new_tobacco_smoking_type_if_it_already_exists(self):
103106
TobaccoSmokingHistoryFactory(response_set=self.response_set, cigarettes=True)
104107

108+
HaveYouEverSmokedResponseFactory(response_set=self.response_set, current_smoker=True)
109+
AgeWhenStartedSmokingResponseFactory(response_set=self.response_set, value=20)
110+
105111
form = TypesTobaccoSmokingForm(
106112
response_set=self.response_set,
107113
data={
@@ -116,6 +122,10 @@ def test_does_not_create_a_new_tobacco_smoking_type_if_it_already_exists(self):
116122
def test_deletes_a_tobacco_smoking_type_if_it_is_no_longer_selected(self):
117123
TobaccoSmokingHistoryFactory(response_set=self.response_set, cigarettes=True)
118124

125+
HaveYouEverSmokedResponseFactory(response_set=self.response_set, current_smoker=True)
126+
127+
AgeWhenStartedSmokingResponseFactory(response_set=self.response_set, value=20)
128+
119129
form = TypesTobaccoSmokingForm(
120130
response_set=self.response_set,
121131
data={

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ def test_responds_successfully(self):
8686

8787
self.assertEqual(response.status_code, 200)
8888

89-
def test_back_link_normal_level(self):
89+
def test_back_link_normal_level_multiple_types(self):
90+
self.tobacco_smoking_history = TobaccoSmokingHistoryFactory.create(
91+
response_set=self.response_set,
92+
cigarettes=True,
93+
pipe=True,
94+
complete=True
95+
)
96+
9097
response = self.client.get(reverse("questions:smoking_frequency", kwargs = {
9198
"tobacco_type": TobaccoSmokingHistoryTypes.CIGARETTES.value.lower()
9299
}))
@@ -96,6 +103,16 @@ def test_back_link_normal_level(self):
96103
"/cigarettes-smoked-total-years",
97104
)
98105

106+
def test_back_link_normal_level_single_type(self):
107+
response = self.client.get(reverse("questions:smoking_frequency", kwargs = {
108+
"tobacco_type": TobaccoSmokingHistoryTypes.CIGARETTES.value.lower()
109+
}))
110+
111+
self.assertEqual(
112+
response.context_data["back_link_url"],
113+
"/types-tobacco-smoking",
114+
)
115+
99116
def test_back_link_increased_level(self):
100117
TobaccoSmokingHistoryFactory.create(
101118
response_set=self.response_set,

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

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from django.urls import reverse
33
from django.utils.text import slugify
44

5-
from lung_cancer_screening.questions.tests.factories.smoked_total_years_response_factory import SmokedTotalYearsResponseFactory
5+
from ...factories.age_when_started_smoking_response_factory import AgeWhenStartedSmokingResponseFactory
6+
from ...factories.have_you_ever_smoked_response_factory import HaveYouEverSmokedResponseFactory
7+
from ...factories.when_you_quit_smoking_response_factory import WhenYouQuitSmokingResponseFactory
68

79
from .helpers.authentication import login_user
810
from ...factories.response_set_factory import ResponseSetFactory
@@ -134,6 +136,8 @@ def test_redirects_when_the_user_is_not_eligible(self):
134136
def test_creates_a_tobacco_smoking_type_parent_model_for_each_type_given(self):
135137
response_set = ResponseSetFactory.create(user=self.user, eligible=True)
136138

139+
AgeWhenStartedSmokingResponseFactory(response_set=response_set, value=20)
140+
137141
self.client.post(
138142
reverse("questions:types_tobacco_smoking"),
139143
self.valid_params
@@ -155,10 +159,14 @@ def test_post_redirects_to_the_current_of_first_type_of_tobacco_smoking_history_
155159
"tobacco_type": slugify(TobaccoSmokingHistoryTypes.CIGARETTES.value)
156160
}), fetch_redirect_response=False)
157161

158-
@tag("wip")
159162
def test_post_redirects_to_frequency_if_one_type_of_tobacco_smoking_history_given(self):
160163
response_set = ResponseSetFactory.create(user=self.user, eligible=True)
161164

165+
AgeWhenStartedSmokingResponseFactory.create(
166+
response_set=response_set,
167+
value=20
168+
)
169+
162170
response = self.client.post(
163171
reverse("questions:types_tobacco_smoking"),
164172
{"value": [TobaccoSmokingHistoryTypes.PIPE.value]}
@@ -169,8 +177,49 @@ def test_post_redirects_to_frequency_if_one_type_of_tobacco_smoking_history_give
169177
}), fetch_redirect_response=False)
170178

171179
response_set.tobacco_smoking_history.first().refresh_from_db()
172-
print(response_set.tobacco_smoking_history.first().is_current())
180+
173181
self.assertTrue(response_set.tobacco_smoking_history.first().is_pipe())
174182
self.assertTrue(response_set.tobacco_smoking_history.first().is_current())
175-
print(response_set.tobacco_smoking_history.first().duration_years())
176-
#self.assertTrue(response_set.tobacco_smoking_history.first().duration_years() == 5)
183+
184+
age = response_set.date_of_birth_response.age_in_years()
185+
age_started = response_set.age_when_started_smoking_response.value
186+
187+
self.assertEqual(response_set.tobacco_smoking_history.first().duration_years(), age-age_started)
188+
189+
190+
def test_post_redirects_to_frequency_if_one_type_of_tobacco_smoking_history_given_for_former_smoker(self):
191+
response_set = ResponseSetFactory.create(user=self.user, eligible=True)
192+
193+
AgeWhenStartedSmokingResponseFactory.create(
194+
response_set=response_set,
195+
value=20
196+
)
197+
198+
response_set.have_you_ever_smoked_response.delete()
199+
200+
HaveYouEverSmokedResponseFactory.create(
201+
response_set=response_set,
202+
former_smoker=True,
203+
)
204+
205+
age_when_quit = 30
206+
WhenYouQuitSmokingResponseFactory.create(
207+
response_set=response_set,
208+
value=age_when_quit
209+
)
210+
211+
response = self.client.post(
212+
reverse("questions:types_tobacco_smoking"),
213+
{"value": [TobaccoSmokingHistoryTypes.PIPE.value]}
214+
)
215+
216+
self.assertRedirects(response, reverse("questions:smoking_frequency", kwargs={
217+
"tobacco_type": slugify(TobaccoSmokingHistoryTypes.PIPE.value)
218+
}), fetch_redirect_response=False)
219+
220+
response_set.tobacco_smoking_history.first().refresh_from_db()
221+
222+
self.assertTrue(response_set.tobacco_smoking_history.first().is_pipe())
223+
self.assertFalse(response_set.tobacco_smoking_history.first().is_current())
224+
225+
self.assertEqual(response_set.tobacco_smoking_history.first().duration_years(), 10)

lung_cancer_screening/questions/views/smoking_frequency.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ def get_success_url(self):
4646

4747
def get_back_link_url(self):
4848
if not self.kwargs.get("level") :
49-
return reverse(
50-
"questions:smoked_total_years",
51-
kwargs=self.kwargs,
52-
query=self.get_change_query_params(),
53-
)
49+
if self.request.response_set.tobacco_smoking_history.count() == 1:
50+
return reverse(
51+
"questions:types_tobacco_smoking",
52+
)
53+
else:
54+
return reverse(
55+
"questions:smoked_total_years",
56+
kwargs=self.kwargs,
57+
query=self.get_change_query_params(),
58+
)
5459

5560
elif self.kwargs.get("level") == TobaccoSmokingHistory.Levels.INCREASED:
5661
return reverse(

lung_cancer_screening/questions/views/types_tobacco_smoking.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from .mixins.ensure_eligible import EnsureEligibleMixin
77

88
from ..forms.types_tobacco_smoking_form import TypesTobaccoSmokingForm
9-
from ..models.smoking_current_response import SmokingCurrentResponse
10-
119

1210
class TypesTobaccoSmokingView(
1311
LoginRequiredMixin, EnsureResponseSet, EnsureEligibleMixin, FormView
@@ -39,26 +37,10 @@ def get_success_url(self):
3937
)
4038

4139
if (tobacco_smoking_history.count() == 1):
42-
print(f"is current smoker: {self.request.response_set.have_you_ever_smoked_response.is_current_smoker()}")
43-
44-
print(f"smoking current: {hasattr(next_tobacco_smoking_history, 'smoking_current_response')}")
45-
46-
print(f"next smoking history type: {next_tobacco_smoking_history.type}")
47-
48-
SmokingCurrentResponse.objects.update_or_create(
49-
tobacco_smoking_history=next_tobacco_smoking_history,
50-
value=self.request.response_set.have_you_ever_smoked_response.is_current_smoker()
51-
)
52-
53-
print(f"smoking current: {next_tobacco_smoking_history.smoking_current_response}")
54-
55-
#age_started = self.request.response_set.age_when_started_smoking
56-
#age_stopped = self.request.response_set.age_stopped_smoking_responses.first().value
57-
#next_tobacco_smoking_history.smoked_total_years_response.value = 5
58-
next_tobacco_smoking_history.save()
5940
return reverse("questions:smoking_frequency", kwargs={
6041
"tobacco_type": next_tobacco_smoking_history.url_type()
6142
})
43+
6244
return reverse(
6345
"questions:smoking_current",
6446
kwargs={

0 commit comments

Comments
 (0)