import re from bootstrap_datepicker_plus import DateTimePickerInput from django import forms from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ from .models import Participation, Phase, Team, Video class TeamForm(forms.ModelForm): def clean_trigram(self): trigram = self.cleaned_data["trigram"].upper() if not re.match("[A-Z]{3}", trigram): raise ValidationError(_("The trigram must be composed of three uppercase letters.")) return trigram class Meta: model = Team fields = ('name', 'trigram', 'grant_animath_access_videos',) class JoinTeamForm(forms.ModelForm): def clean_access_code(self): access_code = self.cleaned_data["access_code"] if not Team.objects.filter(access_code=access_code).exists(): raise ValidationError(_("No team was found with this access code.")) return access_code def clean(self): cleaned_data = super().clean() if "access_code" in cleaned_data: team = Team.objects.get(access_code=cleaned_data["access_code"]) self.instance = team return cleaned_data class Meta: model = Team fields = ('access_code',) class ParticipationForm(forms.ModelForm): class Meta: model = Participation fields = ('problem',) class RequestValidationForm(forms.Form): _form_type = forms.CharField( initial="RequestValidationForm", widget=forms.HiddenInput(), ) engagement = forms.BooleanField( label=_("I engage myself to participate to the whole \"Correspondances\"."), required=True, ) class ValidateParticipationForm(forms.Form): _form_type = forms.CharField( initial="ValidateParticipationForm", widget=forms.HiddenInput(), ) message = forms.CharField( label=_("Message to address to the team:"), widget=forms.Textarea(), ) class UploadVideoForm(forms.ModelForm): class Meta: model = Video fields = ('link',) def clean(self): if Phase.current_phase().phase_number != 1 and self.instance.link: self.add_error("link", _("You can't upload your video after the deadline.")) return super().clean() class PhaseForm(forms.ModelForm): class Meta: model = Phase fields = ('start', 'end',) widgets = { 'start': DateTimePickerInput(format='%d/%m/%Y %H:%M'), 'end': DateTimePickerInput(format='%d/%m/%Y %H:%M'), } def clean(self): # Ensure that dates are in a right order cleaned_data = super().clean() if cleaned_data["end"] <= cleaned_data["start"]: self.add_error("end", _("Start date must be before the end date.")) if Phase.objects.filter(phase_number__lt=self.instance.phase_number, end__gt=cleaned_data["start"]).exists(): self.add_error("start", _("This phase must start after the previous phases.")) if Phase.objects.filter(phase_number__gt=self.instance.phase_number, start__lt=cleaned_data["end"]).exists(): self.add_error("end", _("This phase must end after the next phases.")) return cleaned_data