Skip to content

Commit 1f8ac7d

Browse files
authored
Merge pull request #141 from NHSDigital/migrate-to-class-based-views
Migrate to class based views
2 parents 7c74983 + 4a5df11 commit 1f8ac7d

File tree

11 files changed

+160
-140
lines changed

11 files changed

+160
-140
lines changed

lung_cancer_screening/questions/urls.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,40 @@
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
1717
from django.urls import path
18-
from .views.start import start
19-
from .views.have_you_ever_smoked import have_you_ever_smoked
20-
from .views.date_of_birth import date_of_birth
21-
from .views.responses import responses
18+
from .views.start import StartView
19+
from .views.have_you_ever_smoked import HaveYouEverSmokedView
20+
from .views.date_of_birth import DateOfBirthView
21+
from .views.responses import ResponsesView
2222
from .views.age_range_exit import age_range_exit
2323
from .views.non_smoker_exit import non_smoker_exit
2424
from .views.your_results import your_results
25-
from .views.height import height
26-
from .views.weight import weight
27-
from .views.sex_at_birth import sex_at_birth
28-
from .views.gender import gender
29-
from .views.ethnicity import ethnicity
25+
from .views.height import HeightView
26+
from .views.weight import WeightView
27+
from .views.sex_at_birth import SexAtBirthView
28+
from .views.gender import GenderView
29+
from .views.ethnicity import EthnicityView
3030
from .views.education import EducationView
3131
from .views.respiratory_conditions import RespiratoryConditionsView
3232
from .views.asbestos_exposure import AsbestosExposureView
3333
from .views.cancer_diagnosis import CancerDiagnosisView
3434
from .views.family_history_lung_cancer import FamilyHistoryLungCancerView
3535

