diff --git a/dashboard/templates/dashboard/past_games.html b/dashboard/templates/dashboard/past_games.html
new file mode 100644
index 0000000..002328a
--- /dev/null
+++ b/dashboard/templates/dashboard/past_games.html
@@ -0,0 +1,63 @@
+{% extends "base.html" %}
+{% load static %}
+
+{% block content %}
+
+
Past Games
+
+ {% if not games_by_team %}
+
+ You are not associated with any team that has played games.
+
+ {% else %}
+ {% for team, games in games_by_team.items %}
+
{{ team.name }}
+ {% if not games %}
+
No past games with results for this team.
+ {% else %}
+
+
+
+
+ | Date |
+ Opponent |
+ Scoreline |
+ Final |
+
+
+ |
+ {% for i in "123456789" %}
+ {{ i }} |
+ {% endfor %}
+ R |
+ H |
+
+
+
+ {% for data in games %}
+
+ | {{ data.game.start_time|date:"d.m.Y" }} |
+ {{ data.game.opponent }} |
+ {% for inning_score in data.away_innings %}
+ {{ inning_score }} |
+ {% endfor %}
+ {{ data.away_score }} |
+ {{ data.game.result.away_hits }} |
+
+
+ | {{ team.name }} |
+ {% for inning_score in data.home_innings %}
+ {{ inning_score }} |
+ {% endfor %}
+ {{ data.home_score }} |
+ {{ data.game.result.home_hits }} |
+
+ {% endfor %}
+
+
+
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+
+{% endblock %}
diff --git a/dashboard/urls.py b/dashboard/urls.py
index f92d1f8..10c29c3 100644
--- a/dashboard/urls.py
+++ b/dashboard/urls.py
@@ -4,4 +4,5 @@ from . import views
urlpatterns = [
path('', views.dashboard, name='dashboard'),
path('players/', views.player_list, name='player_list'),
+ path('past-games/', views.past_games, name='past_games'),
]
\ No newline at end of file
diff --git a/dashboard/views.py b/dashboard/views.py
index 5191d5d..e637547 100644
--- a/dashboard/views.py
+++ b/dashboard/views.py
@@ -1,11 +1,12 @@
from django.shortcuts import render
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 accounts.models import CustomUser
from django.db.models import Q
from django.utils import timezone
import datetime
+from itertools import chain
def get_all_child_teams(parent_team):
"""
@@ -40,7 +41,7 @@ def dashboard(request):
assisted_teams = user.assisted_teams.all()
- from itertools import chain
+
all_teams = list(set(chain(player_teams, expanded_coached_teams, assisted_teams)))
now = timezone.now()
@@ -130,3 +131,68 @@ def player_list(request):
'teams': all_teams
}
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)
diff --git a/templates/base.html b/templates/base.html
index f311321..fe8738a 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -38,6 +38,9 @@
Profile
+
+ Past Games
+
{% if user.coached_teams.all %}
Create New Player