fix event deletion signal

bracket-feature
Raz 10 months ago
parent 13c545f391
commit afbb3a2272
  1. 7
      tournaments/models/tournament.py
  2. 22
      tournaments/signals.py

@ -75,7 +75,6 @@ class Tournament(models.Model):
minimum_player_per_team = models.IntegerField(default=2) minimum_player_per_team = models.IntegerField(default=2)
maximum_player_per_team = models.IntegerField(default=2) maximum_player_per_team = models.IntegerField(default=2)
information = models.CharField(max_length=4000, null=True, blank=True) information = models.CharField(max_length=4000, null=True, blank=True)
_being_deleted = False # Class attribute
def __str__(self): def __str__(self):
if self.name: if self.name:
@ -1186,12 +1185,6 @@ class Tournament(models.Model):
return waiting_teams[0].team_registration return waiting_teams[0].team_registration
return None return None
def delete(self, *args, **kwargs):
# Mark this tournament as being deleted
self._being_deleted = True
super().delete(*args, **kwargs)
def broadcasted_prog(self): def broadcasted_prog(self):
# Get matches from broadcasted_matches_and_group_stages # Get matches from broadcasted_matches_and_group_stages
matches, _ = self.broadcasted_matches_and_group_stages() matches, _ = self.broadcasted_matches_and_group_stages()

@ -1,6 +1,6 @@
import random import random
import string import string
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete, post_delete
from django.dispatch import receiver from django.dispatch import receiver
from django.conf import settings from django.conf import settings
from tournaments.models.tournament import Tournament from tournaments.models.tournament import Tournament
@ -12,6 +12,8 @@ import requests
from tournaments.services.email_service import TournamentEmailService from tournaments.services.email_service import TournamentEmailService
from tournaments.models import PlayerDataSource from tournaments.models import PlayerDataSource
tournament_deletion_in_progress = set()
def generate_unique_code(): def generate_unique_code():
characters = string.ascii_lowercase + string.digits characters = string.ascii_lowercase + string.digits
while True: while True:
@ -59,13 +61,13 @@ def send_discord_message(webhook_url, content):
f'Error sending message to Discord webhook: {response.status_code}, {response.text}' f'Error sending message to Discord webhook: {response.status_code}, {response.text}'
) )
@receiver(pre_delete, sender=TeamRegistration) @receiver(post_delete, sender=TeamRegistration)
def unregister_team(sender, instance, **kwargs): def unregister_team(sender, instance, **kwargs):
team_registration = instance team_registration = instance
tournament = instance.tournament tournament = instance.tournament
global tournament_deletion_in_progress
# Skip creating unregistration records if tournament is being deleted print('pre_delete TeamRegistration', tournament_deletion_in_progress)
if not tournament or tournament._being_deleted == True: if tournament.id in tournament_deletion_in_progress:
return return
first_waiting_list_team = tournament.first_waiting_list_team() first_waiting_list_team = tournament.first_waiting_list_team()
@ -117,10 +119,18 @@ def unregister_team(sender, instance, **kwargs):
other_player other_player
) )
@receiver(post_delete, sender=Tournament)
def tournament_deletion(sender, instance, **kwargs):
global tournament_deletion_in_progress
tournament_deletion_in_progress.discard(instance.id)
print('post tournament_deletion_in_progress', tournament_deletion_in_progress)
@receiver(pre_delete, sender=Tournament) @receiver(pre_delete, sender=Tournament)
def notify_players_of_tournament_cancellation(sender, instance, **kwargs): def notify_players_of_tournament_cancellation(sender, instance, **kwargs):
tournament = instance tournament = instance
global tournament_deletion_in_progress
tournament_deletion_in_progress.add(instance.id)
print('pre tournament_deletion_in_progress', tournament_deletion_in_progress)
# Get all team registrations # Get all team registrations
team_registrations = tournament.teamregistration_set.all() team_registrations = tournament.teamregistration_set.all()

Loading…
Cancel
Save