Skip to content

Commit 8064b9d

Browse files
committed
PPHA-589: Ensure no change smoking redirects to next type
1 parent 47b36ce commit 8064b9d

8 files changed

Lines changed: 165 additions & 49 deletions

File tree

features/smoking_history.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Feature: Smoking history pages
108108
And I check "Rolling tobacco"
109109
And I check "Pipe"
110110
And I check "Cigarillos"
111+
And I check "Small cigars"
111112
And I check "Medium cigars"
112113
And I submit the form
113114

@@ -261,6 +262,29 @@ Feature: Smoking history pages
261262
When I fill in "Roughly how many years did you smoke 5 full pipe loads a month?" with "4"
262263
And I submit the form
263264

265+
# Small cigars with no change
266+
Then I am on "/small-cigars-smoking-current"
267+
And I see a page title "Do you currently smoke small cigars?"
268+
When I check "Yes"
269+
And I submit the form
270+
271+
Then I am on "/small-cigars-smoked-total-years"
272+
When I fill in "Roughly how many years have you smoked small cigars?" with "8"
273+
And I submit the form
274+
275+
Then I am on "/small-cigars-smoking-frequency"
276+
And I see a page title "How often do you smoke small cigars?"
277+
When I check "Monthly"
278+
And I submit the form
279+
280+
Then I am on "/small-cigars-smoked-amount"
281+
When I fill in "Roughly how many small cigars do you currently smoke in a normal month?" with "9"
282+
And I submit the form
283+
284+
Then I am on "/small-cigars-smoking-change"
285+
And I see a page title "Has the number of small cigars you normally smoke changed over time?"
286+
When I check "No, it has not changed"
287+
And I submit the form
264288

265289
# Medium cigars with only decreased
266290
Then I am on "/medium-cigars-smoking-current"

lung_cancer_screening/questions/models/response_set.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ def types_tobacco_smoking_history(self):
129129
))
130130

131131

132+
def user_editable_tobacco_smoking_histories(self):
133+
return self.tobacco_smoking_history.user_editable().in_form_order().all()
134+
135+
132136
def previous_normal_smoking_history(self, smoking_history_item):
133-
histories = list(self.tobacco_smoking_history.in_form_order().all())
137+
histories = list(self.user_editable_tobacco_smoking_histories())
134138
current_history_index = histories.index(smoking_history_item)
135139

136140
if current_history_index == 0:
@@ -146,3 +150,13 @@ def previous_smoking_history(self, smoking_history_item):
146150
return self.tobacco_smoking_history.filter(
147151
type=self.previous_normal_smoking_history(smoking_history_item).type,
148152
).in_form_order().last()
153+
154+
155+
def next_smoking_history(self, smoking_history_item):
156+
histories = list(self.user_editable_tobacco_smoking_histories())
157+
current_history_index = histories.index(smoking_history_item)
158+
159+
if current_history_index == len(histories) - 1:
160+
return None
161+
162+
return histories[current_history_index + 1]

lung_cancer_screening/questions/models/tobacco_smoking_history.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ def in_form_order(self):
2828
*[When(level=level_val, then=Value(i)) for i, level_val in enumerate(levels_order)],
2929
default=Value(len(levels_order)),
3030
)
31-
return self.order_by(order_levels, order_types)
31+
return self.order_by(order_types, order_levels)
32+
33+
34+
def user_editable(self):
35+
return self.exclude(level=Levels.NO_CHANGE)
36+
3237

3338
def increased(self):
3439
return self.filter(level='increased')
3540

41+
3642
def decreased(self):
3743
return self.filter(level='decreased')
3844

45+
3946
def normal(self):
4047
return self.filter(level='normal')
4148

@@ -114,6 +121,7 @@ def clean(self):
114121
super().clean()
115122
self._validate_no_change_and_other_levels()
116123

