From 32f10e183e6fcfb827d65949b22dc89ae0434d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 5 Jan 2023 11:24:07 +0100 Subject: [PATCH] Display total hours played on homepage --- CHANGELOG.md | 1 + src/web/tracker/templates/index.html | 2 +- src/web/tracker/views.py | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a976fd..9834b4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Unreleased +* Display total hours played on homepage * Add format_duration to common.util.time * Allow deleting sessions * Redirect after adding game/platform/purchase/session diff --git a/src/web/tracker/templates/index.html b/src/web/tracker/templates/index.html index 831eb9b..16b8a27 100644 --- a/src/web/tracker/templates/index.html +++ b/src/web/tracker/templates/index.html @@ -5,7 +5,7 @@ {% block content %}
{% if session_count > 0 %} -You have played a total of {{ session_count }} sessions. +You have played a total of {{ session_count }} sessions for a total of {{ total_duration }}. {% else %} Start by clicking the links at the top. To track playtime, you need to have at least 1 owned game. {% endif %} diff --git a/src/web/tracker/views.py b/src/web/tracker/views.py index f100f16..12a1f7e 100644 --- a/src/web/tracker/views.py +++ b/src/web/tracker/views.py @@ -5,7 +5,8 @@ from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm from datetime import datetime from zoneinfo import ZoneInfo from django.conf import settings -from common.util.time import now as now_with_tz +from common.util.time import now as now_with_tz, format_duration +from django.db.models import Sum def model_counts(request): @@ -102,4 +103,9 @@ 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" + ) + context["title"] = "Index" return render(request, "index.html", context)