Skip to content

Commit 6aaafab

Browse files
committed
Migrate to class based views
Sonarqube complains about method based views due to handling of multiple HTTP methods. Use class based views to appease it.
1 parent 3274445 commit 6aaafab

File tree

11 files changed

+163
-146
lines changed

11 files changed

+163
-146
lines changed

lung_cancer_screening/questions/urls.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@
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

3131
urlpatterns = [
32-
path('start', start, name='start'),
33-
path('have-you-ever-smoked', have_you_ever_smoked, name='have_you_ever_smoked'),
34-
path('date-of-birth', date_of_birth, name='date_of_birth'),
35-
path('height', height, name='height'),
36-
path('weight', weight, name='weight'),
37-
path('sex-at-birth', sex_at_birth, name='sex_at_birth'),
38-
path('gender', gender, name='gender'),
39-
path('ethnicity', ethnicity, name='ethnicity'),
40-
path('responses', responses, name='responses'),
32+
path('start', StartView.as_view(), name='start'),
33+
path('have-you-ever-smoked', HaveYouEverSmokedView.as_view(), name='have_you_ever_smoked'),
34+
path('date-of-birth', DateOfBirthView.as_view(), name='date_of_birth'),
35+
path('height', HeightView.as_view(), name='height'),
36+
path('weight', WeightView.as_view(), name='weight'),
37+
path('sex-at-birth', SexAtBirthView.as_view(), name='sex_at_birth'),
38+
path('gender', GenderView.as_view(), name='gender'),
39+
path('ethnicity', EthnicityView.as_view(), name='ethnicity'),
40+
path('responses', ResponsesView.as_view(), name='responses'),
4141
path('age-range-exit', age_range_exit, name='age_range_exit'),
4242
path('non-smoker-exit', non_smoker_exit, name='non_smoker_exit'),
4343
path('your-results', your_results, name='your_results'),

lung_cancer_screening/questions/views/date_of_birth.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
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(
15+
request,
16+
"date_of_birth.jinja",
17+
{ "form": DateOfBirthForm(participant=request.participant) }
18+
)
19+
20+
def post(self, request):
1421
form = DateOfBirthForm(
1522
participant=request.participant,
1623
data=request.POST
@@ -37,9 +44,3 @@ def date_of_birth(request):
3744
{ "form": form },
3845
status=422
3946
)
40-
41-
return render(
42-
request,
43-
"date_of_birth.jinja",
44-
{ "form": DateOfBirthForm(participant=request.participant) }
45-
)
Lines changed: 11 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.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(
13+
request,
14+
"ethnicity.jinja",
15+
{ "form": EthnicityForm(participant=request.participant) }
16+
)
1117

12-
if request.method == "POST":
18+
def post(self, request):
1319
form = EthnicityForm(
1420
participant=request.participant,
1521
data=request.POST
@@ -27,9 +33,3 @@ def ethnicity(request):
2733
{ "form": form },
2834
status=422
2935
)
30-
31-
return render(
32-
request,
33-
"ethnicity.jinja",
34-
{ "form": EthnicityForm(participant=request.participant) }
35-
)

lung_cancer_screening/questions/views/gender.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +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.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(
13+
request,
14+
"gender.jinja",
15+
{ "form": GenderForm(participant=request.participant) }
16+
)
17+
18+
def post(self, request):
1219
form = GenderForm(
1320
participant=request.participant,
1421
data=request.POST
@@ -26,9 +33,3 @@ def gender(request):
2633
{ "form": form },
2734
status=422
2835
)
29-
30-
return render(
31-
request,
32-
"gender.jinja",
33-
{ "form": GenderForm(participant=request.participant) }
34-
)

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,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

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(
14+
request,
15+
"have_you_ever_smoked.jinja",
16+
{ "form": HaveYouEverSmokedForm(participant=request.participant) }
17+
)
18+
19+
def post(self, request):
1320
form = HaveYouEverSmokedForm(
1421
data=request.POST, participant=request.participant
1522
)
@@ -34,9 +41,3 @@ def have_you_ever_smoked(request):
3441
{ "form": form },
3542
status=422
3643
)
37-
38-
return render(
39-
request,
40-
"have_you_ever_smoked.jinja",
41-
{"form": HaveYouEverSmokedForm(participant=request.participant)}
42-
)
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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +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.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(
13+
request,
14+
"sex_at_birth.jinja",
15+
{ "form": SexAtBirthForm(participant=request.participant) }
16+
)
17+
18+
def post(self, request):
1219
form = SexAtBirthForm(
1320
participant=request.participant,
1421
data=request.POST
@@ -26,9 +33,3 @@ def sex_at_birth(request):
2633
{ "form": form },
2734
status=422
2835
)
29-
30-
return render(
31-
request,
32-
"sex_at_birth.jinja",
33-
{ "form": SexAtBirthForm(participant=request.participant) }
34-
)
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from django.shortcuts import render, redirect
22
from django.urls import reverse
33
from django.core.exceptions import ValidationError
4-
from django.views.decorators.http import require_http_methods
4+
from django.views import View
55

66
from lung_cancer_screening.questions.models.participant import Participant
77

8-
@require_http_methods(["GET", "POST"])
9-
def start(request):
10-
if request.method == "POST":
8+
class StartView(View):
9+
def get(self, request):
10+
return render(
11+
request,
12+
"start.jinja"
13+
)
14+
15+
def post(self, request):
1116
try:
1217
participant, _ = Participant.objects.get_or_create(
1318
unique_id=request.POST['participant_id']
@@ -24,9 +29,3 @@ def start(request):
2429
{"error_messages": [{ "text": message } for message in e.messages ]},
2530
status=422
2631
)
27-
28-
else:
29-
return render(
30-
request,
31-
"start.jinja"
32-
)

0 commit comments

Comments
 (0)