diff --git a/team_stats/templates/team_stats/season_report.html b/team_stats/templates/team_stats/season_report.html new file mode 100644 index 0000000..7899605 --- /dev/null +++ b/team_stats/templates/team_stats/season_report.html @@ -0,0 +1,107 @@ + + + + + Season Report for {{ team.name }} - {{ season }} + + + + + +

Season Report

+

Team: {{ team.name }}

+

Season: {{ season }}

+ + + + + + + {% for player in players %} + + {% endfor %} + + + + {% for row in report_data %} + + + + {% for status in row.statuses %} + + {% endfor %} + + {% empty %} + + + + {% endfor %} + +
DateOpponent
{{ player.get_full_name }}
{{ row.game.start_time|date:"d.m.Y" }}{{ row.game.opponent }} + {% if status == 'attending' %} + ✔ + {% elif status == 'rejected' %} + ✖ + {% else %} + ? + {% endif %} +
No games found for this season.
+ + + diff --git a/team_stats/templates/team_stats/team_statistics.html b/team_stats/templates/team_stats/team_statistics.html index 1120bd4..28949d8 100644 --- a/team_stats/templates/team_stats/team_statistics.html +++ b/team_stats/templates/team_stats/team_statistics.html @@ -22,6 +22,11 @@
+ {% if selected_season %} +
+ Generate Season Report +
+ {% endif %} diff --git a/team_stats/urls.py b/team_stats/urls.py index e514dab..8e50771 100644 --- a/team_stats/urls.py +++ b/team_stats/urls.py @@ -5,4 +5,5 @@ app_name = 'team_stats' urlpatterns = [ path('team//', views.team_statistics, name='team_statistics'), + path('team//report//', views.season_report, name='season_report'), ] diff --git a/team_stats/views.py b/team_stats/views.py index a2f821e..7043f11 100644 --- a/team_stats/views.py +++ b/team_stats/views.py @@ -147,3 +147,50 @@ def team_statistics(request, team_id): } return render(request, 'team_stats/team_statistics.html', context) + +@login_required +def season_report(request, team_id, season): + team = get_object_or_404(Team, pk=team_id) + + # Security check: only head coach can view + if request.user != team.head_coach: + return HttpResponseForbidden("You are not authorized to view this report.") + + # 1. Get all players for the team, ordered + players = team.players.all().order_by('last_name', 'first_name') + player_ids = [p.id for p in players] + + # 2. Get all games for the team and season + games = Game.objects.filter(team=team, season=season).order_by('start_time') + game_ids = [g.id for g in games] + + # 3. Get all relevant participation data in one query + participations = EventParticipation.objects.filter( + event_id__in=game_ids, + user_id__in=player_ids + ) + + # 4. Create a fast lookup map + participation_map = { + (p.event_id, p.user_id): p.status for p in participations + } + + # 5. Build the final data structure for the template + report_data = [] + for game in games: + statuses = [] + for player in players: + status = participation_map.get((game.id, player.id), 'maybe') + statuses.append(status) + report_data.append({ + 'game': game, + 'statuses': statuses + }) + + context = { + 'team': team, + 'season': season, + 'players': players, + 'report_data': report_data, + } + return render(request, 'team_stats/season_report.html', context) \ No newline at end of file