From cc5c9f22e59ae56d7cae4609dd46d4df4470a555 Mon Sep 17 00:00:00 2001 From: Ethan Nguyen Date: Mon, 19 Apr 2021 22:44:41 -0400 Subject: [PATCH] feat: validate test scores --- tjdests/apps/profile/forms.py | 38 ++++++++++++++++++++++++++++++++++- tjdests/apps/profile/views.py | 6 +++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/tjdests/apps/profile/forms.py b/tjdests/apps/profile/forms.py index 03b933c..78f4ac9 100644 --- a/tjdests/apps/profile/forms.py +++ b/tjdests/apps/profile/forms.py @@ -1,10 +1,12 @@ +from typing import Dict, Any + from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit from django import forms from tjdests.apps.authentication.models import User -from tjdests.apps.destinations.models import Decision +from tjdests.apps.destinations.models import Decision, TestScore class ProfilePublishForm(forms.ModelForm): @@ -33,3 +35,37 @@ class DecisionForm(forms.ModelForm): class Meta: model = Decision fields = ["college", "decision_type", "admission_status"] + + +class TestScoreForm(forms.ModelForm): + class Meta: + model = TestScore + fields = ["exam_type", "exam_score"] + + def clean(self) -> Dict[str, Any]: + cleaned_data = super().clean() + + exam_score = int(cleaned_data.get("exam_score")) + + # ACT is 1-36 + if cleaned_data.get("exam_type").startswith("ACT_"): + if not 1 <= exam_score <= 36: + self.add_error("exam_score", "This is not a valid ACT exam score") + + # SAT2 and SAT EBRW/Math sections are 200-800 and mod 10 + elif cleaned_data.get("exam_type").startswith("SAT2_") or cleaned_data.get("exam_type") in ["SAT_EBRW", "SAT_MATH"]: + if not 200 <= exam_score <= 800 or not exam_score % 10 == 0: + self.add_error("exam_score", "This is not a valid SAT section exam score") + + # SAT total is 400-1600 mod 10 + elif cleaned_data.get("exam_type") == "SAT_TOTAL": + if not 400 <= exam_score <= 1600 or not exam_score % 10 == 0: + self.add_error("exam_score", "This is not a valid SAT exam score") + + # AP is 1-5 + if cleaned_data.get("exam_type").startswith("AP_"): + if not 1 <= exam_score <= 36: + self.add_error("exam_score", "This is not a valid AP exam score") + + return cleaned_data + diff --git a/tjdests/apps/profile/views.py b/tjdests/apps/profile/views.py index 8869a73..73d381e 100644 --- a/tjdests/apps/profile/views.py +++ b/tjdests/apps/profile/views.py @@ -10,7 +10,7 @@ from django.views.generic import CreateView, DeleteView, UpdateView from tjdests.apps.authentication.decorators import require_accept_tos from tjdests.apps.destinations.models import Decision, TestScore -from .forms import ProfilePublishForm +from .forms import ProfilePublishForm, TestScoreForm @login_required @@ -43,7 +43,7 @@ class TestScoreCreateView( LoginRequiredMixin, SuccessMessageMixin, UserPassesTestMixin, CreateView ): model = TestScore - fields = ["exam_type", "exam_score"] + form_class = TestScoreForm template_name = "profile/testscore_form.html" success_message = "Test score created successfully." @@ -62,7 +62,7 @@ class TestScoreUpdateView( LoginRequiredMixin, SuccessMessageMixin, UserPassesTestMixin, UpdateView ): model = TestScore - fields = ["exam_type", "exam_score"] + form_class = TestScoreForm template_name = "profile/testscore_form.html" success_message = "Test score updated successfully."