1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-30 04:13:06 +00:00
plateforme-tfjm2/apps/tournament/forms.py
2020-05-05 02:20:45 +02:00

125 lines
4.0 KiB
Python

import os
import re
from datetime import datetime
from django import forms
from django.utils.translation import gettext_lazy as _
from member.models import TFJMUser, Solution, Synthesis
from tfjm.inputs import DatePickerInput, DateTimePickerInput, AmountInput
from tournament.models import Tournament, Team
class TournamentForm(forms.ModelForm):
def clean(self):
cleaned_data = super().clean()
if not self.instance.pk:
if Tournament.objects.filter(name=cleaned_data["data"], year=os.getenv("TFJM_YEAR")):
self.add_error("name", _("This tournament already exists."))
if cleaned_data["final"] and Tournament.objects.filter(final=True, year=os.getenv("TFJM_YEAR")):
self.add_error("name", _("The final tournament was already defined."))
return cleaned_data
class Meta:
model = Tournament
fields = '__all__'
widgets = {
"price": AmountInput(),
"date_start": DatePickerInput(),
"date_end": DatePickerInput(),
"date_inscription": DateTimePickerInput(),
"date_solutions": DateTimePickerInput(),
"date_syntheses": DateTimePickerInput(),
}
class OrganizerForm(forms.ModelForm):
class Meta:
model = TFJMUser
fields = ('last_name', 'first_name', 'email', 'is_superuser',)
def clean(self):
cleaned_data = super().clean()
if TFJMUser.objects.filter(email=cleaned_data["email"], year=os.getenv("TFJM_YEAR")).exists():
self.add_error("trigram", _("This organizer already exist."))
return cleaned_data
def save(self, commit=True):
user = self.instance
user.role = '0admin' if user.is_superuser else '1organizer'
super().save(commit)
class TeamForm(forms.ModelForm):
tournament = forms.ModelChoiceField(
Tournament.objects.filter(date_inscription__gte=datetime.today(), final=False),
)
class Meta:
model = Team
fields = ('name', 'trigram', 'tournament',)
def clean(self):
cleaned_data = super().clean()
cleaned_data["trigram"] = cleaned_data["trigram"].upper()
if not re.match("[A-Z]{3}", cleaned_data["trigram"]):
self.add_error("trigram", _("The trigram must be composed of three upcase letters."))
if not self.instance.pk:
if Team.objects.filter(trigram=cleaned_data["trigram"], year=os.getenv("TFJM_YEAR")).exists():
self.add_error("trigram", _("This trigram is already used."))
if Team.objects.filter(name=cleaned_data["name"], year=os.getenv("TFJM_YEAR")).exists():
self.add_error("name", _("This name is already used."))
if cleaned_data["tournament"].date_inscription < datetime.today():
self.add_error("tournament", _("This tournament is already closed."))
return cleaned_data
class JoinTeam(forms.Form):
access_code = forms.CharField(
label=_("Access code"),
max_length=6,
)
def clean(self):
cleaned_data = super().clean()
if not re.match("[a-z0-9]{6}", cleaned_data["access_code"]):
self.add_error('access_code', _("The access code must be composed of 6 alphanumeric characters."))
team = Team.objects.filter(access_code=cleaned_data["access_code"])
if not team.exists():
self.add_error('access_code', _("This access code is invalid."))
team = team.get()
if not team.invalid:
self.add_error('access_code', _("The team is already validated."))
cleaned_data["team"] = team
return cleaned_data
class SolutionForm(forms.ModelForm):
problem = forms.ChoiceField(
label=_("Problem"),
choices=[(str(i), _("Problem #{problem:d}").format(problem=i)) for i in range(1, 9)],
)
class Meta:
model = Solution
fields = ('file', 'problem',)
class SynthesisForm(forms.ModelForm):
class Meta:
model = Synthesis
fields = ('file', 'source', 'round',)