optimize code using count() in place of len() and displays unfinished tournament that should be over

bracket-feature
Raz 9 months ago
parent 193ca2c043
commit 05f0465584
  1. 2
      tournaments/models/club.py
  2. 2
      tournaments/models/custom_user.py
  3. 6
      tournaments/models/match.py
  4. 2
      tournaments/models/team_registration.py
  5. 22
      tournaments/models/tournament.py
  6. 4
      tournaments/views.py

@ -28,7 +28,7 @@ class Club(models.Model):
return self.name
def events_count(self):
return len(self.event_set.all())
return self.event_set.count()
def court_name(self, index):
for court in self.court_set.all():

@ -55,7 +55,7 @@ class CustomUser(AbstractUser):
return f"{self.username} : {self.first_name} {self.last_name} | {self.email} | {self.phone}"
def event_count(self):
return len(self.event_set.all())
return self.event_set.count()
def full_name(self):
return f"{self.first_name} {self.last_name}"

@ -85,7 +85,7 @@ class Match(models.Model):
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()):
if previous_index < self.round.tournament.round_set.count():
return self.round.tournament.round_set.filter(index=previous_index, parent = None).first()
# Return None or an appropriate value if the index is out of bounds
@ -148,7 +148,7 @@ class Match(models.Model):
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 self.round.tournament.round_set.count() == self.round.index -1):
return teams
if (self.group_stage):
return teams
@ -280,7 +280,7 @@ class Match(models.Model):
def should_appear(self):
if self.disabled is True:
return False
return len(self.team_scores.all()) > 0
return self.team_scores.count() > 0
# elif self.group_stage is None:
# if len(self.team_scores.all()) == 2:

@ -109,7 +109,7 @@ class TeamRegistration(models.Model):
self.save() # Save the updated weight if necessary
def is_valid_for_summon(self):
return len(self.playerregistration_set.all()) > 0 or self.name is not None
return self.playerregistration_set.count() > 0 or self.name is not None
def initial_weight(self):
if self.locked_weight is None:

@ -215,7 +215,7 @@ class Tournament(models.Model):
return "Annulé"
teams = self.teams(True)
if self.supposedly_in_progress() or self.end_date is not None:
if self.supposedly_in_progress() or self.end_date is not None or self.should_be_over():
teams = [t for t in teams if t.stage != "Attente"]
if teams is not None and len(teams) > 0:
word = "équipe"
@ -614,7 +614,7 @@ class Tournament(models.Model):
matches = []
group_stages = []
if len(self.groupstage_set.all()) > 0 and self.no_bracket_match_has_started():
if self.groupstage_set.count() > 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()
@ -798,7 +798,7 @@ class Tournament(models.Model):
return False
def has_team_registrations(self):
return len(self.teamregistration_set.all()) > 0
return self.teamregistration_set.count() > 0
def display_summons(self):
if self.end_date is not None:
@ -893,6 +893,14 @@ class Tournament(models.Model):
return start <= now <= end
def should_be_over(self):
if self.end_date is not None:
return True
timezoned_datetime = timezone.localtime(self.start_date)
end = timezoned_datetime + timedelta(days=self.day_duration + 1)
now = timezone.now()
return now >= end and self.is_build_and_not_empty()
def display_points_earned(self):
return self.federal_level_category != FederalLevelCategory.UNLISTED and self.hide_points_earned is False
@ -901,7 +909,7 @@ class Tournament(models.Model):
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 self.groupstage_set.count() > 0 or self.round_set.count() > 0 and self.teamregistration_set.count() >= 4
def day_duration_formatted(self):
return plural_format("jour", self.day_duration)
@ -983,7 +991,7 @@ class Tournament(models.Model):
# Check target team count and waiting list limit
if self.team_count is not None:
current_team_count = len([tr for tr in self.teamregistration_set.all() if tr.out_of_tournament() is False])
current_team_count = self.teamregistration_set.exclude(walk_out=True).count()
if current_team_count >= self.team_count:
if self.waiting_list_limit is not None:
waiting_list_count = current_team_count - self.team_count
@ -1013,7 +1021,7 @@ class Tournament(models.Model):
if self.team_count is not None:
# Get all team registrations excluding walk_outs
current_team_count = len([tr for tr in self.teamregistration_set.all() if tr.out_of_tournament() is False])
current_team_count = self.teamregistration_set.exclude(walk_out=True).count()
if current_team_count >= self.team_count:
if self.waiting_list_limit is not None:
waiting_list_count = current_team_count - self.team_count
@ -1048,7 +1056,7 @@ class Tournament(models.Model):
return -1
# Get count of active teams (not walked out)
current_team_count = len([tr for tr in self.teamregistration_set.all() if tr.out_of_tournament() is False])
current_team_count = self.teamregistration_set.exclude(walk_out=True).count()
# If current count is less than target count, next team is not in waiting list
if current_team_count < self.team_count:

@ -141,8 +141,8 @@ def tournaments_query(query, club_id, ascending):
return Tournament.objects.filter(*queries).order_by(sortkey)
def finished_tournaments(club_id):
ended_tournaments = tournaments_query(Q(end_date__isnull=False), club_id, False)
return [t for t in ended_tournaments if t.display_tournament()]
ended_tournaments = tournaments_query(Q(is_private=False, is_deleted=False, event__club__isnull=False), club_id, False)
return [t for t in ended_tournaments if t.display_tournament() and t.should_be_over()]
def live_tournaments(club_id):
tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True)

Loading…
Cancel
Save