From e8908b672d0e9c5615cc616e4f12cd0663932251 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 16 Dec 2024 10:11:13 +0100 Subject: [PATCH] fix set naming --- .../0098_alter_drawlog_tournament.py | 19 +++++ tournaments/models/group_stage.py | 2 +- tournaments/models/match.py | 10 +-- tournaments/models/round.py | 2 +- tournaments/models/tournament.py | 75 +++++++++---------- tournaments/views.py | 6 +- 6 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 tournaments/migrations/0098_alter_drawlog_tournament.py diff --git a/tournaments/migrations/0098_alter_drawlog_tournament.py b/tournaments/migrations/0098_alter_drawlog_tournament.py new file mode 100644 index 0000000..055736d --- /dev/null +++ b/tournaments/migrations/0098_alter_drawlog_tournament.py @@ -0,0 +1,19 @@ +# Generated by Django 5.1 on 2024-12-16 09:02 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tournaments', '0097_special_store_id'), + ] + + operations = [ + migrations.AlterField( + model_name='drawlog', + name='tournament', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='draw_logs', to='tournaments.tournament'), + ), + ] diff --git a/tournaments/models/group_stage.py b/tournaments/models/group_stage.py index 990b608..d83be58 100644 --- a/tournaments/models/group_stage.py +++ b/tournaments/models/group_stage.py @@ -149,7 +149,7 @@ class GroupStage(SideStoreModel): return False def is_completed(self): - return not self.match_set.filter(end_date__isnull=True).exists() + return not self.matches.filter(end_date__isnull=True).exists() class LiveGroupStage: def __init__(self, title, step, index): diff --git a/tournaments/models/match.py b/tournaments/models/match.py index eceb32f..18ea87c 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -91,8 +91,8 @@ class Match(SideStoreModel): previous_index = self.round.index + 1 # Check if the next index is within the bounds of the rounds array - if previous_index < len(self.round.tournament.round_set.all()): - return self.round.tournament.round_set.filter(index=previous_index, parent = None).first() + if previous_index < len(self.round.tournament.rounds.all()): + return self.round.tournament.rounds.filter(index=previous_index, parent = None).first() # Return None or an appropriate value if the index is out of bounds return None @@ -102,7 +102,7 @@ class Match(SideStoreModel): #print(previous_round) match = None if previous_round: - matches = previous_round.match_set.all() # Retrieve the QuerySet + matches = previous_round.matches.all() # Retrieve the QuerySet match_index = self.index * 2 + 1 if top == False: match_index += 1 @@ -118,7 +118,7 @@ class Match(SideStoreModel): def index_in_round(self): if self.round is not None: matches = sorted( - self.round.match_set.filter(disabled=False), + self.round.matches.filter(disabled=False), key=lambda match: match.index ) try: @@ -154,7 +154,7 @@ class Match(SideStoreModel): previous_top_match = self.precedent_match(True) previous_bottom_match = self.precedent_match(False) if len(team_scores) == 0: - if (self.round and len(self.round.tournament.round_set.all()) == self.round.index -1): + if (self.round and len(self.round.tournament.rounds.all()) == self.round.index -1): return teams if (self.group_stage): return teams diff --git a/tournaments/models/round.py b/tournaments/models/round.py index c19b1a5..d3fa5ee 100644 --- a/tournaments/models/round.py +++ b/tournaments/models/round.py @@ -90,7 +90,7 @@ class Round(SideStoreModel): return self.parent.root_round() def all_matches_are_over(self): - for match in self.match_set.all(): + for match in self.matches.all(): if match.end_date is None and match.disabled is False: return False diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 1d95703..f724175 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -66,13 +66,6 @@ class Tournament(BaseModel): initial_seed_round = models.IntegerField(default=0) initial_seed_count = models.IntegerField(default=0) - def get_child_models(self): - return { - 'Round': 'round_set', - 'GroupStage': 'groupstage_set', - 'TeamRegistration': 'teamregistration_set' - } - def __str__(self): if self.name: return self.name @@ -250,7 +243,7 @@ class Tournament(BaseModel): summons.append(summon) else: print('>>> team_summons') - for team_registration in self.teamregistration_set.all(): + for team_registration in self.team_registrations.all(): if team_registration.is_valid_for_summon(): next_match = team_registration.next_match() if next_match and next_match.start_date is not None: @@ -264,7 +257,7 @@ class Tournament(BaseModel): return summons def has_summons(self): - for team_registration in self.teamregistration_set.all(): + for team_registration in self.team_registrations.all(): if team_registration.is_valid_for_summon(): next_match = team_registration.next_match() if next_match and next_match.start_date is not None: @@ -273,7 +266,7 @@ class Tournament(BaseModel): def rankings(self): rankings = [] - for team_registration in self.teamregistration_set.all(): + for team_registration in self.team_registrations.all(): if team_registration.walk_out is False and team_registration.final_ranking is not None: names = team_registration.team_names() ranking = team_registration.final_ranking @@ -296,7 +289,7 @@ class Tournament(BaseModel): closed_registration_date = self.closed_registration_date # print(f"Closed registration date: {closed_registration_date}") - for team_registration in self.teamregistration_set.all(): + for team_registration in self.team_registrations.all(): # print(f"Processing team registration: {team_registration}") is_valid = False if closed_registration_date is not None and team_registration.registration_date is not None and team_registration.registration_date <= closed_registration_date: @@ -413,10 +406,10 @@ class Tournament(BaseModel): match_groups = [] if group_stage_id: - group_stage = self.groupstage_set.filter(id=group_stage_id).first() + group_stage = self.group_stages.filter(id=group_stage_id).first() match_groups.append(self.group_stage_match_group(group_stage, broadcasted, hide_empty_matches=False)) elif round_id: - round = self.round_set.filter(id=round_id).first() + round = self.rounds.filter(id=round_id).first() if round and display_brackets is True: match_groups = self.round_match_groups(round, broadcasted, hide_empty_matches=False) else: @@ -428,11 +421,11 @@ class Tournament(BaseModel): groups = [] if self.display_matches(): - for round in self.round_set.filter(parent=None, group_stage_loser_bracket=False).all().order_by('index'): + for round in self.rounds.filter(parent=None, group_stage_loser_bracket=False).all().order_by('index'): groups.extend(self.round_match_groups(round, broadcasted, hide_empty_matches=True)) if self.display_group_stages(): - for round in self.round_set.filter(parent=None, group_stage_loser_bracket=True).all().order_by('index'): + for round in self.group_stages.all().order_by('index'): groups.extend(self.round_match_groups(round, broadcasted, hide_empty_matches=True)) ordered = sorted(self.get_computed_group_stage(), key=lambda s: (-s.step, s.index)) @@ -444,7 +437,7 @@ class Tournament(BaseModel): return groups def group_stage_match_group(self, group_stage, broadcasted, hide_empty_matches): - matches = group_stage.match_set.all() + matches = group_stage.matches.all() if hide_empty_matches: matches = [m for m in matches if m.should_appear()] else: @@ -460,7 +453,7 @@ class Tournament(BaseModel): def round_match_groups(self, round, broadcasted, hide_empty_matches): groups = [] - matches = round.match_set.order_by('index').all() + matches = round.matches.order_by('index').all() if hide_empty_matches: matches = [m for m in matches if m.should_appear()] else: @@ -515,7 +508,7 @@ class Tournament(BaseModel): def get_computed_group_stage(self): # Get all group stages and sort by step (descending) and index (ascending) - group_stages = self.groupstage_set.all().order_by('-step', 'index') + group_stages = self.group_stages.all().order_by('-step', 'index') # List to collect live group stages from finished steps filtered = [] @@ -523,7 +516,7 @@ class Tournament(BaseModel): for group_stage in group_stages: if group_stage.step > 0: # Check the previous step's group stages - previous_step_group_stages = self.groupstage_set.filter(step=group_stage.step - 1) + previous_step_group_stages = self.group_stages.filter(step=group_stage.step - 1) # Check if all previous step group stages are completed if all(gs.is_completed() for gs in previous_step_group_stages): @@ -535,7 +528,7 @@ class Tournament(BaseModel): return filtered def get_previous_live_group_stages(self, step): - previous_step_group_stages = self.groupstage_set.filter(step=step).order_by('index') + previous_step_group_stages = self.group_stages.filter(step=step).order_by('index') return [gs.live_group_stages() for gs in previous_step_group_stages] def last_group_stage_step(self): @@ -605,7 +598,7 @@ class Tournament(BaseModel): matches = [] group_stages = [] - if len(self.groupstage_set.all()) > 0 and self.no_bracket_match_has_started(): + if len(self.group_stages.all()) > 0 and self.no_bracket_match_has_started(): group_stages = [gs.live_group_stages() for gs in self.last_group_stage_step()] matches = self.broadcasted_group_stages_matches() first_round = self.first_round() @@ -642,18 +635,18 @@ class Tournament(BaseModel): def no_bracket_match_has_started(self): matches = [] - for round in self.round_set.all(): - for match in round.match_set.all(): + for round in self.rounds.all(): + for match in round.matches.all(): if match.started(): return False return True def all_matches(self, hide_empty_matches): matches = [] - for round in self.round_set.all(): + for round in self.rounds.all(): matches.extend(round.all_matches(hide_empty_matches)) - for group_stage in self.groupstage_set.all(): - matches.extend(group_stage.match_set.all()) + for group_stage in self.group_stages.all(): + matches.extend(group_stage.matches.all()) matches = [m for m in matches if m.should_appear()] @@ -661,12 +654,12 @@ class Tournament(BaseModel): def group_stage_matches(self): matches = [] - for group_stage in self.groupstage_set.all(): - matches.extend(group_stage.match_set.all()) + for group_stage in self.group_stages.all(): + matches.extend(group_stage.matches.all()) return matches def group_stages_running(self): - if len(self.groupstage_set.all()) > 0: + if len(self.group_stages.all()) > 0: # check le debut des match de Round matches = self.group_stage_matches() running_group_stage_matches = [m for m in matches if m.end_date is None] @@ -701,7 +694,7 @@ class Tournament(BaseModel): if round_root_index == 0: return last_finished_match.round else: - round = self.round_set.filter(parent=None,index=round_root_index-1).first() + round = self.rounds.filter(parent=None,index=round_root_index-1).first() if round: return round else: @@ -718,10 +711,10 @@ class Tournament(BaseModel): return matches[0] if matches else None def round_for_index(self, index): - return self.round_set.filter(index=index, parent=None).first() + return self.rounds.filter(index=index, parent=None).first() def first_round(self): - main_rounds = list(self.round_set.filter(parent=None)) + main_rounds = list(self.rounds.filter(parent=None)) main_rounds.sort(key=lambda r: r.index, reverse=True) return main_rounds[0] if main_rounds else None @@ -730,11 +723,11 @@ class Tournament(BaseModel): group_stages = self.elected_broadcast_group_stages() group_stages.sort(key=lambda gs: (gs.index, gs.start_date is None, gs.start_date)) for group_stage in group_stages: - matches.extend(group_stage.match_set.all()) + matches.extend(group_stage.matches.all()) matches = [m for m in matches if m.should_appear()] matches.sort(key=lambda m: (m.start_date is None, m.end_date is not None, m.start_date, m.index)) - group_stage_loser_bracket = list(self.round_set.filter(parent=None, group_stage_loser_bracket=True).all()) + group_stage_loser_bracket = list(self.rounds.filter(parent=None, group_stage_loser_bracket=True).all()) if len(group_stage_loser_bracket) > 0: loser_matches = group_stage_loser_bracket[0].all_matches(True) loser_matches = [m for m in loser_matches if m.should_appear()] @@ -744,7 +737,7 @@ class Tournament(BaseModel): return matches def elected_broadcast_group_stages(self): - group_stages = list(self.last_group_stage_step()) + group_stages = list(self.group_stages()) started = [gs for gs in group_stages if gs.starts_soon()] if len(started) > 0: return started @@ -779,7 +772,7 @@ class Tournament(BaseModel): return False def has_team_registrations(self): - return len(self.teamregistration_set.all()) > 0 + return len(self.team_registrations.all()) > 0 def display_summons(self): if self.end_date is not None: @@ -793,7 +786,7 @@ class Tournament(BaseModel): def display_group_stages(self): if self.end_date is not None: return True - if len(self.groupstage_set.all()) == 0: + if len(self.group_stages.all()) == 0: return False if self.publish_group_stages: return True @@ -805,7 +798,7 @@ class Tournament(BaseModel): return timezone.now() >= first_group_stage_start_date def group_stage_start_date(self): - group_stages = [gs for gs in self.groupstage_set.all() if gs.start_date is not None] + group_stages = [gs for gs in self.group_stages.all() if gs.start_date is not None] if len(group_stages) == 0: return None @@ -842,7 +835,7 @@ class Tournament(BaseModel): def bracket_matches(self): matches = [] - for round in self.round_set.all(): + for round in self.rounds.all(): matches.extend(round.all_matches(False)) return matches @@ -882,7 +875,7 @@ class Tournament(BaseModel): return self.hide_teams_weight def is_build_and_not_empty(self): - return (len(self.groupstage_set.all()) > 0 or len(self.round_set.all()) > 0) and len(self.teamregistration_set.all()) >= 4 + return (len(self.group_stages.all()) > 0 or len(self.rounds.all()) > 0) and len(self.team_registrations.all()) >= 4 def day_duration_formatted(self): return plural_format("jour", self.day_duration) @@ -894,7 +887,7 @@ class Tournament(BaseModel): return False def has_all_group_stages_started(self): - for group_stage in self.groupstage_set.all(): + for group_stage in self.group_stages.all(): if group_stage.has_at_least_one_started_match() is False: return False return True diff --git a/tournaments/views.py b/tournaments/views.py index be0d89f..44ce649 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -497,9 +497,9 @@ def tournament_import_view(request): tournament = Tournament.objects.get(id=tournament_id) # Delete existing relationships - tournament.round_set.all().delete() - tournament.groupstage_set.all().delete() - tournament.teamregistration_set.all().delete() + tournament.rounds.all().delete() + tournament.group_stages.all().delete() + tournament.team_registrations.all().delete() with zipfile.ZipFile(zip_file) as z: # First, process rounds