Use the new asave function of Django 4.2

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2023-04-04 11:20:43 +02:00
parent bb9f0dab22
commit 90bfc45858
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 31 additions and 31 deletions

View File

@ -120,7 +120,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
{'type': 'draw.send_poules', 'round': r}) {'type': 'draw.send_poules', 'round': r})
draw.current_round = r1 draw.current_round = r1
await sync_to_async(draw.save)() await draw.asave()
async for td in r1.teamdraw_set.prefetch_related('participation__team').all(): async for td in r1.teamdraw_set.prefetch_related('participation__team').all():
await self.channel_layer.group_send(f"team-{td.participation.team.trigram}", await self.channel_layer.group_send(f"team-{td.participation.team.trigram}",
@ -145,7 +145,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
@ensure_orga @ensure_orga
async def abort(self, **kwargs): async def abort(self, **kwargs):
await sync_to_async(self.tournament.draw.delete)() await self.tournament.draw.adelete()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", {'type': 'draw_abort'}) await self.channel_layer.group_send(f"tournament-{self.tournament.id}", {'type': 'draw_abort'})
async def draw_abort(self, content): async def draw_abort(self, content):
@ -201,7 +201,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
team_draw.passage_dice = res team_draw.passage_dice = res
else: else:
team_draw.choice_dice = res team_draw.choice_dice = res
await sync_to_async(team_draw.save)() await team_draw.asave()
await self.channel_layer.group_send( await self.channel_layer.group_send(
f"tournament-{self.tournament.id}", {'type': 'draw.dice', 'team': trigram, 'result': res}) f"tournament-{self.tournament.id}", {'type': 'draw.dice', 'team': trigram, 'result': res})
@ -223,7 +223,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
for dup in dups: for dup in dups:
dup.passage_dice = None dup.passage_dice = None
await sync_to_async(dup.save)() await dup.asave()
await self.channel_layer.group_send( await self.channel_layer.group_send(
f"tournament-{self.tournament.id}", f"tournament-{self.tournament.id}",
{'type': 'draw.dice', 'team': dup.participation.team.trigram, 'result': None}) {'type': 'draw.dice', 'team': dup.participation.team.trigram, 'result': None})
@ -247,7 +247,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
for i, td in enumerate(pool_tds): for i, td in enumerate(pool_tds):
td.pool = p td.pool = p
td.passage_index = i td.passage_index = i
await sync_to_async(td.save)() await td.asave()
tds_copy = tds.copy() tds_copy = tds.copy()
round2 = await self.tournament.draw.round_set.filter(number=2).aget() round2 = await self.tournament.draw.round_set.filter(number=2).aget()
@ -266,11 +266,11 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if current_pool_id == len(round2_pools): if current_pool_id == len(round2_pools):
current_pool_id = 0 current_pool_id = 0
current_passage_index += 1 current_passage_index += 1
await sync_to_async(td2.save)() await td2.asave()
pool = await Pool.objects.filter(round=self.tournament.draw.current_round, letter=1).aget() pool = await Pool.objects.filter(round=self.tournament.draw.current_round, letter=1).aget()
self.tournament.draw.current_round.current_pool = pool self.tournament.draw.current_round.current_pool = pool
await sync_to_async(self.tournament.draw.current_round.save)() await self.tournament.draw.current_round.asave()
msg = "Les résultats des dés sont les suivants : " msg = "Les résultats des dés sont les suivants : "
msg += await sync_to_async(lambda: ", ".join( msg += await sync_to_async(lambda: ", ".join(
@ -280,7 +280,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
msg += "Attention : les ordres de passage sont déterminés à partir des scores des dés, mais ne sont pas " msg += "Attention : les ordres de passage sont déterminés à partir des scores des dés, mais ne sont pas "
msg += "directement l'ordre croissant des dés, afin d'avoir des poules mélangées." msg += "directement l'ordre croissant des dés, afin d'avoir des poules mélangées."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
for td in tds: for td in tds:
await self.channel_layer.group_send( await self.channel_layer.group_send(
@ -327,7 +327,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
for dup in dups: for dup in dups:
dup.choice_dice = None dup.choice_dice = None
await sync_to_async(dup.save)() await dup.asave()
await self.channel_layer.group_send( await self.channel_layer.group_send(
f"tournament-{self.tournament.id}", f"tournament-{self.tournament.id}",
{'type': 'draw.dice', 'team': dup.participation.team.trigram, 'result': None}) {'type': 'draw.dice', 'team': dup.participation.team.trigram, 'result': None})
@ -345,13 +345,13 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
tds.sort(key=lambda x: -x.choice_dice) tds.sort(key=lambda x: -x.choice_dice)
for i, td in enumerate(tds): for i, td in enumerate(tds):
td.choose_index = i td.choose_index = i
await sync_to_async(td.save)() await td.asave()
pool.current_team = tds[0] pool.current_team = tds[0]
await sync_to_async(pool.save)() await pool.asave()
self.tournament.draw.last_message = "" self.tournament.draw.last_message = ""
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.set_info', 'draw': self.tournament.draw}) {'type': 'draw.set_info', 'draw': self.tournament.draw})
@ -392,7 +392,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
break break
td.purposed = problem td.purposed = problem
await sync_to_async(td.save)() await td.asave()
trigram = await sync_to_async(lambda: td.participation.team.trigram)() trigram = await sync_to_async(lambda: td.participation.team.trigram)()
await self.channel_layer.group_send(f"team-{trigram}", await self.channel_layer.group_send(f"team-{trigram}",
@ -407,7 +407,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
{'type': 'draw.draw_problem', 'team': trigram, 'problem': problem}) {'type': 'draw.draw_problem', 'team': trigram, 'problem': problem})
self.tournament.draw.last_message = "" self.tournament.draw.last_message = ""
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.set_info', 'draw': self.tournament.draw}) {'type': 'draw.set_info', 'draw': self.tournament.draw})
@ -428,7 +428,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
td.accepted = td.purposed td.accepted = td.purposed
td.purposed = None td.purposed = None
await sync_to_async(td.save)() await td.asave()
trigram = await sync_to_async(lambda: td.participation.team.trigram)() trigram = await sync_to_async(lambda: td.participation.team.trigram)()
msg = f"L'équipe <strong>{trigram}</strong> a accepté le problème <strong>{td.accepted}</strong>. " msg = f"L'équipe <strong>{trigram}</strong> a accepté le problème <strong>{td.accepted}</strong>. "
@ -437,7 +437,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
else: else:
msg += "Plus personne ne peut l'accepter." msg += "Plus personne ne peut l'accepter."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
await self.channel_layer.group_send(f"team-{trigram}", await self.channel_layer.group_send(f"team-{trigram}",
{'type': 'draw.buttons_visibility', 'visible': False}) {'type': 'draw.buttons_visibility', 'visible': False})
@ -453,7 +453,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
# Continue # Continue
next_td = await pool.next_td() next_td = await pool.next_td()
pool.current_team = next_td pool.current_team = next_td
await sync_to_async(pool.save)() await pool.asave()
new_trigram = await sync_to_async(lambda: next_td.participation.team.trigram)() new_trigram = await sync_to_async(lambda: next_td.participation.team.trigram)()
await self.channel_layer.group_send(f"team-{new_trigram}", await self.channel_layer.group_send(f"team-{new_trigram}",
@ -476,13 +476,13 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
tds[0].passage_index = p_index tds[0].passage_index = p_index
tds[1].passage_index = p_index + 1 tds[1].passage_index = p_index + 1
p_index += 2 p_index += 2
await sync_to_async(tds[0].save)() await tds[0].asave()
await sync_to_async(tds[1].save)() await tds[1].asave()
for pb, tds in problems.items(): for pb, tds in problems.items():
if len(tds) == 1: if len(tds) == 1:
tds[0].passage_index = p_index tds[0].passage_index = p_index
p_index += 1 p_index += 1
await sync_to_async(tds[0].save)() await tds[0].asave()
print(p_index) print(p_index)
@ -498,12 +498,12 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
msg += f"<br><br>Le tirage de la poule {pool.get_letter_display()}{r.number} est terminé. " \ msg += f"<br><br>Le tirage de la poule {pool.get_letter_display()}{r.number} est terminé. " \
f"Le tableau récapitulatif est en bas." f"Le tableau récapitulatif est en bas."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
if await r.teamdraw_set.filter(accepted__isnull=True).aexists(): if await r.teamdraw_set.filter(accepted__isnull=True).aexists():
# Next pool # Next pool
next_pool = await r.next_pool() next_pool = await r.next_pool()
r.current_pool = next_pool r.current_pool = next_pool
await sync_to_async(r.save)() await r.asave()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.dice_visibility', 'visible': True}) {'type': 'draw.dice_visibility', 'visible': True})
@ -515,7 +515,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
self.tournament.draw.current_round = r2 self.tournament.draw.current_round = r2
msg += "<br><br>Le tirage au sort du tour 1 est terminé." msg += "<br><br>Le tirage au sort du tour 1 est terminé."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
for participation in self.participations: for participation in self.participations:
await self.channel_layer.group_send( await self.channel_layer.group_send(
@ -531,7 +531,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
# Start the first pool of the second round # Start the first pool of the second round
p1: Pool = await r2.pool_set.filter(letter=1).aget() p1: Pool = await r2.pool_set.filter(letter=1).aget()
r2.current_pool = p1 r2.current_pool = p1
await sync_to_async(r2.save)() await r2.asave()
async for td in p1.teamdraw_set.prefetch_related('participation__team').all(): async for td in p1.teamdraw_set.prefetch_related('participation__team').all():
await self.channel_layer.group_send(f"team-{td.participation.team.trigram}", await self.channel_layer.group_send(f"team-{td.participation.team.trigram}",
@ -542,7 +542,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
# For the final tournament, we wait for a manual update between the two rounds. # For the final tournament, we wait for a manual update between the two rounds.
msg += "<br><br>Le tirage au sort du tour 1 est terminé." msg += "<br><br>Le tirage au sort du tour 1 est terminé."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
await self.channel_layer.group_send(f"volunteer-{self.tournament.id}", await self.channel_layer.group_send(f"volunteer-{self.tournament.id}",
{'type': 'draw.export_visibility', 'visible': True}) {'type': 'draw.export_visibility', 'visible': True})
@ -572,7 +572,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if not already_refused: if not already_refused:
td.rejected.append(problem) td.rejected.append(problem)
td.purposed = None td.purposed = None
await sync_to_async(td.save)() await td.asave()
remaining = settings.PROBLEM_COUNT - 5 - len(td.rejected) remaining = settings.PROBLEM_COUNT - 5 - len(td.rejected)
@ -586,7 +586,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
else: else:
msg += "Cela ajoute une pénalité de 0.5 sur le coefficient de l'oral de læ défenseur⋅se." msg += "Cela ajoute une pénalité de 0.5 sur le coefficient de l'oral de læ défenseur⋅se."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
await self.channel_layer.group_send(f"team-{trigram}", await self.channel_layer.group_send(f"team-{trigram}",
{'type': 'draw.buttons_visibility', 'visible': False}) {'type': 'draw.buttons_visibility', 'visible': False})
@ -602,7 +602,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
next_td = await pool.next_td() next_td = await pool.next_td()
pool.current_team = next_td pool.current_team = next_td
await sync_to_async(pool.save)() await pool.asave()
new_trigram = await sync_to_async(lambda: next_td.participation.team.trigram)() new_trigram = await sync_to_async(lambda: next_td.participation.team.trigram)()
await self.channel_layer.group_send(f"team-{new_trigram}", await self.channel_layer.group_send(f"team-{new_trigram}",
@ -635,11 +635,11 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
msg = "Le tirage au sort pour le tour 2 va commencer. " \ msg = "Le tirage au sort pour le tour 2 va commencer. " \
"L'ordre de passage est déterminé à partir du classement du premier tour." "L'ordre de passage est déterminé à partir du classement du premier tour."
self.tournament.draw.last_message = msg self.tournament.draw.last_message = msg
await sync_to_async(self.tournament.draw.save)() await self.tournament.draw.asave()
pool = await Pool.objects.filter(round=self.tournament.draw.current_round, letter=1).aget() pool = await Pool.objects.filter(round=self.tournament.draw.current_round, letter=1).aget()
r2.current_pool = pool r2.current_pool = pool
await sync_to_async(r2.save)() await r2.asave()
notes = dict() notes = dict()
async for participation in self.tournament.participations.filter(valid=True).prefetch_related('team').all(): async for participation in self.tournament.participations.filter(valid=True).prefetch_related('team').all():
@ -654,7 +654,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
td = await TeamDraw.objects.aget(round=r2, participation=participation) td = await TeamDraw.objects.aget(round=r2, participation=participation)
td.pool = pool td.pool = pool
td.passage_index = i td.passage_index = i
await sync_to_async(td.save)() await td.asave()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.send_poules', 'round': r2}) {'type': 'draw.send_poules', 'round': r2})