Skip to content

Commit 6452a91

Browse files
committed
Add presenter for ResponseSet presentation layer
1 parent 9472a27 commit 6452a91

32 files changed

+515
-75
lines changed

lung_cancer_screening/questions/jinja2/responses.jinja

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,17 @@
1818
<div class="nhsuk-grid-column-two-thirds">
1919
<h1 class="nhsuk-heading-l">Responses</h1>
2020
<ul class="responses">
21-
{% set have_you_ever_smoked = response_set.have_you_ever_smoked_response %}
22-
{% set date_of_birth = response_set.date_of_birth_response %}
23-
{% set height = response_set.height_response %}
24-
{% set weight = response_set.weight_response %}
25-
{% set sex_at_birth = response_set.sex_at_birth_response %}
26-
{% set gender = response_set.gender_response %}
27-
{% set ethnicity = response_set.ethnicity_response %}
28-
{% set asbestos_exposure = response_set.asbestos_exposure_response %}
29-
{% set respiratory_conditions = response_set.respiratory_conditions_response %}
30-
<li>Have you ever smoked? {% if have_you_ever_smoked %}{{ have_you_ever_smoked.get_value_display() }}{% endif %}</li>
31-
<li>What is your date of birth? {% if date_of_birth %}{{ date_of_birth.value }}{% endif %}</li>
32-
<li>What is your height? {% if height %}{{ height.formatted }}{% endif %}</li>
33-
<li>What is your weight? {% if weight %}{{ weight.formatted }}{% endif %}</li>
34-
<li>What was your sex at birth? {% if sex_at_birth %}{{ sex_at_birth.get_value_display() }}{% endif %}</li>
35-
<li>Which of these best describes you? {% if gender %}{{ gender.get_value_display() }}{% endif %}</li>
36-
<li>What is your ethnic background? {% if ethnicity %}{{ ethnicity.get_value_display() }}{% endif %}</li>
37-
<li>Have you ever worked in a job where you might have been exposed to asbestos? {% if asbestos_exposure %}{{ "Yes" if asbestos_exposure.value else "No" }}{% else %}No{% endif %}</li>
38-
<li>Have you ever been diagnosed with any of the following respiratory conditions? {% if respiratory_conditions %}{{ respiratory_conditions.formatted }}{% endif %}</li>
21+
<li>Have you ever smoked? {{ response_set.have_you_ever_smoked }}</li>
22+
<li>What is your date of birth? {{ response_set.date_of_birth }}</li>
23+
<li>What is your height? {{ response_set.height }}</li>
24+
<li>What is your weight? {{ response_set.weight }}</li>
25+
<li>What was your sex at birth? {{ response_set.sex_at_birth }}</li>
26+
<li>Which of these best describes you? {{ response_set.gender }}</li>
27+
<li>What is your ethnic background? {{ response_set.ethnicity }}</li>
28+
<li>Have you ever worked in a job where you might have been exposed to asbestos? {{ response_set.asbestos_exposure }}</li>
29+
<li>Have you ever been diagnosed with any of the following respiratory conditions? {{ response_set.respiratory_conditions }}</li>
3930
</ul>
31+
4032
<form action="{{ request.path }}" method="post">
4133
{{ csrf_input }}
4234
{{ button({

lung_cancer_screening/questions/models/height_response.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from django.db import models
22
from django.core.exceptions import ValidationError
33
from django.core.validators import MaxValueValidator, MinValueValidator
4-
from decimal import Decimal
54

65
from .base import BaseModel
76
from .response_set import ResponseSet
@@ -29,12 +28,3 @@ def clean(self):
2928
raise ValidationError("Either metric or imperial height must be provided.")
3029
if self.metric and self.imperial:
3130
raise ValidationError("Cannot provide both metric and imperial height.")
32-
33-
@property
34-
def formatted(self):
35-
if self.metric:
36-
return f"{Decimal(self.metric) / 10}cm"
37-
elif self.imperial:
38-
value = Decimal(self.imperial)
39-
return f"{value // 12} feet {value % 12} inches"
40-
return None

lung_cancer_screening/questions/models/respiratory_conditions_response.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,3 @@ class RespiratoryConditionsResponse(BaseModel):
2929
models.CharField(max_length=1, choices=RespiratoryConditionValues.choices),
3030
validators=[validate_singleton_option]
3131
)
32-
33-
@property
34-
def formatted(self):
35-
if not self.value:
36-
return None
37-
display_values = [
38-
RespiratoryConditionValues(code).label
39-
for code in self.value
40-
]
41-
return ", ".join(display_values)

lung_cancer_screening/questions/models/weight_response.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from django.db import models
22
from django.core.exceptions import ValidationError
33
from django.core.validators import MaxValueValidator, MinValueValidator
4-
from decimal import Decimal
54

65
from .base import BaseModel
76
from .response_set import ResponseSet
@@ -29,12 +28,3 @@ def clean(self):
2928
raise ValidationError("Either metric or imperial weight must be provided.")
3029
if self.metric and self.imperial:
3130
raise ValidationError("Cannot provide both metric and imperial weight.")
32-
33-
@property
34-
def formatted(self):
35-
if self.metric:
36-
return f"{Decimal(self.metric) / 10}kg"
37-
elif self.imperial:
38-
value = Decimal(self.imperial)
39-
return f"{value // 14} stone {value % 14} pounds"
40-
return None

lung_cancer_screening/questions/presenters/__init__.py

Whitespace-only changes.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from decimal import Decimal
2+
3+
from ..models.respiratory_conditions_response import RespiratoryConditionValues
4+
5+
class ResponseSetPresenter:
6+
DATE_FORMAT = "%-d %B %Y" # eg 8 September 2000
7+
8+
def __init__(self, response_set):
9+
self.response_set = response_set
10+
11+
@property
12+
def have_you_ever_smoked(self):
13+
if not hasattr(self.response_set, 'have_you_ever_smoked_response'):
14+
return None
15+
16+
return self.response_set.have_you_ever_smoked_response.get_value_display()
17+
18+
@property
19+
def date_of_birth(self):
20+
if not hasattr(self.response_set, 'date_of_birth_response'):
21+
return None
22+
23+
return self.response_set.date_of_birth_response.value.strftime(self.DATE_FORMAT)
24+
25+
@property
26+
def height(self):
27+
if not hasattr(self.response_set, 'height_response'):
28+
return None
29+
30+
if self.response_set.height_response.metric:
31+
return f"{Decimal(self.response_set.height_response.metric) / 10} cm"
32+
elif self.response_set.height_response.imperial:
33+
value = Decimal(self.response_set.height_response.imperial)
34+
return f"{value // 12} feet {value % 12} inches"
35+
else:
36+
return None
37+
38+
@property
39+
def weight(self):
40+
if not hasattr(self.response_set, 'weight_response'):
41+
return None
42+
43+
if self.response_set.weight_response.metric:
44+
return f"{Decimal(self.response_set.weight_response.metric) / 10} kg"
45+
elif self.response_set.weight_response.imperial:
46+
value = Decimal(self.response_set.weight_response.imperial)
47+
return f"{value // 14} stone {value % 14} pounds"
48+
else:
49+
return None
50+
51+
@property
52+
def sex_at_birth(self):
53+
if not hasattr(self.response_set, 'sex_at_birth_response'):
54+
return None
55+
56+
return self.response_set.sex_at_birth_response.get_value_display()
57+
58+
@property
59+
def gender(self):
60+
if not hasattr(self.response_set, 'gender_response'):
61+
return None
62+
63+
return self.response_set.gender_response.get_value_display()
64+
65+
@property
66+
def ethnicity(self):
67+
if not hasattr(self.response_set, 'ethnicity_response'):
68+
return None
69+
70+
return self.response_set.ethnicity_response.get_value_display()
71+
72+
@property
73+
def asbestos_exposure(self):
74+
if not hasattr(self.response_set, 'asbestos_exposure_response'):
75+
return None
76+
77+
return "Yes" if self.response_set.asbestos_exposure_response.value else "No"
78+
79+
@property
80+
def respiratory_conditions(self):
81+
if not hasattr(self.response_set, 'respiratory_conditions_response'):
82+
return None
83+
84+
return self._list_to_sentence([
85+
RespiratoryConditionValues(code).label
86+
for code in self.response_set.respiratory_conditions_response.value
87+
])
88+
89+
90+
def _list_to_sentence(self, list):
91+
if len(list) == 0:
92+
return ''
93+
if len(list) == 1:
94+
return list[0]
95+
if len(list) == 2:
96+
return '{} and {}'.format(list[0], list[1])
97+
98+
return '{}, and {}'.format(', '.join(list[:-1]), list[-1])
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from .response_set_factory import ResponseSetFactory
4+
from ...models.asbestos_exposure_response import AsbestosExposureResponse
5+
6+
7+
class AsbestosExposureResponseFactory(factory.django.DjangoModelFactory):
8+
class Meta:
9+
model = AsbestosExposureResponse
10+
11+
response_set = factory.SubFactory(ResponseSetFactory)
12+
value = factory.Faker('boolean')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from .response_set_factory import ResponseSetFactory
4+
from ...models.date_of_birth_response import DateOfBirthResponse
5+
6+
7+
class DateOfBirthResponseFactory(factory.django.DjangoModelFactory):
8+
class Meta:
9+
model = DateOfBirthResponse
10+
11+
response_set = factory.SubFactory(ResponseSetFactory)
12+
value = factory.Faker('date_of_birth')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from .response_set_factory import ResponseSetFactory
4+
from ...models.ethnicity_response import EthnicityResponse, EthnicityValues
5+
6+
7+
class EthnicityResponseFactory(factory.django.DjangoModelFactory):
8+
class Meta:
9+
model = EthnicityResponse
10+
11+
response_set = factory.SubFactory(ResponseSetFactory)
12+
value = factory.Iterator(EthnicityValues)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from .response_set_factory import ResponseSetFactory
4+
from ...models.gender_response import GenderResponse, GenderValues
5+
6+
7+
class GenderResponseFactory(factory.django.DjangoModelFactory):
8+
class Meta:
9+
model = GenderResponse
10+
11+
response_set = factory.SubFactory(ResponseSetFactory)
12+
value = factory.Iterator(GenderValues)

0 commit comments

Comments
 (0)