Skip to content

Commit d6ba914

Browse files
committed
Height form
1 parent fa3637a commit d6ba914

File tree

6 files changed

+103
-11
lines changed

6 files changed

+103
-11
lines changed

.vscode/settings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
"MD013": false,
44
"MD024": { "siblings_only": true },
55
"MD033": false
6-
}
6+
},
7+
"cSpell.words": [
8+
"dateutil",
9+
"nhsuk",
10+
"relativedelta",
11+
"responseset",
12+
"unsubmitted"
13+
]
714
}

lung_cancer_screening/core/form_fields.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,33 @@ def widget_attrs(self, widget):
206206
attrs.pop("step", None)
207207

208208
return attrs
209+
210+
class DecimalField(forms.DecimalField):
211+
def __init__(
212+
self,
213+
*args,
214+
hint=None,
215+
label_classes=None,
216+
classes=None,
217+
**kwargs,
218+
):
219+
kwargs["template_name"] = "forms/input.jinja"
220+
221+
self.hint = hint
222+
self.classes = classes
223+
self.label_classes = label_classes
224+
225+
super().__init__(*args, **kwargs)
226+
227+
def widget_attrs(self, widget):
228+
attrs = super().widget_attrs(widget)
229+
230+
# Don't use min/max/step attributes.
231+
attrs.pop("min", None)
232+
attrs.pop("max", None)
233+
attrs.pop("step", None)
234+
235+
return attrs
209236

210237

211238
class BoundChoiceField(forms.BoundField):
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import decimal
2+
from django import forms
3+
4+
from lung_cancer_screening.core.form_fields import DecimalField
5+
from ..models.response_set import ResponseSet
6+
7+
class HeightForm(forms.ModelForm):
8+
9+
def __init__(self, *args, **kwargs):
10+
self.participant = kwargs.pop('participant')
11+
super().__init__(*args, **kwargs)
12+
self.instance.participant = self.participant
13+
14+
self.fields["height_in_cm"] = DecimalField(
15+
label="Centimetres",
16+
classes="nhsuk-input--width-4",
17+
)
18+
19+
20+
21+
def clean_height_in_cm(self):
22+
data = self.cleaned_data['height_in_cm']
23+
return data*10
24+
25+
class Meta:
26+
model = ResponseSet
27+
fields = ['height']

lung_cancer_screening/questions/jinja2/height.jinja

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,7 @@
2828
"classes": "nhsuk-label--m"
2929
}
3030
}) %}
31-
{{
32-
input({
33-
"id": "height",
34-
"name": "height",
35-
"label": {
36-
"text": "Centimetres",
37-
},
38-
"classes": "nhsuk-input--width-4"
39-
})
40-
}}
31+
{{ form }}
4132
{% endcall %}
4233

4334
{{ button({

lung_cancer_screening/questions/models/response_set.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ def clean(self):
5252
raise ValidationError(
5353
"Responses have already been submitted for this participant"
5454
)
55+
56+
@property
57+
def height(self):
58+
return self.height
59+
60+
@height.setter
61+
def _height(self, value):
62+
if value > 0 :
63+
print("The date chosen was in the future.")
64+
self._height = value
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django.test import TestCase
2+
3+
from ....models.participant import Participant
4+
from ....forms.height_form import HeightForm
5+
6+
class TestHeightForm(TestCase):
7+
def setUp(self):
8+
self.participant = Participant.objects.create(unique_id="1234567890")
9+
10+
def test_cm_to_mm(self):
11+
height = "170.4"
12+
form = HeightForm(
13+
participant=self.participant,
14+
data={
15+
"height_in_cm": height
16+
}
17+
)
18+
19+
self.assertTrue(form.is_valid())
20+
21+
self.assertEqual(form.cleaned_data["height_in_cm"], 1704)
22+
23+
def test_is_invalid(self):
24+
form = HeightForm(
25+
participant=self.participant,
26+
data={
27+
"height_in_cm": "invalid"
28+
}
29+
)
30+
self.assertFalse(form.is_valid())

0 commit comments

Comments
 (0)