124+
117125
def _validate_no_change_and_other_levels(self):
118126
others = self.response_set.tobacco_smoking_history.filter(type=self.type).exclude(pk=self.pk)
119127
if self.level == NO_CHANGE_VALUE:

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,48 @@ def test_returns_decreased_when_the_previous_normal_smoking_history_when_both_pr
421421
self.response_set.previous_smoking_history(current_smoking_history),
422422
decreased_smoking_history
423423
)
424+
425+
426+
def test_next_smoking_history_returns_none_when_it_is_the_only_smoking_history_item(self):
427+
smoking_history_item = TobaccoSmokingHistoryFactory.create(
428+
response_set=self.response_set
429+
)
430+
self.assertIsNone(
431+
self.response_set.next_smoking_history(smoking_history_item)
432+
)
433+
434+
435+
def test_next_smoking_history_returns_the_next_smoking_history_when_it_is_not_the_only_smoking_history_item_respecting_form_order(self):
436+
TobaccoSmokingHistoryFactory.create(
437+
response_set=self.response_set,
438+
cigarillos=True,
439+
increased=True,
440+
)
441+
next_smoking_history = TobaccoSmokingHistoryFactory.create(
442+
response_set=self.response_set,
443+
cigarillos=True,
444+
)
445+
current_smoking_history = TobaccoSmokingHistoryFactory.create(
446+
response_set=self.response_set,
447+
cigarettes=True,
448+
)
449+
self.assertEqual(
450+
self.response_set.next_smoking_history(current_smoking_history),
451+
next_smoking_history
452+
)
453+
454+
455+
def test_next_smoking_history_does_not_include_no_change(self):
456+
current_smoking_history = TobaccoSmokingHistoryFactory.create(
457+
response_set=self.response_set,
458+
cigarillos=True,
459+
normal=True,
460+
)
461+
TobaccoSmokingHistoryFactory.create(
462+
response_set=self.response_set,
463+
cigarillos=True,
464+
no_change=True,
465+
)
466+
self.assertIsNone(
467+
self.response_set.next_smoking_history(current_smoking_history)
468+
)

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ def test_is_complete_returns_true_for_a_no_change_level_when_no_responses_are_pr
652652

653653

