diff --git a/tournaments/models/group_stage.py b/tournaments/models/group_stage.py index 92c25c9..5d323ba 100644 --- a/tournaments/models/group_stage.py +++ b/tournaments/models/group_stage.py @@ -95,6 +95,13 @@ class GroupStage(models.Model): lgs.add_team(team) return lgs + def starts_soon(self): + if self.start_date: + early_start = self.start_date - timedelta(minute=30) + return datetime.now() > early_start + else: + return False + class LiveGroupStage: def __init__(self, title): self.title = title diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 04a1116..157c140 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -147,7 +147,10 @@ class Match(models.Model): if self.disabled is True: return False elif self.group_stage is None: - return (self.start_date or self.end_date) and len(self.team_scores.all()) > 0 + if len(self.team_scores.all()) == 2: + return True + else: + return (self.start_date or self.end_date) and len(self.team_scores.all()) > 0 else: return len(self.team_scores.all()) > 0 diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 743fb33..89dea15 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -102,7 +102,9 @@ class Tournament(models.Model): def filter_name(self): components = [self.formatted_start_date(), self.short_base_name()] - if self.event.name: + if self.event and self.event.club and self.event.club.name: + components.append(self.event.club.name) + elif self.event.name: components.append(self.event.name) elif self.name: components.append(self.name) @@ -337,7 +339,6 @@ class Tournament(models.Model): else: matches = [m for m in matches if m.disabled is False] - if matches: group = self.create_match_group(round.name(), matches, broadcasted) groups.append(group) @@ -360,7 +361,7 @@ class Tournament(models.Model): live_matches = [match.live_match() for match in matches] return MatchGroup(name, live_matches) - def live_group_stages(self): + def broadcasted_group_stages(self): group_stages = list(self.groupstage_set.all()) group_stages.sort(key=lambda gs: gs.index) return [gs.live_group_stages() for gs in group_stages] @@ -400,8 +401,8 @@ class Tournament(models.Model): group_stages = [] if self.group_stages_running(): - group_stages = self.live_group_stages() - matches = self.group_stages_matches() + group_stages = self.broadcasted_group_stages() + matches = self.broadcasted_group_stages_matches() else: # last_started_match = self.first_unfinished_match() current_round = self.round_to_show() @@ -413,7 +414,7 @@ class Tournament(models.Model): matches.extend(previous_round.get_matches_recursive(True)) else: matches.extend(current_round.all_matches(True)) - group_stages = self.live_group_stages() + group_stages = self.broadcasted_group_stages() return matches, group_stages @@ -471,16 +472,24 @@ class Tournament(models.Model): def round_for_index(self, index): return self.round_set.filter(index=index, parent=None).first() - def group_stages_matches(self): + def broadcasted_group_stages_matches(self): matches = [] - for group_stage in self.groupstage_set.all(): + 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 = [m for m in matches if m.should_appear()] - # matches.sort(key=lambda m: (m.non_null_start_date(), m.index), reverse=True) matches.sort(key=lambda m: (m.start_date is None, m.end_date is not None, m.start_date, m.index)) - return matches + def elected_broadcast_group_stages(self): + group_stages = list(self.groupstage_set.all()) + started = [gs for gs in group_stages if gs.starts_soon()] + if len(started) > 0: + return started + else: + return group_stages + def display_rankings(self): if self.publish_rankings is True and self.end_date is not None: return True @@ -587,7 +596,7 @@ class Tournament(models.Model): return date.replace(hour=8, minute=0, second=0, microsecond=0) def supposedly_in_progress(self): - end = self.start_date + timedelta(days=self.day_duration + 2) + end = self.start_date + timedelta(days=self.day_duration + 1) return self.start_date.replace(hour=0, minute=0) <= timezone.now() <= end def display_points_earned(self): diff --git a/tournaments/views.py b/tournaments/views.py index c3a1396..ee4d58f 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -224,7 +224,7 @@ def tournament_broadcasted_group_stages(request, tournament_id): def tournament_live_group_stage_json(request, tournament_id): tournament = get_object_or_404(Tournament, pk=tournament_id) - gs_dicts = [gs.to_dict() for gs in tournament.live_group_stages()] + gs_dicts = [gs.to_dict() for gs in tournament.broadcasted_group_stages()] # group_stages = tournament.live_group_stages() data = json.dumps(gs_dicts) return HttpResponse(data, content_type='application/json')