From 85f52fc73597a2b81cf8e232c2a3696404a9486a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 5 Jan 2023 16:33:32 +0100 Subject: [PATCH] Fix checking duration when database is empty --- src/web/tracker/views.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/web/tracker/views.py b/src/web/tracker/views.py index 12a1f7e..05a200c 100644 --- a/src/web/tracker/views.py +++ b/src/web/tracker/views.py @@ -103,9 +103,13 @@ def add_platform(request): def index(request): context = {} - result = Session.objects.all().aggregate(Sum("duration_calculated")) - context["total_duration"] = format_duration( - result["duration_calculated__sum"], "%H hours %m minutes" - ) + if Session.objects.count() == 0: + duration_value = 0 + else: + result = Session.objects.all().aggregate(Sum("duration_calculated")) + context["total_duration"] = format_duration( + result["duration_calculated__sum"], "%H hours %m minutes" + ) + context["total_duration"] = duration_value context["title"] = "Index" return render(request, "index.html", context)