2020-05-05 00:20:45 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
from django import forms
|
2020-05-05 02:45:38 +00:00
|
|
|
from django.db.models import Q
|
2020-05-05 14:07:53 +00:00
|
|
|
from django.utils import timezone
|
2020-05-04 18:21:53 +00:00
|
|
|
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
|
2020-05-05 02:45:38 +00:00
|
|
|
from tournament.models import Tournament, Team, Pool
|
2020-05-04 18:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TournamentForm(forms.ModelForm):
|
2020-05-05 02:45:38 +00:00
|
|
|
organizers = forms.ModelMultipleChoiceField(
|
|
|
|
TFJMUser.objects.filter(Q(role="0admin") | Q(role="1volunteer")).order_by('role'),
|
|
|
|
label=_("Organizers"),
|
|
|
|
)
|
|
|
|
|
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
|
2020-05-05 14:04:53 +00:00
|
|
|
exclude = ('year',)
|
2020-05-04 18:21:53 +00:00
|
|
|
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
|
2020-05-05 15:40:57 +00:00
|
|
|
print(user)
|
|
|
|
print(user.pk)
|
2020-05-04 18:21:53 +00:00
|
|
|
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(
|
2020-05-05 14:07:53 +00:00
|
|
|
Tournament.objects.filter(date_inscription__gte=timezone.now().date(), final=False),
|
2020-05-05 00:20:45 +00:00
|
|
|
)
|
|
|
|
|
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."))
|
|
|
|
|
2020-05-05 14:07:53 +00:00
|
|
|
if cleaned_data["tournament"].date_inscription < timezone.now().date():
|
2020-05-05 00:20:45 +00:00
|
|
|
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',)
|
2020-05-05 02:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PoolForm(forms.ModelForm):
|
|
|
|
team1 = forms.ModelChoiceField(
|
|
|
|
Team.objects.filter(validation_status="2valid").all(),
|
|
|
|
empty_label=_("Choose a team..."),
|
|
|
|
label=_("Team 1"),
|
|
|
|
)
|
|
|
|
|
|
|
|
problem1 = forms.IntegerField(
|
|
|
|
min_value=1,
|
|
|
|
max_value=8,
|
|
|
|
initial=1,
|
|
|
|
label=_("Problem defended by team 1"),
|
|
|
|
)
|
|
|
|
|
|
|
|
team2 = forms.ModelChoiceField(
|
|
|
|
Team.objects.filter(validation_status="2valid").all(),
|
|
|
|
empty_label=_("Choose a team..."),
|
|
|
|
label=_("Team 2"),
|
|
|
|
)
|
|
|
|
|
|
|
|
problem2 = forms.IntegerField(
|
|
|
|
min_value=1,
|
|
|
|
max_value=8,
|
|
|
|
initial=2,
|
|
|
|
label=_("Problem defended by team 2"),
|
|
|
|
)
|
|
|
|
|
|
|
|
team3 = forms.ModelChoiceField(
|
|
|
|
Team.objects.filter(validation_status="2valid").all(),
|
|
|
|
empty_label=_("Choose a team..."),
|
|
|
|
label=_("Team 3"),
|
|
|
|
)
|
|
|
|
|
|
|
|
problem3 = forms.IntegerField(
|
|
|
|
min_value=1,
|
|
|
|
max_value=8,
|
|
|
|
initial=3,
|
|
|
|
label=_("Problem defended by team 3"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
|
|
|
|
team1, pb1 = cleaned_data["team1"], cleaned_data["problem1"]
|
|
|
|
team2, pb2 = cleaned_data["team2"], cleaned_data["problem2"]
|
|
|
|
team3, pb3 = cleaned_data["team3"], cleaned_data["problem3"]
|
|
|
|
|
|
|
|
sol1 = Solution.objects.get(team=team1, problem=pb1, final=team1.selected_for_final)
|
|
|
|
sol2 = Solution.objects.get(team=team2, problem=pb2, final=team2.selected_for_final)
|
|
|
|
sol3 = Solution.objects.get(team=team3, problem=pb3, final=team3.selected_for_final)
|
|
|
|
|
|
|
|
cleaned_data["teams"] = [team1, team2, team3]
|
|
|
|
cleaned_data["solutions"] = [sol1, sol2, sol3]
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
pool = super().save(commit)
|
|
|
|
|
|
|
|
pool.refresh_from_db()
|
|
|
|
pool.teams.set(self.cleaned_data["teams"])
|
|
|
|
pool.solutions.set(self.cleaned_data["solutions"])
|
|
|
|
pool.save()
|
|
|
|
|
|
|
|
return pool
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Pool
|
|
|
|
fields = ('round', 'juries',)
|