diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e81e0b..f4596a7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@
## Fixed
* Fix title not being displayed on the Recent sessions page
+* Avoid errors when displaying game overview with zero sessions
## 1.5.2 / 2024-01-14 21:27+01:00
diff --git a/games/templates/view_game.html b/games/templates/view_game.html
index 422770f..00fa2e5 100644
--- a/games/templates/view_game.html
+++ b/games/templates/view_game.html
@@ -62,6 +62,7 @@
Sessions
({{ session_count }})
+{% if latest_session_id %}
{% url 'view_game_start_session_from_session' latest_session_id as add_session_link %}
New
+{% endif %}
and Notes ({{ sessions_with_notes_count }})
diff --git a/games/views.py b/games/views.py
index d354efe..5d72290 100644
--- a/games/views.py
+++ b/games/views.py
@@ -27,7 +27,7 @@ from django.utils import timezone
from django.shortcuts import get_object_or_404
from common.time import format_duration
-from common.utils import safe_division
+from common.utils import safe_division, safe_getattr
from .forms import (
DeviceForm,
@@ -179,15 +179,20 @@ def view_game(request, game_id=None):
Session.objects.without_manual().filter(purchase__edition__game=game).count()
)
- playrange_start = sessions.earliest().timestamp_start.strftime("%b %Y")
- latest_session = sessions.latest()
- playrange_end = latest_session.timestamp_start.strftime("%b %Y")
+ if sessions:
+ playrange_start = sessions.earliest().timestamp_start.strftime("%b %Y")
+ latest_session = sessions.latest()
+ playrange_end = latest_session.timestamp_start.strftime("%b %Y")
+
+ playrange = (
+ playrange_start
+ if playrange_start == playrange_end
+ else f"{playrange_start} — {playrange_end}"
+ )
+ else:
+ playrange = "N/A"
+ latest_session = None
- playrange = (
- playrange_start
- if playrange_start == playrange_end
- else f"{playrange_start} — {playrange_end}"
- )
total_hours = float(format_duration(sessions.total_duration_unformatted(), "%2.1H"))
total_hours_without_manual = float(
format_duration(sessions.calculated_duration_unformatted(), "%2.1H")
@@ -209,7 +214,7 @@ def view_game(request, game_id=None):
"sessions": sessions.order_by("-timestamp_start"),
"title": f"Game Overview - {game.name}",
"hours_sum": total_hours,
- "latest_session_id": latest_session.pk,
+ "latest_session_id": safe_getattr(latest_session, "pk"),
}
request.session["return_path"] = request.path