654654
def test_in_form_order_returns_the_tobacco_smoking_history_in_form_order(self):
655+
cigarettes_decreased = TobaccoSmokingHistoryFactory(
656+
response_set=self.response_set,
657+
cigarettes=True,
658+
decreased=True,
659+
)
655660
medium_cigars_decreased = TobaccoSmokingHistoryFactory(
656661
response_set=self.response_set,
657662
medium_cigars=True,
@@ -676,6 +681,18 @@ def test_in_form_order_returns_the_tobacco_smoking_history_in_form_order(self):
676681
in_form_order = TobaccoSmokingHistory.objects.in_form_order()
677682
self.assertQuerySetEqual(
678683
in_form_order,
679-
[cigarettes_normal, medium_cigars_normal, medium_cigars_increased, medium_cigars_decreased],
684+
[cigarettes_normal, cigarettes_decreased, medium_cigars_normal, medium_cigars_increased, medium_cigars_decreased],
680685
ordered=True,
681686
)
687+
688+
689+
def test_user_editable_does_not_include_no_change(self):
690+
TobaccoSmokingHistoryFactory.create(
691+
response_set=self.response_set,
692+
cigarillos=True,
693+
no_change=True,
694+
)
695+
self.assertQuerySetEqual(
696+
TobaccoSmokingHistory.objects.user_editable().all(),
697+
[],
698+
)

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def test_redirects_when_the_user_is_not_eligible(self):
191191
self.assertRedirects(response, reverse("questions:have_you_ever_smoked"))
192192

193193

194+
194195
def test_redirects_to_the_next_question_given_no_level(self):
195196
response = self.client.post(
196197
reverse("questions:smoking_change", kwargs = {
@@ -202,6 +203,35 @@ def test_redirects_to_the_next_question_given_no_level(self):
202203
self.assertRedirects(response, reverse("questions:responses"))
203204

204205

206+
def test_redirects_to_the_next_smoking_type_given_no_change_level_and_other_smoking_histories_exist(self):
207+
self.tobacco_smoking_history.delete()
208+
current_smoking_history = TobaccoSmokingHistoryFactory.create(
209+
response_set=self.response_set,
210+
cigarettes=True,
211+
complete=True,
212+
normal=True,
213+
)
214+
next_smoking_history = TobaccoSmokingHistoryFactory.create(
215+
response_set=self.response_set,
216+
medium_cigars=True,
217+
complete=True,
218+
normal=True,
219+
)
220+
221+
response = self.client.post(
222+
reverse("questions:smoking_change", kwargs = {
223+
"tobacco_type": current_smoking_history.url_type()
224+
}),
225+
{"value": [TobaccoSmokingHistory.Levels.NO_CHANGE]}
226+
)
227+
228+
self.assertRedirects(response,
229+
reverse("questions:smoking_current", kwargs={
230+
"tobacco_type": next_smoking_history.url_type()
231+
}),
232+
fetch_redirect_response=False
233+
)
234+
205235
def test_redirects_to_the_next_question_given_level_increased(self):
206236
response = self.client.post(
207237
reverse("questions:smoking_change", kwargs = {
@@ -230,31 +260,6 @@ def test_redirects_to_the_next_question_given_level_decreased_only(self):
230260
}))
231261

232262

233-
def test_does_not_redirect_to_increased_if_increased_exists_for_another_type_and_is_not_selected(self):
234-
TobaccoSmokingHistoryFactory.create(
235-
response_set=self.response_set,
236-
type=self.tobacco_smoking_history.type,
237-
complete=True,
238-
increased=True,
239-
)
240-
medium_cigars = TobaccoSmokingHistoryFactory.create(
241-
response_set=self.response_set,
242-
medium_cigars=True,
243-
complete=True
244-
)
245-
246-
response = self.client.post(
247-
reverse("questions:smoking_change", kwargs = {
248-
"tobacco_type": medium_cigars.url_type()
249-
}),
250-
{"value": [TobaccoSmokingHistory.Levels.NO_CHANGE]}
251-
)
252-
253-
self.assertRedirects(response, reverse("questions:responses"),
254-
fetch_redirect_response=False
255-
)
256-
257-
258263
def test_creates_a_smoking_change_response(self):
259264
self.client.post(
260265
reverse("questions:smoking_change", kwargs = {

lung_cancer_screening/questions/views/smoked_total_years.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ def prerequisite_responses(self):
105105
"smoked_amount_response"
106106
]
107107

108-
109108
def remaining_unanswered_histories(self):
110109
tobacco_type = camelize(underscore(self.kwargs["tobacco_type"]))
111110
histories = self.request.response_set.types_tobacco_smoking_history()

lung_cancer_screening/questions/views/smoking_change.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from django.contrib.auth.mixins import LoginRequiredMixin
44
from django.views.generic.edit import FormView
55

6-
from lung_cancer_screening.questions.models.tobacco_smoking_history import TobaccoSmokingHistory
76

87
from .mixins.ensure_response_set import EnsureResponseSet
98
from .mixins.ensure_eligible import EnsureEligibleMixin
@@ -57,25 +56,24 @@ def form_valid(self, form):
5756

5857

5958
def get_success_url(self):
60-
tobacco_smoking_history = self.tobacco_smoking_history()
61-
if tobacco_smoking_history.increased().exists():
62-
return reverse(
63-
"questions:smoking_frequency",
64-
kwargs={
65-
"tobacco_type": self.kwargs["tobacco_type"],
66-
"level": TobaccoSmokingHistory.Levels.INCREASED,
67-
},
68-
query=self.get_change_query_params()
69-
)
70-
elif tobacco_smoking_history.decreased().exists():
71-
return reverse(
72-
"questions:smoking_frequency",
73-
kwargs={
74-
"tobacco_type": self.kwargs["tobacco_type"],
75-
"level": TobaccoSmokingHistory.Levels.DECREASED,
76-
},
77-
query=self.get_change_query_params(),
78-
)
59+
if self.next_smoking_history():
60+
if self.next_smoking_history().is_normal():
61+
return reverse(
62+
"questions:smoking_current",
63+
kwargs={
64+
"tobacco_type": self.next_smoking_history().url_type(),
65+
},
66+
query=self.get_change_query_params(),
67+
)
68+
else:
69+
return reverse(
70+
"questions:smoking_frequency",
71+
kwargs={
72+
"tobacco_type": self.next_smoking_history().url_type(),
73+
"level": self.next_smoking_history().level,
74+
},
75+
query=self.get_change_query_params(),
76+
)
7977
else:
8078
return reverse("questions:responses")
8179

@@ -95,3 +93,9 @@ def tobacco_smoking_history(self):
9593
return self.request.response_set.tobacco_smoking_history.by_url_type(
9694
self.kwargs["tobacco_type"]
9795
)
96+
97+
98+
def next_smoking_history(self):
99+
return self.request.response_set.next_smoking_history(
100+
self.tobacco_smoking_history_item()
101+
)

0 commit comments

Comments
 (0)