fix: fix bug introduced in cc5c9f22e5

Also clean up
This commit is contained in:
Ethan Nguyen 2021-04-19 22:51:40 -04:00
parent cc5c9f22e5
commit 2debd182db
No known key found for this signature in database
GPG Key ID: B4CA5339AF911920

View File

@ -1,4 +1,4 @@
from typing import Dict, Any
from typing import Any, Dict
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
@ -45,27 +45,37 @@ class TestScoreForm(forms.ModelForm):
def clean(self) -> Dict[str, Any]:
cleaned_data = super().clean()
exam_score = int(cleaned_data.get("exam_score"))
exam_type = str(cleaned_data.get("exam_type"))
# check if exam score is an integer
try:
exam_score = int(cleaned_data.get("exam_score")) # type: ignore
except (TypeError, ValueError):
pass
else:
# ACT is 1-36
if cleaned_data.get("exam_type").startswith("ACT_"):
if 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"]:
elif 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")
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":
elif 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 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