plateforme-corres2math/apps/participation/forms.py

75 lines
2.0 KiB
Python
Raw Normal View History

2020-09-23 21:20:44 +00:00
import re
2020-09-22 18:41:42 +00:00
from django import forms
from django.core.exceptions import ValidationError
2020-09-23 21:20:44 +00:00
from django.utils.translation import gettext_lazy as _
2020-09-22 18:41:42 +00:00
2020-09-27 12:32:05 +00:00
from .models import Participation, Team, Video
2020-09-22 18:41:42 +00:00
class TeamForm(forms.ModelForm):
2020-09-23 21:20:44 +00:00
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
2020-09-22 18:41:42 +00:00
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():
2020-09-23 21:20:44 +00:00
raise ValidationError(_("No team was found with this access code."))
2020-09-22 18:41:42 +00:00
return access_code
def clean(self):
cleaned_data = super().clean()
if "access_code" in cleaned_data:
2020-09-23 21:20:44 +00:00
team = Team.objects.get(access_code=cleaned_data["access_code"])
self.instance = team
2020-09-22 18:41:42 +00:00
return cleaned_data
class Meta:
model = Team
fields = ('access_code',)
2020-09-24 09:15:54 +00:00
class ParticipationForm(forms.ModelForm):
class Meta:
model = Participation
fields = ('problem',)
2020-09-27 12:32:05 +00:00
2020-10-11 16:44:42 +00:00
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(),
)
2020-09-27 12:32:05 +00:00
class UploadVideoForm(forms.ModelForm):
class Meta:
model = Video
fields = ('link',)