1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-07-05 09:54:13 +02:00

Comment code, fix minor issues

This commit is contained in:
Yohann D'ANELLO
2020-05-11 14:08:19 +02:00
parent c9b9d01523
commit a561364bd0
22 changed files with 650 additions and 179 deletions

View File

@ -9,6 +9,10 @@ from django.utils.translation import gettext_lazy as _
class Tournament(models.Model):
"""
Store the information of a tournament.
"""
name = models.CharField(
max_length=255,
verbose_name=_("name"),
@ -18,10 +22,12 @@ class Tournament(models.Model):
'member.TFJMUser',
related_name="organized_tournaments",
verbose_name=_("organizers"),
help_text=_("List of all organizers that can see and manipulate data of the tournament and the teams."),
)
size = models.PositiveSmallIntegerField(
verbose_name=_("size"),
help_text=_("Number of teams that are allowed to join the tournament."),
)
place = models.CharField(
@ -31,6 +37,7 @@ class Tournament(models.Model):
price = models.PositiveSmallIntegerField(
verbose_name=_("price"),
help_text=_("Price asked to participants. Free with a scholarship."),
)
description = models.TextField(
@ -74,6 +81,7 @@ class Tournament(models.Model):
final = models.BooleanField(
verbose_name=_("final tournament"),
help_text=_("It should be only one final tournament."),
)
year = models.PositiveIntegerField(
@ -83,27 +91,43 @@ class Tournament(models.Model):
@property
def teams(self):
"""
Get all teams that are registered to this tournament, with a distinction for the final tournament.
"""
return self._teams if not self.final else Team.objects.filter(selected_for_final=True)
@property
def linked_organizers(self):
"""
Display a list of the organizers with links to their personal page.
"""
return ['<a href="{url}">'.format(url=reverse_lazy("member:information", args=(user.pk,))) + str(user) + '</a>'
for user in self.organizers.all()]
@property
def solutions(self):
"""
Get all sent solutions for this tournament.
"""
from member.models import Solution
return Solution.objects.filter(final=self.final) if self.final \
else Solution.objects.filter(team__tournament=self)
else Solution.objects.filter(team__tournament=self, final=False)
@property
def syntheses(self):
"""
Get all sent syntheses for this tournament.
"""
from member.models import Synthesis
return Synthesis.objects.filter(final=self.final) if self.final \
else Synthesis.objects.filter(team__tournament=self)
else Synthesis.objects.filter(team__tournament=self, final=False)
@classmethod
def get_final(cls):
"""
Get the final tournament.
This should exist and be unique.
"""
return cls.objects.get(year=os.getenv("TFJM_YEAR"), final=True)
class Meta:
@ -111,6 +135,12 @@ class Tournament(models.Model):
verbose_name_plural = _("tournaments")
def send_mail_to_organizers(self, template_name, subject="Contact TFJM²", **kwargs):
"""
Send a mail to all organizers of the tournament.
The template of the mail should be found either in templates/mail_templates/<template_name>.html for the HTML
version and in templates/mail_templates/<template_name>.txt for the plain text version.
The context of the template contains the tournament and the user. Extra context can be given through the kwargs.
"""
context = kwargs
context["tournament"] = self
for user in self.organizers.all():
@ -130,6 +160,10 @@ class Tournament(models.Model):
class Team(models.Model):
"""
Store information about a registered team.
"""
name = models.CharField(
max_length=255,
verbose_name=_("name"),
@ -138,6 +172,7 @@ class Team(models.Model):
trigram = models.CharField(
max_length=3,
verbose_name=_("trigram"),
help_text=_("The trigram should be composed of 3 capitalize letters, that is a funny acronym for the team."),
)
tournament = models.ForeignKey(
@ -145,6 +180,7 @@ class Team(models.Model):
on_delete=models.PROTECT,
related_name="_teams",
verbose_name=_("tournament"),
help_text=_("The tournament where the team is registered."),
)
inscription_date = models.DateTimeField(
@ -191,31 +227,59 @@ class Team(models.Model):
return self.validation_status == "0invalid"
@property
def encadrants(self):
def coaches(self):
"""
Get all coaches of a team.
"""
return self.users.all().filter(role="2coach")
@property
def linked_encadrants(self):
def linked_coaches(self):
"""
Get a list of the coaches of a team with html links to their pages.
"""
return ['<a href="{url}">'.format(url=reverse_lazy("member:information", args=(user.pk,))) + str(user) + '</a>'
for user in self.encadrants]
for user in self.coaches]
@property
def participants(self):
"""
Get all particpants of a team, coaches excluded.
"""
return self.users.all().filter(role="3participant")
@property
def linked_participants(self):
"""
Get a list of the participants of a team with html links to their pages.
"""
return ['<a href="{url}">'.format(url=reverse_lazy("member:information", args=(user.pk,))) + str(user) + '</a>'
for user in self.participants]
@property
def future_tournament(self):
"""
Get the last tournament where the team is registered.
Only matters if the team is selected for final: if this is the case, we return the final tournament.
Useful for deadlines.
"""
return Tournament.get_final() if self.selected_for_final else self.tournament
@property
def can_validate(self):
"""
Check if a given team is able to ask for validation.
A team can validate if:
* All participants filled the photo consent
* Minor participants filled the parental consent
* Minor participants filled the sanitary plug
* Teams sent their motivation letter
* The team contains at least 4 participants
* The team contains at least 1 coach
"""
# TODO In a normal time, team needs a motivation letter and authorizations.
return self.encadrants.exists() and self.participants.count() >= 4
return self.coaches.exists() and self.participants.count() >= 4\
and self.tournament.date_inscription <= timezone.now()
class Meta:
verbose_name = _("team")
@ -223,6 +287,12 @@ class Team(models.Model):
unique_together = (('name', 'year',), ('trigram', 'year',),)
def send_mail(self, template_name, subject="Contact TFJM²", **kwargs):
"""
Send a mail to all members of a team with a given template.
The template of the mail should be found either in templates/mail_templates/<template_name>.html for the HTML
version and in templates/mail_templates/<template_name>.txt for the plain text version.
The context of the template contains the team and the user. Extra context can be given through the kwargs.
"""
context = kwargs
context["team"] = self
for user in self.users.all():
@ -236,6 +306,12 @@ class Team(models.Model):
class Pool(models.Model):
"""
Store information of a pool.
A pool is only a list of accessible solutions to some teams and some juries.
TODO: check that the set of teams is equal to the set of the teams that have a solution in this set.
TODO: Moreover, a team should send only one solution.
"""
teams = models.ManyToManyField(
Team,
related_name="pools",
@ -264,14 +340,24 @@ class Pool(models.Model):
@property
def problems(self):
"""
Get problem numbers of the sent solutions as a list of integers.
"""
return list(d["problem"] for d in self.solutions.values("problem").all())
@property
def tournament(self):
"""
Get the concerned tournament.
We assume that the pool is correct, so all solutions belong to the same tournament.
"""
return self.solutions.first().tournament
@property
def syntheses(self):
"""
Get the syntheses of the teams that are in this pool, for the correct round.
"""
from member.models import Synthesis
return Synthesis.objects.filter(team__in=self.teams.all(), round=self.round, final=self.tournament.final)
@ -281,6 +367,10 @@ class Pool(models.Model):
class Payment(models.Model):
"""
Store some information about payments, to recover data.
TODO: handle it...
"""
user = models.OneToOneField(
'member.TFJMUser',
on_delete=models.CASCADE,