From 96362c2b5c3c07cfdb35d8b065795ca672424ba9 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Thu, 16 May 2024 18:23:32 +0200 Subject: [PATCH] add new teams page --- tournaments/models/group_stage.py | 2 +- tournaments/models/match.py | 2 +- tournaments/models/team_registration.py | 15 ++++-- tournaments/models/tournament.py | 48 ++++++++++++++++++- .../tournaments/navigation_tournament.html | 4 ++ .../templates/tournaments/team_row.html | 13 +++++ tournaments/templates/tournaments/teams.html | 35 ++++++++++++++ tournaments/urls.py | 1 + tournaments/views.py | 10 ++++ 9 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 tournaments/templates/tournaments/team_row.html create mode 100644 tournaments/templates/tournaments/teams.html diff --git a/tournaments/models/group_stage.py b/tournaments/models/group_stage.py index 83dffd2..d335dff 100644 --- a/tournaments/models/group_stage.py +++ b/tournaments/models/group_stage.py @@ -137,7 +137,7 @@ class GroupStageTeam: self.wins = 0 self.losses = 0 self.diff = 0 - self.weight = team_registration.get_weight() + self.weight = team_registration.weight def wins_losses(self): return f"{self.wins}/{self.losses}" diff --git a/tournaments/models/match.py b/tournaments/models/match.py index c46b85d..22f17af 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -123,7 +123,7 @@ class Match(models.Model): image = team_score.team_registration.logo names = team_score.team_names() scores = team_score.scores_array() - weight = team_score.team_registration.get_weight() + weight = team_score.team_registration.weight is_winner = team_score.team_registration.id == self.winning_team_id walk_out = team_score.walk_out team = Team(image, names, scores, weight, is_winner, walk_out) diff --git a/tournaments/models/team_registration.py b/tournaments/models/team_registration.py index e5dd166..5e5504f 100644 --- a/tournaments/models/team_registration.py +++ b/tournaments/models/team_registration.py @@ -70,10 +70,6 @@ class TeamRegistration(models.Model): return self.group_stage.name() else: return "--" - - def get_weight(self): - top_two_players = self.playerregistration_set.all().order_by('rank')[:2] - weight = 0 for player in top_two_players: rank = player.rank if player.rank is not None else 0 weight += rank @@ -81,3 +77,14 @@ class TeamRegistration(models.Model): def is_valid_for_summon(self): return len(self.playerregistration_set.all()) > 0 + + def is_valid_for_display(self): + return len(self.playerregistration_set.all()) > 0 and self.walk_out is False + + def base_start_stage(self): + if self.group_stage_position is not None: + return "Poule" + if self.bracket_position is not None: + return "Tableau" + + return "Attente" diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index add4690..ec5c00c 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -75,13 +75,26 @@ class Tournament(models.Model): if next_match: names = team_registration.team_names() stage = next_match.stage_name() - weight = team_registration.get_weight() + weight = team_registration.weight summon = TeamSummon(names, next_match.start_date, weight, stage, team_registration.logo) summons.append(summon) summons.sort(key=lambda s: s.weight) return summons + def teams(self): + teams = [] + for team_registration in self.teamregistration_set.all(): + if team_registration.is_valid_for_display(): + names = team_registration.team_names() + weight = team_registration.weight + stage = team_registration.base_start_stage() + team = Team(names, weight, stage, team_registration.logo) + teams.append(team) + + teams.sort(key=lambda s: s.weight) + return teams + def match_groups(self, broadcasted, group_stage_id, round_id): match_groups = [] @@ -153,7 +166,8 @@ class Tournament(models.Model): 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 timezone.now() < self.start_date: + # change timezone to datetime to avoid the bug RuntimeWarning: DateTimeField Tournament.start_date received a naive datetime (2024-05-16 00:00:00) while time zone support is active. + if datetime.now().date() < self.start_date.date(): team_summons_dicts = [summon.to_dict() for summon in self.team_summons()] if group_stages: return { @@ -261,6 +275,15 @@ class Tournament(models.Model): matches.sort(key=lambda m: m.start_date, reverse=True) return matches + def display_teams(self): + if self.end_date is not None: + return True + if self.publish_teams: + return True + if datetime.now().date() >= self.start_date.date(): + return True + return False + def display_summons(self): if self.end_date is not None: return False @@ -292,6 +315,9 @@ class Tournament(models.Model): def group_stage_start_date(self): group_stages = [gs for gs in self.groupstage_set.all() if gs.start_date is not None] + if len(group_stages) == 0: + return None + return min(group_stages, key=lambda gs: gs.start_date).start_date def display_matches(self): @@ -322,6 +348,9 @@ class Tournament(models.Model): def first_match_start_date(self, bracket_matches): matches = [m for m in bracket_matches if m.start_date is not None] + if len(matches) == 0: + return None + return min(matches, key=lambda m: m.start_date).start_date def getEightAm(self, date): @@ -361,3 +390,18 @@ class TeamSummon: "stage": self.stage, "image": self.image, } + +class Team: + def __init__(self, names, weight, stage, image): + self.names = names + self.weight = weight + self.stage = stage + self.image = image + + def to_dict(self): + return { + "names": self.names, + "weight": self.weight, + "stage": self.stage, + "image": self.image, + } diff --git a/tournaments/templates/tournaments/navigation_tournament.html b/tournaments/templates/tournaments/navigation_tournament.html index f18083e..f637f3b 100644 --- a/tournaments/templates/tournaments/navigation_tournament.html +++ b/tournaments/templates/tournaments/navigation_tournament.html @@ -11,4 +11,8 @@ Convocations {% endif %} + {% if tournament.display_teams %} + Équipes + {% endif %} + diff --git a/tournaments/templates/tournaments/team_row.html b/tournaments/templates/tournaments/team_row.html new file mode 100644 index 0000000..db894ae --- /dev/null +++ b/tournaments/templates/tournaments/team_row.html @@ -0,0 +1,13 @@ +{% load static %} + +
+ +
+ {% for name in team.names %} +
{{ name }}
+ {% endfor %} +
+ +
{{ team.weight }}
+
{{ team.stage }}
+
diff --git a/tournaments/templates/tournaments/teams.html b/tournaments/templates/tournaments/teams.html new file mode 100644 index 0000000..10a246b --- /dev/null +++ b/tournaments/templates/tournaments/teams.html @@ -0,0 +1,35 @@ +{% extends 'tournaments/base.html' %} + +{% block head_title %}Équipes{% endblock %} +{% block first_title %}{{ tournament.display_name }}{% endblock %} +{% block second_title %}Équipes{% endblock %} + +{% if tournament.display_teams %} + +{% block content %} + +{% load static %} + + {% include 'tournaments/navigation_tournament.html' %} + + {% if teams %} + +
+ +
+
+ {% for team in teams %} + + {% include 'tournaments/team_row.html' %} + + {% endfor %} + +
+
+ +
+ + {% endif %} + +{% endblock %} +{% endif %} diff --git a/tournaments/urls.py b/tournaments/urls.py index 6bd253c..c356146 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -9,6 +9,7 @@ urlpatterns = [ path("tournament//", include([ path('', views.tournament, name='tournament'), + path('teams/', views.tournament_teams, name='tournament-teams'), path('summons/', views.tournament_summons, name='tournament-summons'), path('broadcast/summons/', views.tournament_broadcasted_summons, name='broadcasted-summons'), path('summons/json/', views.tournament_summons_json, name='tournament-summons-json'), diff --git a/tournaments/views.py b/tournaments/views.py index 15d51f0..2af29a7 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -97,6 +97,16 @@ def tournament(request, tournament_id): 'match_groups': match_groups, }) +def tournament_teams(request, tournament_id): + + tournament = get_object_or_404(Tournament, pk=tournament_id) + teams = tournament.teams() + + return render(request, 'tournaments/teams.html', { + 'tournament': tournament, + 'teams': teams, + }) + def tournament_summons(request, tournament_id): tournament = get_object_or_404(Tournament, pk=tournament_id)