feat: validate test scores

This commit is contained in:
Ethan Nguyen 2021-04-19 22:44:41 -04:00
parent b4bdb01a1e
commit cc5c9f22e5
No known key found for this signature in database
GPG Key ID: B4CA5339AF911920
2 changed files with 40 additions and 4 deletions

View File

@ -1,10 +1,12 @@
from typing import Dict, Any
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit from crispy_forms.layout import Submit
from django import forms from django import forms
from tjdests.apps.authentication.models import User 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): class ProfilePublishForm(forms.ModelForm):
@ -33,3 +35,37 @@ class DecisionForm(forms.ModelForm):
class Meta: class Meta:
model = Decision model = Decision
fields = ["college", "decision_type", "admission_status"] 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

View File

@ -10,7 +10,7 @@ from django.views.generic import CreateView, DeleteView, UpdateView
from tjdests.apps.authentication.decorators import require_accept_tos from tjdests.apps.authentication.decorators import require_accept_tos
from tjdests.apps.destinations.models import Decision, TestScore from tjdests.apps.destinations.models import Decision, TestScore
from .forms import ProfilePublishForm from .forms import ProfilePublishForm, TestScoreForm
@login_required @login_required
@ -43,7 +43,7 @@ class TestScoreCreateView(
LoginRequiredMixin, SuccessMessageMixin, UserPassesTestMixin, CreateView LoginRequiredMixin, SuccessMessageMixin, UserPassesTestMixin, CreateView
): ):
model = TestScore model = TestScore
fields = ["exam_type", "exam_score"] form_class = TestScoreForm
template_name = "profile/testscore_form.html" template_name = "profile/testscore_form.html"
success_message = "Test score created successfully." success_message = "Test score created successfully."
@ -62,7 +62,7 @@ class TestScoreUpdateView(
LoginRequiredMixin, SuccessMessageMixin, UserPassesTestMixin, UpdateView LoginRequiredMixin, SuccessMessageMixin, UserPassesTestMixin, UpdateView
): ):
model = TestScore model = TestScore
fields = ["exam_type", "exam_score"] form_class = TestScoreForm
template_name = "profile/testscore_form.html" template_name = "profile/testscore_form.html"
success_message = "Test score updated successfully." success_message = "Test score updated successfully."