diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 33cf642..f8e12fe 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -307,12 +307,22 @@ class Match(TournamentSubModel): return teams def local_start_date(self): - timezone = self.get_tournament().timezone() - return self.start_date.astimezone(timezone) + tournament = self.get_tournament() + if tournament is None: + return self.start_date + if self.start_date is not None: + timezone = tournament.timezone() + return self.start_date.astimezone(timezone) + return None def local_planned_start_date(self): - timezone = self.get_tournament().timezone() - return self.planned_start_date.astimezone(timezone) + tournament = self.get_tournament() + if tournament is None: + return self.start_date + if self.planned_start_date is not None: + timezone = tournament.timezone() + return self.planned_start_date.astimezone(timezone) + return None def formatted_start_date(self): if self.start_date: @@ -335,10 +345,14 @@ class Match(TournamentSubModel): return 'À suivre' else: # timezoned_datetime = timezone.localtime(self.start_date) - timezone = self.get_tournament().timezone() - local_start = self.start_date.astimezone(timezone) + tournament = self.get_tournament() + day_duration = 3 + if tournament: + day_duration = tournament.day_duration + + local_start = self.local_start_date() time_format ='l H:i' - if self.get_tournament().day_duration >= 7: + if day_duration >= 7: time_format = 'D. d F à H:i' if self.confirmed: return formats.date_format(local_start, format=time_format) diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 2ba071c..e831d46 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -1953,21 +1953,26 @@ class Tournament(BaseModel): - days: List of unique days found (datetime.date objects) - match_groups: Dictionary of match groups by date and hour or just for the selected day """ + event = self.event + tournaments_count = 1 + if event: + tournaments_count = event.tournaments.count() - if event_mode is True and self.event.tournaments.count() == 1: + if event_mode is True and tournaments_count == 1: event_mode = False - if self.event.tournaments.count() == 1: + show_teams_in_prog = False + if tournaments_count == 1: show_teams_in_prog = self.show_teams_in_prog - else: - show_teams_in_prog = self.event.tournaments.filter(show_teams_in_prog=True).first() is not None + elif event: + show_teams_in_prog = event.tournaments.filter(show_teams_in_prog=True).first() is not None # Get all matches from rounds and group stages - use a set to avoid duplicates all_matches = set() tournaments = [self] - if event_mode is True: - tournaments = self.event.tournaments.all() + if event_mode is True and event: + tournaments = event.tournaments.all() # Check if all tournaments have started - if so, always show teams all_started = True @@ -2016,6 +2021,44 @@ class Tournament(BaseModel): sorted_days = sorted(list(days)) # Create match groups for the selected day match_groups = [] + hide_teams = show_teams_in_prog == False + # When broadcast=True, handle all days with matches + if broadcast: + today = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0).date() + sorted_days = [day for day in sorted(list(days)) if day >= today] + + # Process all days with matches + for selected_day in sorted_days: + + # Group matches by hour + matches_by_hour = {} + for match in matches_by_day[selected_day]: + local_time = timezone.localtime(match.planned_start_date) + hour_key = local_time.strftime('%H:%M') + + if hour_key not in matches_by_hour: + matches_by_hour[hour_key] = [] + + matches_by_hour[hour_key].append(match) + + # Create match groups for each hour + for hour, matches in sorted(matches_by_hour.items()): + # Sort matches by court if available + matches.sort(key=lambda m: (m.court_index if m.court_index is not None else 999)) + + local_date = matches[0].local_planned_start_date() + formatted_name = formats.date_format(local_date, format='l j F à H:i').capitalize() + mg = self.create_match_group( + name=formatted_name, + matches=matches, + round_id=None, + round_index=None, + hide_teams=hide_teams, + event_mode=event_mode, + broadcast=broadcast + ) + match_groups.append(mg) + return sorted_days, match_groups if all or day is None: today = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0).date() @@ -2039,7 +2082,6 @@ class Tournament(BaseModel): matches_by_hour[hour_key].append(match) - hide_teams = show_teams_in_prog == False # Create match groups for each hour for hour, matches in sorted(matches_by_hour.items()): # Sort matches by court if available @@ -2084,7 +2126,6 @@ class Tournament(BaseModel): matches_by_hour[hour_key].append(match) - hide_teams = show_teams_in_prog == False # Create match groups for each hour for hour, matches in sorted(matches_by_hour.items()): # Sort matches by court if available