mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-06-25 00:20:31 +02:00
Add export button
Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
# Copyright (C) 2023 by Animath
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from asgiref.sync import sync_to_async
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.utils.text import format_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from participation.models import Participation, Tournament
|
||||
from participation.models import Passage, Participation, Pool as PPool, Tournament
|
||||
|
||||
|
||||
class Draw(models.Model):
|
||||
@ -31,6 +32,10 @@ class Draw(models.Model):
|
||||
verbose_name=_("last message"),
|
||||
)
|
||||
|
||||
@property
|
||||
def exportable(self):
|
||||
return any(pool.exportable for r in self.round_set.all() for pool in r.pool_set.all())
|
||||
|
||||
def get_state(self):
|
||||
if self.current_round.current_pool is None:
|
||||
return 'DICE_SELECT_POULES'
|
||||
@ -173,6 +178,15 @@ class Pool(models.Model):
|
||||
verbose_name=_('current team'),
|
||||
)
|
||||
|
||||
associated_pool = models.OneToOneField(
|
||||
'participation.Pool',
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
default=None,
|
||||
related_name='draw_pool',
|
||||
verbose_name=_("associated pool"),
|
||||
)
|
||||
|
||||
@property
|
||||
def team_draws(self):
|
||||
return self.teamdraw_set.order_by('passage_index').all()
|
||||
@ -194,6 +208,53 @@ class Pool(models.Model):
|
||||
td = await self.teamdraw_set.aget(choose_index=current_index)
|
||||
return td
|
||||
|
||||
@property
|
||||
def exportable(self):
|
||||
return self.associated_pool is None and all(td.accepted is not None for td in self.teamdraw_set.all())
|
||||
|
||||
def export(self):
|
||||
from django.db import transaction
|
||||
with transaction.atomic():
|
||||
self.associated_pool = PPool.objects.create(
|
||||
tournament=self.round.draw.tournament,
|
||||
round=self.round.number,
|
||||
)
|
||||
self.associated_pool.juries.set(self.round.draw.tournament.organizers.all())
|
||||
tds = list(self.team_draws)
|
||||
self.associated_pool.participations.set([td.participation for td in tds])
|
||||
|
||||
if len(tds) == 3:
|
||||
table = [
|
||||
[0, 1, 2],
|
||||
[1, 2, 0],
|
||||
[2, 0, 1],
|
||||
]
|
||||
elif len(tds) == 4:
|
||||
table = [
|
||||
[0, 1, 2],
|
||||
[1, 2, 3],
|
||||
[2, 3, 0],
|
||||
[3, 0, 1],
|
||||
]
|
||||
elif len(tds) == 5:
|
||||
table = [
|
||||
[0, 2, 3],
|
||||
[1, 3, 4],
|
||||
[2, 0, 1],
|
||||
[3, 4, 0],
|
||||
[4, 1, 2],
|
||||
]
|
||||
|
||||
for line in table:
|
||||
Passage.objects.create(
|
||||
pool=self.associated_pool,
|
||||
solution_number=tds[line[0]].accepted,
|
||||
defender=tds[line[0]].participation,
|
||||
opponent=tds[line[1]].participation,
|
||||
reporter=tds[line[2]].participation,
|
||||
defender_penalties=tds[line[0]].penalty_int,
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.get_letter_display()}{self.round.number}"
|
||||
|
||||
@ -267,9 +328,13 @@ class TeamDraw(models.Model):
|
||||
verbose_name=_('rejected problems'),
|
||||
)
|
||||
|
||||
@property
|
||||
def penalty_int(self):
|
||||
return max(0, len(self.rejected) - (settings.PROBLEM_COUNT - 5))
|
||||
|
||||
@property
|
||||
def penalty(self):
|
||||
return max(0, 0.5 * (len(self.rejected) - (settings.PROBLEM_COUNT - 5)))
|
||||
return 0.5 * self.penalty_int
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('team draw')
|
||||
|
Reference in New Issue
Block a user