Feat: Spiele-Historie-Ansicht für alle Team-User

Fügt eine neue Seite 'Spiele-Historie' hinzu, die allen Nutzern, die mit
einem oder mehreren Teams in Verbindung stehen (als Spieler, Head Coach,
Assistant Coach oder Elternteil), eine Übersicht über vergangene Spiele
ihrer Teams bietet. Die Spiele sind pro Team aufgeschlüsselt und zeigen
detaillierte Scorelines inklusive Inning-Ergebnissen an.

Änderungen umfassen:
- Neue View  in  zur Ermittlung der
  zugehörigen Teams und Abfrage der Spielhistorie.
- Aktualisierung der  in der View, um Inning-Scores auf 9
  Einträge aufzufüllen und so die Template-Logik zu vereinfachen.
- Neue URL-Konfiguration in .
- Neues Template  für die
  Darstellung der Spielhistorie mit detaillierter Scoreline pro Inning.
- Ergänzung eines Navigationslinks in  für
  authentifizierte Benutzer.
This commit is contained in:
Matthias Nagel 2025-11-19 10:00:58 +01:00
parent c63ad532b5
commit a37954de65
4 changed files with 135 additions and 2 deletions

View File

@ -0,0 +1,63 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="container mt-4">
<h1 class="mb-4">Past Games</h1>
{% if not games_by_team %}
<div class="alert alert-info" role="alert">
You are not associated with any team that has played games.
</div>
{% else %}
{% for team, games in games_by_team.items %}
<h2 class="mt-5 mb-3">{{ team.name }}</h2>
{% if not games %}
<p>No past games with results for this team.</p>
{% else %}
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Date</th>
<th scope="col">Opponent</th>
<th scope="col" colspan="11" class="text-center">Scoreline</th>
<th scope="col">Final</th>
</tr>
<tr>
<th colspan="2"></th>
{% for i in "123456789" %}
<th scope="col" class="text-center">{{ i }}</th>
{% endfor %}
<th class="text-center">R</th>
<th class="text-center">H</th>
</tr>
</thead>
<tbody>
{% for data in games %}
<tr>
<td rowspan="2">{{ data.game.start_time|date:"d.m.Y" }}</td>
<td>{{ data.game.opponent }}</td>
{% for inning_score in data.away_innings %}
<td class="text-center">{{ inning_score }}</td>
{% endfor %}
<td class="text-center fw-bold">{{ data.away_score }}</td>
<td class="text-center fw-bold">{{ data.game.result.away_hits }}</td>
</tr>
<tr>
<td class="fw-bold">{{ team.name }}</td>
{% for inning_score in data.home_innings %}
<td class="text-center">{{ inning_score }}</td>
{% endfor %}
<td class="text-center fw-bold">{{ data.home_score }}</td>
<td class="text-center fw-bold">{{ data.game.result.home_hits }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
{% endfor %}
{% endif %}
</div>
{% endblock %}

View File

@ -4,4 +4,5 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.dashboard, name='dashboard'), path('', views.dashboard, name='dashboard'),
path('players/', views.player_list, name='player_list'), path('players/', views.player_list, name='player_list'),
path('past-games/', views.past_games, name='past_games'),
] ]

View File

@ -1,11 +1,12 @@
from django.shortcuts import render from django.shortcuts import render
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from calendars.models import Event, EventParticipation from calendars.models import Event, EventParticipation, Game
from clubs.models import Team from clubs.models import Team
from accounts.models import CustomUser from accounts.models import CustomUser
from django.db.models import Q from django.db.models import Q
from django.utils import timezone from django.utils import timezone
import datetime import datetime
from itertools import chain
def get_all_child_teams(parent_team): def get_all_child_teams(parent_team):
""" """
@ -40,7 +41,7 @@ def dashboard(request):
assisted_teams = user.assisted_teams.all() assisted_teams = user.assisted_teams.all()
from itertools import chain
all_teams = list(set(chain(player_teams, expanded_coached_teams, assisted_teams))) all_teams = list(set(chain(player_teams, expanded_coached_teams, assisted_teams)))
now = timezone.now() now = timezone.now()
@ -130,3 +131,68 @@ def player_list(request):
'teams': all_teams 'teams': all_teams
} }
return render(request, 'dashboard/player_list.html', context) return render(request, 'dashboard/player_list.html', context)
@login_required
def past_games(request):
user = request.user
user_teams = set()
# Player's team
if user.team:
user_teams.add(user.team)
# Coached teams
for team in user.coached_teams.all():
user_teams.add(team)
user_teams.update(get_all_child_teams(team))
# Assisted teams
for team in user.assisted_teams.all():
user_teams.add(team)
# Parents' children's teams
if hasattr(user, 'children'):
for child in user.children.all():
if child.team:
user_teams.add(child.team)
# Fetch past games for all collected teams
games_qs = Game.objects.filter(
team__in=list(user_teams),
start_time__lt=timezone.now(),
result__isnull=False
).select_related('team', 'result').order_by('team__name', '-start_time')
# Group games by team
games_by_team = {}
for game in games_qs:
if game.team not in games_by_team:
games_by_team[game.team] = []
# Prepare scoreline data
result = game.result
sorted_items = sorted(result.inning_results.items(), key=lambda x: int(x[0].split('_')[1]))
home_innings = [item[1].get('home', 'X') for item in sorted_items]
away_innings = [item[1].get('guest', 'X') for item in sorted_items]
# Pad innings to 9
home_innings.extend([''] * (9 - len(home_innings)))
away_innings.extend([''] * (9 - len(away_innings)))
home_score = sum(i for i in home_innings if isinstance(i, int))
away_score = sum(i for i in away_innings if isinstance(i, int))
game_data = {
'game': game,
'home_score': home_score,
'away_score': away_score,
'home_innings': home_innings[:9],
'away_innings': away_innings[:9]
}
games_by_team[game.team].append(game_data)
context = {
'games_by_team': games_by_team
}
return render(request, 'dashboard/past_games.html', context)

View File

@ -38,6 +38,9 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'edit_profile' %}">Profile</a> <a class="nav-link" href="{% url 'edit_profile' %}">Profile</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="{% url 'past_games' %}">Past Games</a>
</li>
{% if user.coached_teams.all %} {% if user.coached_teams.all %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'player-add' %}">Create New Player</a> <a class="nav-link" href="{% url 'player-add' %}">Create New Player</a>