From f5ec9d105490bfdba4c65cc0cde9298ca8e08435 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 28 Dec 2020 17:49:59 +0100 Subject: [PATCH] Add some properties to the models --- apps/participation/models.py | 75 +++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/apps/participation/models.py b/apps/participation/models.py index ef66e45..9d1583b 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -171,6 +171,24 @@ class Tournament(models.Model): if qs.exists(): return qs.get() + @property + def participations(self): + if self.final: + return Participation.objects.filter(final=True) + return self.participation_set + + @property + def solutions(self): + if self.final: + return Solution.objects.filter(final_solution=True) + return Solution.objects.filter(participation__tournament=self) + + @property + def syntheses(self): + if self.final: + return Synthesis.objects.filter(final_solution=True) + return Synthesis.objects.filter(participation__tournament=self) + class Meta: verbose_name = _("tournament") verbose_name_plural = _("tournaments") @@ -179,28 +197,6 @@ class Tournament(models.Model): ] -class Pool(models.Model): - tournament = models.ForeignKey( - Tournament, - on_delete=models.CASCADE, - verbose_name=_("tournament"), - ) - - round = models.PositiveSmallIntegerField( - verbose_name=_("round"), - ) - - juries = models.ManyToManyField( - VolunteerRegistration, - verbose_name=_("juries"), - related_name="jury_in", - ) - - class Meta: - verbose_name = _("pool") - verbose_name_plural = _("pools") - - class Participation(models.Model): """ The Participation model contains all data that are related to the participation: @@ -266,6 +262,40 @@ class Participation(models.Model): verbose_name_plural = _("participations") +class Pool(models.Model): + tournament = models.ForeignKey( + Tournament, + on_delete=models.CASCADE, + related_name="pools", + verbose_name=_("tournament"), + ) + + round = models.PositiveSmallIntegerField( + verbose_name=_("round"), + ) + + participations = models.ManyToManyField( + Participation, + related_name="pools", + verbose_name=_("participations"), + ) + + juries = models.ManyToManyField( + VolunteerRegistration, + related_name="jury_in", + verbose_name=_("juries"), + ) + + @property + def solutions(self): + return Solution.objects.filter(participation__in=self.participations, final_solution=self.tournament.final) + + class Meta: + verbose_name = _("pool") + verbose_name_plural = _("pools") + + + class Solution(models.Model): participation = models.ForeignKey( Participation, @@ -307,6 +337,7 @@ class Synthesis(models.Model): pool = models.ForeignKey( Pool, on_delete=models.CASCADE, + related_name="syntheses", verbose_name=_("pool"), )