1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-27 06:13:01 +00:00
plateforme-tfjm2/apps/participation/forms.py

235 lines
8.5 KiB
Python
Raw Normal View History

2020-12-27 10:49:54 +00:00
# Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later
import re
2022-02-04 13:54:40 +00:00
from bootstrap_datepicker_plus.widgets import DatePickerInput, DateTimePickerInput
2020-12-27 10:49:54 +00:00
from django import forms
2020-12-28 17:52:50 +00:00
from django.core.exceptions import ValidationError
2020-12-31 11:13:42 +00:00
from django.utils import formats
2020-12-27 10:49:54 +00:00
from django.utils.translation import gettext_lazy as _
2021-01-21 21:12:36 +00:00
from PyPDF3 import PdfFileReader
2020-12-27 10:49:54 +00:00
2022-02-04 14:01:15 +00:00
from registration.models import VolunteerRegistration
2021-01-17 15:23:48 +00:00
from .models import Note, Participation, Passage, Pool, Solution, Synthesis, Team, Tournament
2020-12-27 10:49:54 +00:00
class TeamForm(forms.ModelForm):
"""
2021-01-12 14:42:32 +00:00
Form to create a team, with the name and the trigram,...
2020-12-27 10:49:54 +00:00
"""
2021-02-07 16:39:24 +00:00
def clean_name(self):
if "name" in self.cleaned_data:
2022-02-04 14:40:45 +00:00
name = self.cleaned_data["name"]
2021-02-07 16:39:24 +00:00
if not self.instance.pk and Team.objects.filter(name=name).exists():
raise ValidationError(_("This name is already used."))
return name
2020-12-27 10:49:54 +00:00
def clean_trigram(self):
2021-02-07 16:39:24 +00:00
if "trigram" in self.cleaned_data:
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."))
if not self.instance.pk and Team.objects.filter(trigram=trigram).exists():
raise ValidationError(_("This trigram is already used."))
return trigram
2020-12-27 10:49:54 +00:00
class Meta:
model = Team
2020-12-28 17:52:50 +00:00
fields = ('name', 'trigram',)
2020-12-27 10:49:54 +00:00
class JoinTeamForm(forms.ModelForm):
"""
Form to join a team by the access code.
"""
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):
"""
Form to update the problem of a team participation.
"""
class Meta:
model = Participation
fields = ('tournament', 'final',)
2020-12-27 10:49:54 +00:00
class MotivationLetterForm(forms.ModelForm):
def clean_file(self):
if "motivation_letter" in self.files:
file = self.files["motivation_letter"]
if file.size > 2e6:
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
if file.content_type not in ["application/pdf", "image/png", "image/jpeg"]:
raise ValidationError(_("The uploaded file must be a PDF, PNG of JPEG file."))
return self.cleaned_data["motivation_letter"]
class Meta:
model = Team
fields = ('motivation_letter',)
2020-12-27 10:49:54 +00:00
class RequestValidationForm(forms.Form):
"""
Form to ask about validation.
"""
_form_type = forms.CharField(
initial="RequestValidationForm",
widget=forms.HiddenInput(),
)
engagement = forms.BooleanField(
2021-01-18 14:52:09 +00:00
label=_("I engage myself to participate to the whole TFJM²."),
2020-12-27 10:49:54 +00:00
required=True,
)
class ValidateParticipationForm(forms.Form):
"""
Form to let administrators to accept or refuse a team.
"""
_form_type = forms.CharField(
initial="ValidateParticipationForm",
widget=forms.HiddenInput(),
)
message = forms.CharField(
label=_("Message to address to the team:"),
widget=forms.Textarea(),
)
2020-12-31 11:13:42 +00:00
class TournamentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["date_start"].widget = DatePickerInput(
format=formats.get_format_lazy(format_type="DATE_INPUT_FORMATS", use_l10n=True)[0])
self.fields["date_end"].widget = DatePickerInput(
format=formats.get_format_lazy(format_type="DATE_INPUT_FORMATS", use_l10n=True)[0])
self.fields["inscription_limit"].widget = DateTimePickerInput(
format=formats.get_format_lazy(format_type="DATETIME_INPUT_FORMATS", use_l10n=True)[0])
self.fields["solution_limit"].widget = DateTimePickerInput(
format=formats.get_format_lazy(format_type="DATETIME_INPUT_FORMATS", use_l10n=True)[0])
2021-01-01 11:11:09 +00:00
self.fields["solutions_draw"].widget = DateTimePickerInput(
format=formats.get_format_lazy(format_type="DATETIME_INPUT_FORMATS", use_l10n=True)[0])
2020-12-31 11:13:42 +00:00
self.fields["syntheses_first_phase_limit"].widget = DateTimePickerInput(
format=formats.get_format_lazy(format_type="DATETIME_INPUT_FORMATS", use_l10n=True)[0])
2021-01-01 11:11:09 +00:00
self.fields["solutions_available_second_phase"].widget = DateTimePickerInput(
format=formats.get_format_lazy(format_type="DATETIME_INPUT_FORMATS", use_l10n=True)[0])
2020-12-31 11:13:42 +00:00
self.fields["syntheses_second_phase_limit"].widget = DateTimePickerInput(
format=formats.get_format_lazy(format_type="DATETIME_INPUT_FORMATS", use_l10n=True)[0])
self.fields["organizers"].widget = forms.CheckboxSelectMultiple()
2022-02-04 14:01:15 +00:00
self.fields["organizers"].queryset = VolunteerRegistration.objects.all()
2020-12-31 11:13:42 +00:00
class Meta:
model = Tournament
fields = '__all__'
2021-01-12 16:24:46 +00:00
class SolutionForm(forms.ModelForm):
def clean_file(self):
if "file" in self.files:
file = self.files["file"]
2021-01-18 23:33:44 +00:00
if file.size > 5e6:
raise ValidationError(_("The uploaded file size must be under 5 Mo."))
if file.content_type != "application/pdf":
raise ValidationError(_("The uploaded file must be a PDF file."))
pdf_reader = PdfFileReader(file)
pages = len(pdf_reader.pages)
if pages > 30:
raise ValidationError(_("The PDF file must not have more than 30 pages."))
2021-04-03 20:02:53 +00:00
return self.cleaned_data["file"]
2021-01-12 16:24:46 +00:00
def save(self, commit=True):
"""
Don't save a solution with this way. Use a view instead
"""
class Meta:
model = Solution
fields = ('problem', 'file',)
2021-01-13 16:00:50 +00:00
class PoolForm(forms.ModelForm):
class Meta:
model = Pool
2021-04-10 08:02:49 +00:00
fields = ('tournament', 'round', 'bbb_url', 'results_available', 'juries',)
2021-01-14 13:44:12 +00:00
widgets = {
"juries": forms.CheckboxSelectMultiple,
}
class PoolTeamsForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["participations"].queryset = self.instance.tournament.participations.all()
class Meta:
model = Pool
fields = ('participations',)
widgets = {
"participations": forms.CheckboxSelectMultiple,
}
2021-01-14 14:59:11 +00:00
class PassageForm(forms.ModelForm):
def clean(self):
cleaned_data = super().clean()
if "defender" in cleaned_data and "opponent" in cleaned_data and "reporter" in cleaned_data \
and len({cleaned_data["defender"], cleaned_data["opponent"], cleaned_data["reporter"]}) < 3:
self.add_error(None, _("The defender, the opponent and the reporter must be different."))
if "defender" in self.cleaned_data and "solution_number" in self.cleaned_data \
and not Solution.objects.filter(participation=cleaned_data["defender"],
problem=cleaned_data["solution_number"]).exists():
self.add_error("solution_number", _("This defender did not work on this problem."))
return cleaned_data
class Meta:
model = Passage
2021-04-04 11:35:45 +00:00
fields = ('solution_number', 'place', 'defender', 'opponent', 'reporter', 'defender_penalties',)
2021-01-14 16:26:08 +00:00
class SynthesisForm(forms.ModelForm):
2021-01-18 23:33:44 +00:00
def clean_file(self):
if "file" in self.files:
file = self.files["file"]
if file.size > 2e6:
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
if file.content_type != "application/pdf":
raise ValidationError(_("The uploaded file must be a PDF file."))
2021-04-03 20:02:53 +00:00
return self.cleaned_data["file"]
2021-01-18 23:33:44 +00:00
2021-01-14 16:26:08 +00:00
def save(self, commit=True):
"""
Don't save a synthesis with this way. Use a view instead
"""
class Meta:
model = Synthesis
2021-04-04 16:13:30 +00:00
fields = ('file',)
2021-01-14 17:21:22 +00:00
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('defender_writing', 'defender_oral', 'opponent_writing',
'opponent_oral', 'reporter_writing', 'reporter_oral', )