From 3197938fa0092f1c854e373b300c5b8e9a27955c Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 29 Mar 2024 15:02:09 +0100 Subject: [PATCH] Automatic broadcasting --- tournaments/models/match.py | 35 ++--- tournaments/models/team_score.py | 4 +- tournaments/models/tournament.py | 58 ++++++-- .../templates/tournaments/base_head.html | 15 ++ .../templates/tournaments/broadcast.html | 1 + .../templates/tournaments/broadcast_base.html | 14 +- .../templates/tournaments/broadcasted.html | 90 ------------ .../tournaments/broadcasted_auto.html | 133 ++++++++++++++++++ .../tournaments/broadcasted_group_stages.html | 2 +- .../tournaments/broadcasted_matches.html | 2 +- .../tournaments/broadcasted_summons.html | 2 +- tournaments/urls.py | 3 +- tournaments/views.py | 26 ++-- 13 files changed, 233 insertions(+), 152 deletions(-) create mode 100644 tournaments/templates/tournaments/base_head.html delete mode 100644 tournaments/templates/tournaments/broadcasted.html create mode 100644 tournaments/templates/tournaments/broadcasted_auto.html diff --git a/tournaments/models/match.py b/tournaments/models/match.py index a163f2c..d477b50 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -149,20 +149,21 @@ class Match(models.Model): class Team: def __init__(self, image, names, scores, weight, is_winner): + # print(f"image = {image}, names= {names}, scores ={scores}, weight={weight}, win={is_winner}") self.image = image self.names = names self.scores = scores self.weight = weight self.is_winner = is_winner - # def to_dict(self): - # return { - # "image": self.image, - # "names": self.names, - # "scores": self.scores, - # "weight": self.weight, - # "is_winner": self.is_winner, - # } + def to_dict(self): + return { + "image": self.image, + "names": self.names, + "scores": self.scores, + "weight": self.weight, + "is_winner": self.is_winner, + } class LiveMatch: def __init__(self, title, date, duration, court, started): @@ -176,12 +177,12 @@ class LiveMatch: def add_team(self, team): self.teams.append(team) - # def to_dict(self): - # return { - # "title": self.title, - # "date": self.date, - # "teams": [team.to_dict() for team in self.teams], - # "duration": self.duration, - # "court": self.court, - # "started": self.started, - # } + def to_dict(self): + return { + "title": self.title, + "date": self.date, + "teams": [team.to_dict() for team in self.teams], + "duration": self.duration, + "court": self.court, + "started": self.started, + } diff --git a/tournaments/models/team_score.py b/tournaments/models/team_score.py index 6c06b6b..029061d 100644 --- a/tournaments/models/team_score.py +++ b/tournaments/models/team_score.py @@ -18,7 +18,7 @@ class TeamScore(models.Model): if self.team_registration.name: return self.team_registration.name else: - names = map(lambda player: player.name(), self.player_registrations.all()) + names = [player.name() for player in self.player_registrations.all()] return " - ".join(names) def team_names(self): @@ -26,7 +26,7 @@ class TeamScore(models.Model): if self.team_registration.name: names.append(self.team_registration.name) else: - names = list(map(lambda player: player.name(), self.player_registrations.all())) + names = [player.name() for player in self.player_registrations.all()] return names def scores(self): diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index a43206b..7423bfc 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -131,36 +131,70 @@ class Tournament(models.Model): def live_group_stages(self): return [gs.live_group_stages() for gs in self.groupstage_set.all()] - def live_matches(self): + def broadcast_content(self): + + now = timezone.now() + + today_matches = [match for match in self.all_matches() if match.start_date.date() == now] + today_matches.sort(key=lambda m: m.start_date) + + matches, group_stages = self.broadcasted_matches_and_group_stages() + group_stages_dicts = [gs.to_dict() for gs in group_stages] + + # if now is before the first match, we want to show the summons + group stage or first matches + if today_matches and now < today_matches[0].start_date: + team_summons_dicts = [summon.to_dict() for summon in self.team_summons()] + if group_stages: + return { + 'matches': [], + 'group_stages': group_stages_dicts, + 'summons': team_summons_dicts, + } + else: + live_matches_dicts = [match.live_match().to_dict() for match in matches] + return { + 'matches': live_matches_dicts, + 'group_stages': [], + 'summons': team_summons_dicts, + } + else: # we want to display the broadcasted content + live_matches_dicts = [match.live_match().to_dict() for match in matches] + return { + 'matches': live_matches_dicts, + 'group_stages': group_stages_dicts, + 'summons': [], + } + + def broadcasted_matches_and_group_stages(self): matches = [] - last_started_match = self.last_match() + group_stages = [] + last_started_match = self.last_started_match() current_round = last_started_match.round if current_round: - previous_round = self.round_for_index(current_round.index - 1) + previous_round = self.round_for_index(current_round.index + 1) if previous_round: matches.extend(current_round.all_matches()) matches.extend(previous_round.all_matches()) else: matches.extend(current_round.all_matches()) - # todo on veut les matchs du round et les poules, comment ça marche ? - # dans le monde idéal on veut afficher plusieurs pages de plusieurs types différents - # est-ce que je construis un json avec des types différents dans un même array ? - # je veux pas que des types différents apparaissent sur une même page + group_stages = self.live_group_stages() else: matches = self.group_stages_matches() - return [match.live_match() for match in matches] + return matches, group_stages - def last_match(self): + def all_matches(self): matches = [] for round in self.round_set.all(): - matches.extend(round.match_set.all()) + matches.extend(round.all_matches()) for group_stage in self.groupstage_set.all(): matches.extend(group_stage.match_set.all()) + return matches - matches = [m for m in matches if m.start_date] + def last_started_match(self): + matches = [m for m in self.all_matches() if m.start_date] matches.sort(key=lambda m: m.start_date, reverse=True) return matches[0] @@ -172,7 +206,7 @@ class Tournament(models.Model): for group_stage in self.groupstage_set.all(): matches.extend(group_stage.match_set.all()) matches = [m for m in matches if m.should_appear()] - matches.sort(key=lambda m: -m.start_date) + matches.sort(key=lambda m: m.start_date, reverse=True) return matches class MatchGroup: diff --git a/tournaments/templates/tournaments/base_head.html b/tournaments/templates/tournaments/base_head.html new file mode 100644 index 0000000..ccc0cdf --- /dev/null +++ b/tournaments/templates/tournaments/base_head.html @@ -0,0 +1,15 @@ +{% load static %} + + + + + + + diff --git a/tournaments/templates/tournaments/broadcast.html b/tournaments/templates/tournaments/broadcast.html index a7e4a93..a2c4de8 100644 --- a/tournaments/templates/tournaments/broadcast.html +++ b/tournaments/templates/tournaments/broadcast.html @@ -11,6 +11,7 @@
+
Automatic
Matchs
Poules
Convocations
diff --git a/tournaments/templates/tournaments/broadcast_base.html b/tournaments/templates/tournaments/broadcast_base.html index 3dca010..35144a2 100644 --- a/tournaments/templates/tournaments/broadcast_base.html +++ b/tournaments/templates/tournaments/broadcast_base.html @@ -3,19 +3,7 @@ {% load static %} - - - - - - + {% include 'tournaments/base_head.html' %} diff --git a/tournaments/templates/tournaments/broadcasted.html b/tournaments/templates/tournaments/broadcasted.html deleted file mode 100644 index 176220e..0000000 --- a/tournaments/templates/tournaments/broadcasted.html +++ /dev/null @@ -1,90 +0,0 @@ -{% extends 'tournaments/broadcast_base.html' %} - -{% block head_title %}Convocations{% endblock %} -{% block first_title %}{{ tournament.name }}{% endblock %} -{% block second_title %}Convocations{% endblock %} - -{% block content %} - -{% load static %} - -
- -
- - - - - - - -
-
- -{% endblock %} diff --git a/tournaments/templates/tournaments/broadcasted_auto.html b/tournaments/templates/tournaments/broadcasted_auto.html new file mode 100644 index 0000000..94d90b8 --- /dev/null +++ b/tournaments/templates/tournaments/broadcasted_auto.html @@ -0,0 +1,133 @@ + + + {% load static %} + + + {% include 'tournaments/base_head.html' %} + + + + Broadcast + + + + + +
+ +
+
+
+
+
+ +
+

{{ tournament.name }}

+

+ +
+
+
+
+
+ +
+ +
+ + + + + + + +
+ + +
+
+ +
+ + diff --git a/tournaments/templates/tournaments/broadcasted_group_stages.html b/tournaments/templates/tournaments/broadcasted_group_stages.html index 5728bc1..a11093f 100644 --- a/tournaments/templates/tournaments/broadcasted_group_stages.html +++ b/tournaments/templates/tournaments/broadcasted_group_stages.html @@ -9,7 +9,7 @@ - Padel + Poules diff --git a/tournaments/templates/tournaments/broadcasted_matches.html b/tournaments/templates/tournaments/broadcasted_matches.html index b6223d5..2959a97 100644 --- a/tournaments/templates/tournaments/broadcasted_matches.html +++ b/tournaments/templates/tournaments/broadcasted_matches.html @@ -9,7 +9,7 @@ - Padel + Matchs diff --git a/tournaments/templates/tournaments/broadcasted_summons.html b/tournaments/templates/tournaments/broadcasted_summons.html index c3978fd..157e74b 100644 --- a/tournaments/templates/tournaments/broadcasted_summons.html +++ b/tournaments/templates/tournaments/broadcasted_summons.html @@ -8,7 +8,6 @@ {% load static %} -