From 8466f67c86086dc83ed666d76d76fde9ba489776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 1 Jan 2024 18:21:50 +0100 Subject: [PATCH] Fix errors caused by empty values --- games/views.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/games/views.py b/games/views.py index 8cd5eed..114e3e5 100644 --- a/games/views.py +++ b/games/views.py @@ -328,7 +328,10 @@ def stats(request, year: int = 0): ) ) longest_session = this_year_sessions_with_durations.order_by("-duration").first() - this_year_games_with_session_counts = Game.objects.annotate( + this_year_games = Game.objects.filter( + edition__purchase__session__in=this_year_sessions + ).distinct() + this_year_games_with_session_counts = this_year_games.annotate( session_count=Count( "edition__purchase__session", filter=Q(edition__purchase__session__timestamp_start__year=year), @@ -469,15 +472,24 @@ def stats(request, year: int = 0): ), "backlog_decrease_count": backlog_decrease_count, "longest_session_time": format_duration( - longest_session.duration if longest_session else timedelta(0), - "%2.0Hh %2.0mm", - ), - "longest_session_game": longest_session.purchase.edition.name, - "highest_session_count": game_highest_session_count.session_count, - "highest_session_count_game": game_highest_session_count.name, + longest_session.duration, "%2.0Hh %2.0mm" + ) + if longest_session + else 0, + "longest_session_game": longest_session.purchase.edition.name + if longest_session + else "N/A", + "highest_session_count": game_highest_session_count.session_count + if game_highest_session_count + else 0, + "highest_session_count_game": game_highest_session_count.name + if game_highest_session_count + else "N/A", "highest_session_average": format_duration( highest_session_average_game.session_average, "%2.0Hh %2.0mm" - ), + ) + if highest_session_average_game + else 0, "highest_session_average_game": highest_session_average_game, "title": f"{year} Stats", }