diff --git a/Main/forms.py b/Main/forms.py index 2b55968..c574320 100644 --- a/Main/forms.py +++ b/Main/forms.py @@ -5,7 +5,8 @@ from json import JSONDecoder, JSONDecodeError from typing import List -from django.forms import ModelForm, TextInput, Textarea +from django.db.models import Q +from django.forms import ModelForm, TextInput, Textarea, ValidationError from django.forms.fields import CharField from jsonschema.validators import Draft202012Validator @@ -75,6 +76,20 @@ class CreateReviewForm(ModelForm): model = models.Review fields = ['schoology_id', 'rubric'] + def clean(self) -> dict: + """ + This function is run to validate form data. + It ensures that a student can only have two requested reviews at once. + + :returns: The cleaned data of the form + :rtype: dict + """ + + if self.user is not None and models.Review.objects.filter(student=self.user).filter( + Q(status=models.Review.Status.OPEN) | Q(status=models.Review.Status.ASSIGNED)).count() >= 2: + raise ValidationError("You can only have 2 requested reviews at once.") + return super(CreateReviewForm, self).clean() + def save(self, commit=True): """ This function is run to save the new Review diff --git a/Main/templates/form_base.html b/Main/templates/form_base.html index ae0a584..112d630 100644 --- a/Main/templates/form_base.html +++ b/Main/templates/form_base.html @@ -29,20 +29,6 @@ - {% block errorHandling %} - {# This block can be overidden to change how non-field errors are handled #} - {% if form.non_field_errors %} -