36+
3637
urlpatterns = [
37-
path('start', start, name='start'),
38-
path('have-you-ever-smoked', have_you_ever_smoked, name='have_you_ever_smoked'),
39-
path('date-of-birth', date_of_birth, name='date_of_birth'),
40-
path('height', height, name='height'),
41-
path('weight', weight, name='weight'),
42-
path('sex-at-birth', sex_at_birth, name='sex_at_birth'),
43-
path('gender', gender, name='gender'),
44-
path('ethnicity', ethnicity, name='ethnicity'),
38+
path('start', StartView.as_view(), name='start'),
39+
path('have-you-ever-smoked', HaveYouEverSmokedView.as_view(), name='have_you_ever_smoked'),
40+
path('date-of-birth', DateOfBirthView.as_view(), name='date_of_birth'),
41+
path('height', HeightView.as_view(), name='height'),
42+
path('weight', WeightView.as_view(), name='weight'),
43+
path('sex-at-birth', SexAtBirthView.as_view(), name='sex_at_birth'),
44+
path('gender', GenderView.as_view(), name='gender'),
45+
path('ethnicity', EthnicityView.as_view(), name='ethnicity'),
4546
path('education', EducationView.as_view(), name='education'),
4647
path('respiratory-conditions', RespiratoryConditionsView.as_view(), name='respiratory_conditions'),
4748
path('asbestos-exposure', AsbestosExposureView.as_view(), name='asbestos_exposure'),
4849
path('cancer-diagnosis', CancerDiagnosisView.as_view(), name='cancer_diagnosis'),
4950
path('family-history-lung-cancer', FamilyHistoryLungCancerView.as_view(), name='family_history_lung_cancer'),
50-
path('responses', responses, name='responses'),
51+
path('responses', ResponsesView.as_view(), name='responses'),
5152
path('age-range-exit', age_range_exit, name='age_range_exit'),
5253
path('non-smoker-exit', non_smoker_exit, name='non_smoker_exit'),
5354
path('your-results', your_results, name='your_results'),

lung_cancer_screening/questions/views/date_of_birth.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
3-
from django.views.decorators.http import require_http_methods
3+
from django.views import View
4+
from django.utils.decorators import method_decorator
45
from datetime import date
56
from dateutil.relativedelta import relativedelta
67

78
from .decorators.participant_decorators import require_participant
89
from ..forms.date_of_birth_form import DateOfBirthForm
910

10-
@require_http_methods(["GET", "POST"])
11-
@require_participant
12-
def date_of_birth(request):
13-
if request.method == "POST":
11+
@method_decorator(require_participant, name="dispatch")
12+
class DateOfBirthView(View):
13+
def get(self, request):
14+
return render_template(
15+
request,
16+
DateOfBirthForm(participant=request.participant)
17+
)
18+
19+
def post(self, request):
1420
form = DateOfBirthForm(
1521
participant=request.participant,
1622
data=request.POST
@@ -37,11 +43,6 @@ def date_of_birth(request):
3743
status=422
3844
)
3945

40-
return render_template(
41-
request,
42-
DateOfBirthForm(participant=request.participant)
43-
)
44-
4546
def render_template(request, form, status=200):
4647
return render(
4748
request,

lung_cancer_screening/questions/views/ethnicity.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
3-
from django.views.decorators.http import require_http_methods
3+
from django.views import View
4+
from django.utils.decorators import method_decorator
45

56
from .decorators.participant_decorators import require_participant
67
from ..forms.ethnicity_form import EthnicityForm
78

8-
@require_http_methods(["GET", "POST"])
9-
@require_participant
10-
def ethnicity(request):
9+
@method_decorator(require_participant, name="dispatch")
10+
class EthnicityView(View):
11+
def get(self, request):
12+
return render_template(
13+
request,
14+
EthnicityForm(participant=request.participant)
15+
)
1116

12-
if request.method == "POST":
17+
def post(self, request):
1318
form = EthnicityForm(
1419
participant=request.participant,
1520
data=request.POST
@@ -27,10 +32,6 @@ def ethnicity(request):
2732
status=422
2833
)
2934

30-
return render_template(
31-
request,
32-
EthnicityForm(participant=request.participant)
33-
)
3435

3536
def render_template(request, form, status=200):
3637
return render(

lung_cancer_screening/questions/views/gender.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
3-
from django.views.decorators.http import require_http_methods
3+
from django.views import View
4+
from django.utils.decorators import method_decorator
45

56
from .decorators.participant_decorators import require_participant
67
from ..forms.gender_form import GenderForm
78

8-
@require_http_methods(["GET", "POST"])
9-
@require_participant
10-
def gender(request):
11-
if request.method == "POST":
9+
@method_decorator(require_participant, name="dispatch")
10+
class GenderView(View):
11+
def get(self, request):
12+
return render_template(
13+
request,
14+
GenderForm(participant=request.participant),
15+
)
16+
17+
def post(self, request):
1218
form = GenderForm(
1319
participant=request.participant,
1420
data=request.POST
@@ -26,11 +32,6 @@ def gender(request):
2632
status=422
2733
)
2834

29-
return render_template(
30-
request,
31-
GenderForm(participant=request.participant),
32-
)
33-
3435
def render_template(request, form, status=200):
3536
return render(
3637
request,

lung_cancer_screening/questions/views/have_you_ever_smoked.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
3-
from django.views.decorators.http import require_http_methods
3+
from django.views import View
4+
from django.utils.decorators import method_decorator
45

56
from .decorators.participant_decorators import require_participant
67
from ..forms.have_you_ever_smoked_form import HaveYouEverSmokedForm
78
from ..models.response_set import HaveYouEverSmokedValues
89

9-
@require_http_methods(["GET", "POST"])
10-
@require_participant
11-
def have_you_ever_smoked(request):
12-
if request.method == "POST":
10+
@method_decorator(require_participant, name="dispatch")
11+
class HaveYouEverSmokedView(View):
12+
def get(self, request):
13+
return render_template(
14+
request,
15+
HaveYouEverSmokedForm(participant=request.participant)
16+
)
17+
18+
def post(self, request):
1319
form = HaveYouEverSmokedForm(
1420
data=request.POST, participant=request.participant
1521
)
@@ -30,15 +36,10 @@ def have_you_ever_smoked(request):
3036
else:
3137
return render_template(
3238
request,
33-
HaveYouEverSmokedForm(participant=request.participant),
39+
form,
3440
status=422
3541
)
3642

37-
return render_template(
38-
request,
39-
HaveYouEverSmokedForm(participant=request.participant)
40-
)
41-
4243

4344
def render_template(request, form, status=200):
4445
return render(
Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
from django.shortcuts import render, redirect
2-
from django.views.decorators.http import require_http_methods
2+
from django.views import View
3+
from django.utils.decorators import method_decorator
34

45
from lung_cancer_screening.questions.forms.metric_height_form import MetricHeightForm
56
from lung_cancer_screening.questions.forms.imperial_height_form import ImperialHeightForm
67
from .decorators.participant_decorators import require_participant
78

8-
@require_http_methods(["GET", "POST"])
9-
@require_participant
10-
def height(request):
11-
unit = request.GET.get('unit')
12-
form_klass = ImperialHeightForm if unit == "imperial" else MetricHeightForm
13-
14-
if request.method == "POST":
15-
form = form_klass(
16-
instance = request.participant.responseset_set.last(),
17-
data=request.POST,
18-
participant=request.participant
9+
@method_decorator(require_participant, name="dispatch")
10+
class HeightView(View):
11+
def get(self, request):
12+
unit = request.GET.get('unit')
13+
form_klass = ImperialHeightForm if unit == "imperial" else MetricHeightForm
14+
15+
return render(
16+
request,
17+
"height.jinja",
18+
{
19+
"form": form_klass(participant=request.participant),
20+
"unit": unit,
21+
"switch_to_unit": "metric" if unit == "imperial" else "imperial"
22+
}
1923
)
2024

21-
if form.is_valid():
22-
form.save()
23-
24-
return redirect("questions:weight")
25-
else:
26-
return render(
27-
request,
28-
"height.jinja",
29-
{
30-
"form": form,
31-
"unit": unit,
32-
"switch_to_unit": "metric" if unit == "imperial" else "imperial"
33-
},
34-
status=422
25+
26+
def post(self, request):
27+
unit = request.GET.get('unit')
28+
form_klass = ImperialHeightForm if unit == "imperial" else MetricHeightForm
29+
30+
if request.method == "POST":
31+
form = form_klass(
32+
instance = request.participant.responseset_set.last(),
33+
data=request.POST,
34+
participant=request.participant
3535
)
3636

37-
return render(
38-
request,
39-
"height.jinja",
40-
{
41-
"form": form_klass(participant=request.participant),
42-
"unit": unit,
43-
"switch_to_unit": "metric" if unit == "imperial" else "imperial"
44-
}
45-
)
37+
if form.is_valid():
38+
form.save()
4639

40+
return redirect("questions:weight")
41+
else:
42+
return render(
43+
request,
44+
"height.jinja",
45+
{
46+
"form": form,
47+
"unit": unit,
48+
"switch_to_unit": "metric" if unit == "imperial" else "imperial"
49+
},
50+
status=422
51+
)

lung_cancer_screening/questions/views/responses.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
33
from django.utils import timezone
4-
from django.views.decorators.http import require_http_methods
4+
from django.views import View
5+
from django.utils.decorators import method_decorator
56

67
from .decorators.participant_decorators import require_participant
78

8-
@require_http_methods(["GET", "POST"])
9-
@require_participant
10-
def responses(request):
11-
response_set = request.participant.responseset_set.last()
9+
@method_decorator(require_participant, name="dispatch")
10+
class ResponsesView(View):
11+
def get(self, request):
12+
return render(
13+
request,
14+
"responses.jinja",
15+
{"response_set": request.participant.responseset_set.last()}
16+
)
17+
18+
def post(self, request):
19+
response_set = request.participant.responseset_set.last()
1220

13-
if request.method == "POST":
1421
response_set.submitted_at = timezone.now()
1522
response_set.save()
1623

1724
return redirect(reverse("questions:your_results"))
18-
19-
return render(
20-
request,
21-
"responses.jinja",
22-
{"response_set": response_set}
23-
)

lung_cancer_screening/questions/views/sex_at_birth.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
3-
from django.views.decorators.http import require_http_methods
3+
from django.views import View
4+
from django.utils.decorators import method_decorator
45

56
from .decorators.participant_decorators import require_participant
67
from ..forms.sex_at_birth_form import SexAtBirthForm
78

8-
@require_http_methods(["GET", "POST"])
9-
@require_participant
10-
def sex_at_birth(request):
11-
if request.method == "POST":
9+
@method_decorator(require_participant, name="dispatch")
10+
class SexAtBirthView(View):
11+
def get(self, request):
12+
return render_template(
13+
request,
14+
SexAtBirthForm(participant=request.participant)
15+
)
16+
17+
def post(self, request):
1218
form = SexAtBirthForm(
1319
participant=request.participant,
1420
data=request.POST
@@ -26,10 +32,6 @@ def sex_at_birth(request):
2632
status=422
2733
)
2834

29-
return render_template(
30-
request,
31-
SexAtBirthForm(participant=request.participant)
32-
)
3335

3436
def render_template(request, form, status=200):
3537
return render(

0 commit comments

Comments
 (0)