feat(profile): limit GPA to reasonable values

GPA must be 0 <= GPA <= 5
This commit is contained in:
Ethan Nguyen 2021-11-18 14:16:29 -06:00
parent f7a2dd1768
commit 23cda5d64d
No known key found for this signature in database
GPG Key ID: B4CA5339AF911920
2 changed files with 32 additions and 0 deletions

View File

@ -22,6 +22,18 @@ class ProfilePublishForm(forms.ModelForm):
user=self.instance, admission_status__contains="ADMIT" user=self.instance, admission_status__contains="ADMIT"
) )
def clean(self) -> Dict[str, Any]:
cleaned_data = super().clean()
# Check the GPA: 0.0 <= GPA <= 5.0
if cleaned_data.get("GPA"):
gpa = cleaned_data.get("GPA")
assert type(gpa) is float
if not 0.0 <= gpa <= 5.0:
self.add_error("GPA", "This is not a valid GPA")
return cleaned_data
class Meta: class Meta:
model = User model = User
fields = ["publish_data", "GPA", "biography", "attending_decision"] fields = ["publish_data", "GPA", "biography", "attending_decision"]

View File

@ -52,6 +52,26 @@ class ProfileTest(TJDestsTestCase):
).count(), ).count(),
) )
response = self.client.post(
reverse("profile:index"),
data={
"biography": "sdf",
"attending_decision": "",
"publish_data": False,
},
)
self.assertEqual(200, response.status_code)
self.assertEqual(
1,
User.objects.filter(
GPA=None,
id=user.id,
biography="sdf",
attending_decision=None,
publish_data=False,
).count(),
)
# Test creating an admitted decision, then setting that as our destination. # Test creating an admitted decision, then setting that as our destination.
college = College.objects.get_or_create(name="test college")[0] college = College.objects.get_or_create(name="test college")[0]
decision = Decision.objects.get_or_create( decision = Decision.objects.get_or_create(