From 26a5ecf2c90ccaac0eb664a515e2a0aca15a6316 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 25 Mar 2024 17:49:13 +0100 Subject: [PATCH] Add rounds in the matches navigtaion --- tournaments/models/match.py | 2 +- tournaments/models/tournament.py | 38 +++++++++++++++---- tournaments/templates/tournaments/base.html | 4 ++ .../templates/tournaments/matches.html | 13 +++++++ tournaments/views.py | 11 +++++- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 2a6e80d..17ab232 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -6,8 +6,8 @@ import uuid class Match(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) round = models.ForeignKey(Round, null=True, blank=True, on_delete=models.CASCADE) - name = models.CharField(max_length=200, null=True, blank=True) group_stage = models.ForeignKey(GroupStage, null=True, blank=True, on_delete=models.CASCADE) + name = models.CharField(max_length=200, null=True, blank=True) start_date = models.DateTimeField(null=True, blank=True) end_date = models.DateTimeField(null=True, blank=True) index = models.IntegerField(default=0) diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 10610dc..a8f104e 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -53,6 +53,9 @@ class Tournament(models.Model): def formatted_start_date(self): return self.start_date.strftime("%d/%m/%y") + def in_progress(self): + return self.end_date is None + def team_summons(self): summons = [] for team_registration in self.teamregistration_set.all(): @@ -67,14 +70,15 @@ class Tournament(models.Model): summons.sort(key=lambda s: s.weight) return summons - def live_matches(self, broadcasted): + def live_matches(self, broadcasted, group_stage_id, round_id): + matches = [] - for group_stage in self.groupstage_set.all(): - for match in group_stage.match_set.all(): - matches.append(match) - for round in self.round_set.all(): - for match in round.match_set.all(): - matches.append(match) + if group_stage_id: + matches = self.group_stage_matches(group_stage_id) + elif round_id: + matches = self.round_matches(round_id) + else: + matches = self.all_matches() matches = [m for m in matches if m.broadcasted==True] if not broadcasted: @@ -85,6 +89,26 @@ class Tournament(models.Model): return [match.live_match() for match in matches] # return map(lambda match: match.live_match(), matches) + def all_matches(self): + matches = [] + for group_stage in self.groupstage_set.all(): + for match in group_stage.match_set.all(): + matches.append(match) + for round in self.round_set.all(): + for match in round.match_set.all(): + matches.append(match) + return matches + + def group_stage_matches(self, group_stage_id): + group_stage = self.groupstage_set.filter(id=group_stage_id).first() + print(group_stage.name()) + print(len(group_stage.match_set.all())) + return group_stage.match_set.all() + + def round_matches(self, round_id): + round = self.round_set.filter(id=round_id).first() + return round.match_set.all() + def live_group_stages(self): return [gs.live_group_stages() for gs in self.groupstage_set.all()] # return map(lambda gs: gs.live_group_stages(), self.groupstage_set.all()) diff --git a/tournaments/templates/tournaments/base.html b/tournaments/templates/tournaments/base.html index 131f31e..dadb3fe 100644 --- a/tournaments/templates/tournaments/base.html +++ b/tournaments/templates/tournaments/base.html @@ -44,7 +44,11 @@
diff --git a/tournaments/templates/tournaments/matches.html b/tournaments/templates/tournaments/matches.html index 8df8f07..0bc561a 100644 --- a/tournaments/templates/tournaments/matches.html +++ b/tournaments/templates/tournaments/matches.html @@ -6,7 +6,20 @@ {% block content %} +{% if rounds or group_stages %} + + {% endif %} + {% if matches %} +
{% for match in matches %} {% include 'tournaments/match_cell.html' %} diff --git a/tournaments/views.py b/tournaments/views.py index b5fc2ba..769f0d5 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -35,10 +35,19 @@ def index(request): ) def tournament(request, tournament_id): + tournament = get_object_or_404(Tournament, pk=tournament_id) - live_matches = list(tournament.live_matches(broadcasted=False)) + + round_id = request.GET.get('round') + group_stage_id = request.GET.get('group_stage') + live_matches = list(tournament.live_matches(False, group_stage_id, round_id)) + + rounds = tournament.round_set.order_by('index') + group_stages = tournament.groupstage_set.order_by('index') return render(request, 'tournaments/matches.html', { 'tournament': tournament, + 'rounds': rounds, + 'group_stages': group_stages, 'matches': live_matches, })