diff --git a/lung_cancer_screening/questions/urls.py b/lung_cancer_screening/questions/urls.py index a340d22e..482d16e2 100644 --- a/lung_cancer_screening/questions/urls.py +++ b/lung_cancer_screening/questions/urls.py @@ -15,39 +15,40 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.urls import path -from .views.start import start -from .views.have_you_ever_smoked import have_you_ever_smoked -from .views.date_of_birth import date_of_birth -from .views.responses import responses +from .views.start import StartView +from .views.have_you_ever_smoked import HaveYouEverSmokedView +from .views.date_of_birth import DateOfBirthView +from .views.responses import ResponsesView from .views.age_range_exit import age_range_exit from .views.non_smoker_exit import non_smoker_exit from .views.your_results import your_results -from .views.height import height -from .views.weight import weight -from .views.sex_at_birth import sex_at_birth -from .views.gender import gender -from .views.ethnicity import ethnicity +from .views.height import HeightView +from .views.weight import WeightView +from .views.sex_at_birth import SexAtBirthView +from .views.gender import GenderView +from .views.ethnicity import EthnicityView from .views.education import EducationView from .views.respiratory_conditions import RespiratoryConditionsView from .views.asbestos_exposure import AsbestosExposureView from .views.cancer_diagnosis import CancerDiagnosisView from .views.family_history_lung_cancer import FamilyHistoryLungCancerView + urlpatterns = [ - path('start', start, name='start'), - path('have-you-ever-smoked', have_you_ever_smoked, name='have_you_ever_smoked'), - path('date-of-birth', date_of_birth, name='date_of_birth'), - path('height', height, name='height'), - path('weight', weight, name='weight'), - path('sex-at-birth', sex_at_birth, name='sex_at_birth'), - path('gender', gender, name='gender'), - path('ethnicity', ethnicity, name='ethnicity'), + path('start', StartView.as_view(), name='start'), + path('have-you-ever-smoked', HaveYouEverSmokedView.as_view(), name='have_you_ever_smoked'), + path('date-of-birth', DateOfBirthView.as_view(), name='date_of_birth'), + path('height', HeightView.as_view(), name='height'), + path('weight', WeightView.as_view(), name='weight'), + path('sex-at-birth', SexAtBirthView.as_view(), name='sex_at_birth'), + path('gender', GenderView.as_view(), name='gender'), + path('ethnicity', EthnicityView.as_view(), name='ethnicity'), path('education', EducationView.as_view(), name='education'), path('respiratory-conditions', RespiratoryConditionsView.as_view(), name='respiratory_conditions'), path('asbestos-exposure', AsbestosExposureView.as_view(), name='asbestos_exposure'), path('cancer-diagnosis', CancerDiagnosisView.as_view(), name='cancer_diagnosis'), path('family-history-lung-cancer', FamilyHistoryLungCancerView.as_view(), name='family_history_lung_cancer'), - path('responses', responses, name='responses'), + path('responses', ResponsesView.as_view(), name='responses'), path('age-range-exit', age_range_exit, name='age_range_exit'), path('non-smoker-exit', non_smoker_exit, name='non_smoker_exit'), path('your-results', your_results, name='your_results'), diff --git a/lung_cancer_screening/questions/views/date_of_birth.py b/lung_cancer_screening/questions/views/date_of_birth.py index 4bdf8933..b0465cb7 100644 --- a/lung_cancer_screening/questions/views/date_of_birth.py +++ b/lung_cancer_screening/questions/views/date_of_birth.py @@ -1,16 +1,22 @@ from django.shortcuts import render, redirect from django.urls import reverse -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from datetime import date from dateutil.relativedelta import relativedelta from .decorators.participant_decorators import require_participant from ..forms.date_of_birth_form import DateOfBirthForm -@require_http_methods(["GET", "POST"]) -@require_participant -def date_of_birth(request): - if request.method == "POST": +@method_decorator(require_participant, name="dispatch") +class DateOfBirthView(View): + def get(self, request): + return render_template( + request, + DateOfBirthForm(participant=request.participant) + ) + + def post(self, request): form = DateOfBirthForm( participant=request.participant, data=request.POST @@ -37,11 +43,6 @@ def date_of_birth(request): status=422 ) - return render_template( - request, - DateOfBirthForm(participant=request.participant) - ) - def render_template(request, form, status=200): return render( request, diff --git a/lung_cancer_screening/questions/views/ethnicity.py b/lung_cancer_screening/questions/views/ethnicity.py index 3d02353c..18e2d775 100644 --- a/lung_cancer_screening/questions/views/ethnicity.py +++ b/lung_cancer_screening/questions/views/ethnicity.py @@ -1,15 +1,20 @@ from django.shortcuts import render, redirect from django.urls import reverse -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from .decorators.participant_decorators import require_participant from ..forms.ethnicity_form import EthnicityForm -@require_http_methods(["GET", "POST"]) -@require_participant -def ethnicity(request): +@method_decorator(require_participant, name="dispatch") +class EthnicityView(View): + def get(self, request): + return render_template( + request, + EthnicityForm(participant=request.participant) + ) - if request.method == "POST": + def post(self, request): form = EthnicityForm( participant=request.participant, data=request.POST @@ -27,10 +32,6 @@ def ethnicity(request): status=422 ) - return render_template( - request, - EthnicityForm(participant=request.participant) - ) def render_template(request, form, status=200): return render( diff --git a/lung_cancer_screening/questions/views/gender.py b/lung_cancer_screening/questions/views/gender.py index fc5acdb6..482f5469 100644 --- a/lung_cancer_screening/questions/views/gender.py +++ b/lung_cancer_screening/questions/views/gender.py @@ -1,14 +1,20 @@ from django.shortcuts import render, redirect from django.urls import reverse -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from .decorators.participant_decorators import require_participant from ..forms.gender_form import GenderForm -@require_http_methods(["GET", "POST"]) -@require_participant -def gender(request): - if request.method == "POST": +@method_decorator(require_participant, name="dispatch") +class GenderView(View): + def get(self, request): + return render_template( + request, + GenderForm(participant=request.participant), + ) + + def post(self, request): form = GenderForm( participant=request.participant, data=request.POST @@ -26,11 +32,6 @@ def gender(request): status=422 ) - return render_template( - request, - GenderForm(participant=request.participant), - ) - def render_template(request, form, status=200): return render( request, diff --git a/lung_cancer_screening/questions/views/have_you_ever_smoked.py b/lung_cancer_screening/questions/views/have_you_ever_smoked.py index 8da6a556..09a95881 100644 --- a/lung_cancer_screening/questions/views/have_you_ever_smoked.py +++ b/lung_cancer_screening/questions/views/have_you_ever_smoked.py @@ -1,15 +1,21 @@ from django.shortcuts import render, redirect from django.urls import reverse -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from .decorators.participant_decorators import require_participant from ..forms.have_you_ever_smoked_form import HaveYouEverSmokedForm from ..models.response_set import HaveYouEverSmokedValues -@require_http_methods(["GET", "POST"]) -@require_participant -def have_you_ever_smoked(request): - if request.method == "POST": +@method_decorator(require_participant, name="dispatch") +class HaveYouEverSmokedView(View): + def get(self, request): + return render_template( + request, + HaveYouEverSmokedForm(participant=request.participant) + ) + + def post(self, request): form = HaveYouEverSmokedForm( data=request.POST, participant=request.participant ) @@ -30,15 +36,10 @@ def have_you_ever_smoked(request): else: return render_template( request, - HaveYouEverSmokedForm(participant=request.participant), + form, status=422 ) - return render_template( - request, - HaveYouEverSmokedForm(participant=request.participant) - ) - def render_template(request, form, status=200): return render( diff --git a/lung_cancer_screening/questions/views/height.py b/lung_cancer_screening/questions/views/height.py index c69c8e3b..72cb515e 100644 --- a/lung_cancer_screening/questions/views/height.py +++ b/lung_cancer_screening/questions/views/height.py @@ -1,46 +1,51 @@ from django.shortcuts import render, redirect -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from lung_cancer_screening.questions.forms.metric_height_form import MetricHeightForm from lung_cancer_screening.questions.forms.imperial_height_form import ImperialHeightForm from .decorators.participant_decorators import require_participant -@require_http_methods(["GET", "POST"]) -@require_participant -def height(request): - unit = request.GET.get('unit') - form_klass = ImperialHeightForm if unit == "imperial" else MetricHeightForm - - if request.method == "POST": - form = form_klass( - instance = request.participant.responseset_set.last(), - data=request.POST, - participant=request.participant +@method_decorator(require_participant, name="dispatch") +class HeightView(View): + def get(self, request): + unit = request.GET.get('unit') + form_klass = ImperialHeightForm if unit == "imperial" else MetricHeightForm + + return render( + request, + "height.jinja", + { + "form": form_klass(participant=request.participant), + "unit": unit, + "switch_to_unit": "metric" if unit == "imperial" else "imperial" + } ) - if form.is_valid(): - form.save() - - return redirect("questions:weight") - else: - return render( - request, - "height.jinja", - { - "form": form, - "unit": unit, - "switch_to_unit": "metric" if unit == "imperial" else "imperial" - }, - status=422 + + def post(self, request): + unit = request.GET.get('unit') + form_klass = ImperialHeightForm if unit == "imperial" else MetricHeightForm + + if request.method == "POST": + form = form_klass( + instance = request.participant.responseset_set.last(), + data=request.POST, + participant=request.participant ) - return render( - request, - "height.jinja", - { - "form": form_klass(participant=request.participant), - "unit": unit, - "switch_to_unit": "metric" if unit == "imperial" else "imperial" - } - ) + if form.is_valid(): + form.save() + return redirect("questions:weight") + else: + return render( + request, + "height.jinja", + { + "form": form, + "unit": unit, + "switch_to_unit": "metric" if unit == "imperial" else "imperial" + }, + status=422 + ) diff --git a/lung_cancer_screening/questions/views/responses.py b/lung_cancer_screening/questions/views/responses.py index 844ece6c..335e16dd 100644 --- a/lung_cancer_screening/questions/views/responses.py +++ b/lung_cancer_screening/questions/views/responses.py @@ -1,23 +1,24 @@ from django.shortcuts import render, redirect from django.urls import reverse from django.utils import timezone -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from .decorators.participant_decorators import require_participant -@require_http_methods(["GET", "POST"]) -@require_participant -def responses(request): - response_set = request.participant.responseset_set.last() +@method_decorator(require_participant, name="dispatch") +class ResponsesView(View): + def get(self, request): + return render( + request, + "responses.jinja", + {"response_set": request.participant.responseset_set.last()} + ) + + def post(self, request): + response_set = request.participant.responseset_set.last() - if request.method == "POST": response_set.submitted_at = timezone.now() response_set.save() return redirect(reverse("questions:your_results")) - - return render( - request, - "responses.jinja", - {"response_set": response_set} - ) diff --git a/lung_cancer_screening/questions/views/sex_at_birth.py b/lung_cancer_screening/questions/views/sex_at_birth.py index 35dbbd6f..a95af16b 100644 --- a/lung_cancer_screening/questions/views/sex_at_birth.py +++ b/lung_cancer_screening/questions/views/sex_at_birth.py @@ -1,14 +1,20 @@ from django.shortcuts import render, redirect from django.urls import reverse -from django.views.decorators.http import require_http_methods +from django.views import View +from django.utils.decorators import method_decorator from .decorators.participant_decorators import require_participant from ..forms.sex_at_birth_form import SexAtBirthForm -@require_http_methods(["GET", "POST"]) -@require_participant -def sex_at_birth(request): - if request.method == "POST": +@method_decorator(require_participant, name="dispatch") +class SexAtBirthView(View): + def get(self, request): + return render_template( + request, + SexAtBirthForm(participant=request.participant) + ) + + def post(self, request): form = SexAtBirthForm( participant=request.participant, data=request.POST @@ -26,10 +32,6 @@ def sex_at_birth(request): status=422 ) - return render_template( - request, - SexAtBirthForm(participant=request.participant) - ) def render_template(request, form, status=200): return render( diff --git a/lung_cancer_screening/questions/views/start.py b/lung_cancer_screening/questions/views/start.py index 2521785d..55421d41 100644 --- a/lung_cancer_screening/questions/views/start.py +++ b/lung_cancer_screening/questions/views/start.py @@ -1,13 +1,18 @@ from django.shortcuts import render, redirect from django.urls import reverse from django.core.exceptions import ValidationError -from django.views.decorators.http import require_http_methods +from django.views import View from lung_cancer_screening.questions.models.participant import Participant -@require_http_methods(["GET", "POST"]) -def start(request): - if request.method == "POST": +class StartView(View): + def get(self, request): + return render( + request, + "start.jinja" + ) + + def post(self, request): try: participant, _ = Participant.objects.get_or_create( unique_id=request.POST['participant_id'] @@ -24,9 +29,3 @@ def start(request): {"error_messages": [{ "text": message } for message in e.messages ]}, status=422 ) - - else: - return render( - request, - "start.jinja" - ) diff --git a/lung_cancer_screening/questions/views/weight.py b/lung_cancer_screening/questions/views/weight.py index 07716074..51b19c6c 100644 --- a/lung_cancer_screening/questions/views/weight.py +++ b/lung_cancer_screening/questions/views/weight.py @@ -1,20 +1,37 @@ from django.shortcuts import render, redirect +from django.views import View +from django.utils.decorators import method_decorator from lung_cancer_screening.questions.forms.metric_weight_form import MetricWeightForm from lung_cancer_screening.questions.forms.imperial_weight_form import ImperialWeightForm from .decorators.participant_decorators import require_participant -@require_participant -def weight(request): - unit = request.GET.get('unit') - form_klass = ImperialWeightForm if unit == "imperial" else MetricWeightForm +@method_decorator(require_participant, name="dispatch") +class WeightView(View): + def get(self, request): + unit = request.GET.get('unit') + form_klass = ImperialWeightForm if unit == "imperial" else MetricWeightForm + + return render( + request, + "weight.jinja", + { + "form": form_klass(participant=request.participant), + "unit": unit, + "switch_to_unit": "metric" if unit == "imperial" else "imperial" + } + ) + + def post(self, request): + unit = request.GET.get('unit') + form_klass = ImperialWeightForm if unit == "imperial" else MetricWeightForm - if request.method == "POST": form = form_klass( instance=request.participant.responseset_set.last(), data=request.POST, participant=request.participant ) + if form.is_valid(): form.save() return redirect("questions:sex_at_birth") @@ -29,12 +46,3 @@ def weight(request): }, status=422 ) - return render( - request, - "weight.jinja", - { - "form": form_klass(participant=request.participant), - "unit": unit, - "switch_to_unit": "metric" if unit == "imperial" else "imperial" - } - ) diff --git a/lung_cancer_screening/questions/views/your_results.py b/lung_cancer_screening/questions/views/your_results.py index 4188d15e..ce895902 100644 --- a/lung_cancer_screening/questions/views/your_results.py +++ b/lung_cancer_screening/questions/views/your_results.py @@ -1,7 +1,7 @@ from django.shortcuts import render -from django.views.decorators.http import require_http_methods +from django.views.decorators.http import require_GET -@require_http_methods(["GET"]) +@require_GET def your_results(request): return render( request,