Skip to content

Commit 24b947d

Browse files
committed
Correct current with former and max age validation
1 parent e6d61ca commit 24b947d

5 files changed

Lines changed: 36 additions & 8 deletions

File tree

lung_cancer_screening/questions/forms/age_when_started_smoking_form.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, *args, **kwargs):
2121
"invalid": "Enter your age when you started smoking",
2222
"zero_entered":"The age you started smoking must be between 1 and your current age",
2323
"age_started_smoking_greater_than_current_age":"The age you started smoking must be the same as, or less than your current age",
24-
"no_date_of_birth" : format_html("<a href=\"{}\">Provide your date of birth</a> before answering this question", reverse_lazy("questions:date_of_birth"))
24+
"no_date_of_birth" : format_html("<a href=\"{}\">Provide your date of birth</a> before answering age when you started smoking", reverse_lazy("questions:date_of_birth"))
2525
}
2626
)
2727

lung_cancer_screening/questions/forms/periods_when_you_stopped_smoking_form.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from django import forms
2+
from django.urls import reverse_lazy
3+
from django.utils.html import format_html
24

35
from ...nhsuk_forms.integer_field import IntegerField
46
from ...nhsuk_forms.typed_choice_field import TypedChoiceField
@@ -27,7 +29,7 @@ def __init__(self, *args, **kwargs):
2729
error_messages={
2830
"required": self.required_error_message(),
2931
"duration_years": self.duration_years_error_message()
30-
},
32+
}
3133
)
3234

3335
self.fields["duration_years"] = IntegerField(
@@ -37,6 +39,9 @@ def __init__(self, *args, **kwargs):
3739
hint=self.duration_years_hint(),
3840
required=False,
3941
suffix="years",
42+
error_messages={
43+
"no_when_you_quit_smoking": format_html("<a href=\"{}\">Provide when you quit smoking</a> before answering the total number of years you stopped smoking", reverse_lazy("questions:when_you_quit_smoking"))
44+
}
4045
)
4146

4247
def clean_duration_years(self):
@@ -84,7 +89,7 @@ def stopped_or_quit(self):
8489
return "stopped or quit "
8590

8691
def smoked_or_smoke(self):
87-
if self.response_set().current_smoker():
92+
if self.response_set().former_smoker():
8893
return "smoked"
8994
else:
9095
return "have been smoking"

lung_cancer_screening/questions/forms/when_you_quit_smoking_form.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, *args, **kwargs):
2424
"invalid": "Enter your age when you quit smoking",
2525
"min_value":"The age you quit smoking must be between 1 and your current age",
2626
"age_when_quit_smoking_greater_than_age_started":"The age you quit smoking cannot be lower than the age you started smoking",
27+
"age_when_quit_smoking_greater_than_current_age" : "The age you quit smoking must be the same as, or less than, your current age",
2728
"no_date_of_birth" : format_html("<a href=\"{}\">Provide your date of birth</a> before answering this question", reverse_lazy("questions:date_of_birth"))
2829
}
2930
)

lung_cancer_screening/questions/models/age_when_started_smoking_response.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@ def clean(self):
3030
})
3131

3232
def years_smoked_including_stopped(self):
33-
return (
34-
self.response_set.date_of_birth_response.age_in_years() -
35-
self.value
36-
)
33+
if self.response_set.current_smoker():
34+
return (
35+
self.response_set.date_of_birth_response.age_in_years() -
36+
self.value
37+
)
38+
39+
if self.response_set.former_smoker():
40+
if not hasattr(self.response_set, "when_you_quit_smoking_response"):
41+
raise ValidationError({
42+
"duration_years": ValidationError(
43+
"when you quit smoking not set",
44+
code="no_when_you_quit_smoking"
45+
)
46+
})
47+
else:
48+
return self.response_set.when_you_quit_smoking_response.value - self.value

lung_cancer_screening/questions/models/when_you_quit_smoking_response.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.db import models
2-
from django.core.validators import MinValueValidator
2+
from django.core.validators import MinValueValidator, MaxValueValidator
33
from django.core.exceptions import ValidationError
44

55
from .base import BaseModel
@@ -20,6 +20,7 @@ class WhenYouQuitSmokingResponse(BaseModel):
2020
def clean(self):
2121
super().clean()
2222
self.validates_date_of_birth_response()
23+
self.validate_age_when_quit_smoking_greater_than_current_age()
2324
self.validates_age_started_smoking_response()
2425
self.validates_age_when_quit_smoking_greater_than_age_started()
2526

@@ -33,6 +34,15 @@ def validates_date_of_birth_response(self):
3334
)
3435
})
3536

37+
def validate_age_when_quit_smoking_greater_than_current_age(self):
38+
if self.value and self.response_set.date_of_birth_response.age_in_years() < self.value:
39+
raise ValidationError({
40+
"value": ValidationError(
41+
"age when you quit smoking cannot be greater than current age",
42+
code="age_when_quit_smoking_greater_than_current_age"
43+
44+
)
45+
})
3646

3747
def validates_age_started_smoking_response(self):
3848
if not hasattr(self.response_set, "age_when_started_smoking_response"):

0 commit comments

Comments
 (0)