1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-24 21:40:34 +02:00

Reduce the usage of sync_to_async

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2023-04-04 15:10:28 +02:00
parent 4357d51b9a
commit 82cda0b279
3 changed files with 122 additions and 91 deletions

View File

@ -37,10 +37,36 @@ class Draw(models.Model):
return reverse_lazy('draw:index') + f'#{slugify(self.tournament.name)}'
@property
def exportable(self):
def exportable(self) -> bool:
"""
True if any pool of the draw is exportable, ie. can be exported to the tournament interface.
This operation is synchronous.
"""
return any(pool.exportable for r in self.round_set.all() for pool in r.pool_set.all())
def get_state(self):
async def is_exportable(self) -> bool:
"""
True if any pool of the draw is exportable, ie. can be exported to the tournament interface.
This operation is asynchronous.
"""
return any([await pool.is_exportable() async for r in self.round_set.all() async for pool in r.pool_set.all()])
def get_state(self) -> str:
"""
The current state of the draw.
Can be:
* **DICE_SELECT_POULES** if we are waiting for teams to launch their dice to determine pools and passage order ;
* **DICE_ORDER_POULE** if we are waiting for teams to launch their dice to determine the problem draw order ;
* **WAITING_DRAW_PROBLEM** if we are waiting for a team to draw a problem ;
* **WAITING_CHOOSE_PROBLEM** if we are waiting for a team to accept or reject a problem ;
* **WAITING_FINAL** if this is the final tournament and we are between the two rounds ;
* **DRAW_ENDED** if the draw is ended.
Warning: the current round and the current team must be prefetched in an async context.
"""
if self.current_round.current_pool is None:
return 'DICE_SELECT_POULES'
elif self.current_round.current_pool.current_team is None:
@ -151,7 +177,7 @@ class Round(models.Model):
return self.teamdraw_set.order_by('pool__letter', 'passage_index').all()
async def next_pool(self):
pool = await sync_to_async(lambda: self.current_pool)()
pool = self.current_pool
return await self.pool_set.aget(letter=pool.letter + 1)
def __str__(self):
@ -207,70 +233,75 @@ class Pool(models.Model):
@property
def trigrams(self):
return [td.participation.team.trigram for td in self.teamdraw_set.order_by('passage_index').all()]
return [td.participation.team.trigram for td in self.teamdraw_set.order_by('passage_index')\
.prefetch_related('participation__team').all()]
async def atrigrams(self):
return await sync_to_async(lambda: self.trigrams)()
return [td.participation.team.trigram async for td in self.teamdraw_set.order_by('passage_index')\
.prefetch_related('participation__team').all()]
async def next_td(self):
td = await sync_to_async(lambda: self.current_team)()
td = self.current_team
current_index = (td.choose_index + 1) % self.size
td = await self.teamdraw_set.aget(choose_index=current_index)
td = await self.teamdraw_set.prefetch_related('participation__team').aget(choose_index=current_index)
while td.accepted:
current_index += 1
current_index %= self.size
td = await self.teamdraw_set.aget(choose_index=current_index)
td = await self.teamdraw_set.prefetch_related('participation__team').aget(choose_index=current_index)
return td
@property
def exportable(self):
return self.associated_pool is None and self.teamdraw_set.exists() \
return self.associated_pool_id is None and self.teamdraw_set.exists() \
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,
letter=self.letter,
async def is_exportable(self):
return self.associated_pool_id is None and await self.teamdraw_set.aexists() \
and all([td.accepted is not None async for td in self.teamdraw_set.all()])
async def export(self):
self.associated_pool = await PPool.objects.acreate(
tournament=self.round.draw.tournament,
round=self.round.number,
letter=self.letter,
)
await self.associated_pool.juries.aset(self.round.draw.tournament.organizers.all())
tds = [td async for td in self.team_draws.prefetch_related('participation')]
await self.associated_pool.participations.aset([td.participation async for td in self.team_draws\
.prefetch_related('participation')])
await self.asave()
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:
await Passage.objects.acreate(
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,
)
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])
self.save()
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 str(format_lazy(_("Pool {letter}{number}"), letter=self.get_letter_display(), number=self.round.number))