2020-05-05 00:20:45 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from datetime import datetime
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2020-05-04 21:37:21 +00:00
|
|
|
from member.models import TFJMUser, Solution, Synthesis
|
2020-05-04 18:21:53 +00:00
|
|
|
from tfjm.inputs import DatePickerInput, DateTimePickerInput, AmountInput
|
|
|
|
from tournament.models import Tournament, Team
|
|
|
|
|
|
|
|
|
|
|
|
class TournamentForm(forms.ModelForm):
|
2020-05-05 00:20:45 +00:00
|
|
|
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
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
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',)
|
|
|
|
|
2020-05-05 00:20:45 +00:00
|
|
|
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
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
user = self.instance
|
|
|
|
user.role = '0admin' if user.is_superuser else '1organizer'
|
|
|
|
super().save(commit)
|
|
|
|
|
|
|
|
|
|
|
|
class TeamForm(forms.ModelForm):
|
2020-05-05 00:20:45 +00:00
|
|
|
tournament = forms.ModelChoiceField(
|
|
|
|
Tournament.objects.filter(date_inscription__gte=datetime.today(), final=False),
|
|
|
|
)
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
class Meta:
|
|
|
|
model = Team
|
|
|
|
fields = ('name', 'trigram', 'tournament',)
|
|
|
|
|
2020-05-05 00:20:45 +00:00
|
|
|
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
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
|
|
|
|
class JoinTeam(forms.Form):
|
|
|
|
access_code = forms.CharField(
|
|
|
|
label=_("Access code"),
|
|
|
|
max_length=6,
|
|
|
|
)
|
2020-05-04 21:37:21 +00:00
|
|
|
|
2020-05-04 22:56:34 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
|
2020-05-05 00:20:45 +00:00
|
|
|
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."))
|
|
|
|
|
2020-05-04 22:56:34 +00:00
|
|
|
team = Team.objects.filter(access_code=cleaned_data["access_code"])
|
|
|
|
if not team.exists():
|
|
|
|
self.add_error('access_code', _("This access code is invalid."))
|
2020-05-05 00:20:45 +00:00
|
|
|
team = team.get()
|
|
|
|
if not team.invalid:
|
|
|
|
self.add_error('access_code', _("The team is already validated."))
|
|
|
|
cleaned_data["team"] = team
|
2020-05-04 22:56:34 +00:00
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
2020-05-04 21:37:21 +00:00
|
|
|
|
|
|
|
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
|
2020-05-04 22:11:38 +00:00
|
|
|
fields = ('file', 'source', 'round',)
|