Compare commits

..

No commits in common. "32f10e183e6fcfb827d65949b22dc89ae0434d42" and "4b451273351f779bec70a24ffcf94339ad5f1462" have entirely different histories.

4 changed files with 3 additions and 48 deletions

View File

@ -1,6 +1,4 @@
## Unreleased ## Unreleased
* Display total hours played on homepage
* Add format_duration to common.util.time
* Allow deleting sessions * Allow deleting sessions
* Redirect after adding game/platform/purchase/session * Redirect after adding game/platform/purchase/session
* Fix display of duration_manual * Fix display of duration_manual

View File

@ -1,44 +1,7 @@
from datetime import datetime, timedelta from datetime import datetime
from django.conf import settings from django.conf import settings
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import re
def now(): def now():
return datetime.now(ZoneInfo(settings.TIME_ZONE)) return datetime.now(ZoneInfo(settings.TIME_ZONE))
def format_duration(
duration: timedelta, format_string: str = "%H hours %m minutes"
) -> str:
"""
Format timedelta into the specified format_string.
If duration is less than 60 seconds, skips formatting, returns "less than a minute".
If duration is 0, skips formatting, returns 0.
Valid format variables:
- %H hours
- %m minutes
- %s seconds
"""
seconds_in_hours = 3600
seconds_in_minute = 60
hours: int
minutes: int
seconds: int
seconds_total: int
remainder: int
seconds_total = duration.total_seconds()
if seconds_total == 0:
return 0
else:
hours = int(seconds_total // seconds_in_hours)
remainder = int(seconds_total % seconds_in_hours)
minutes = int(remainder // seconds_in_minute)
if hours == 0 and minutes == 0:
return "less than a minute"
seconds = int(minutes % seconds_in_minute)
literals = {"%H": str(hours), "%m": str(minutes), "%s": str(seconds)}
formatted_string = format_string
for pattern, replacement in literals.items():
formatted_string = re.sub(pattern, replacement, formatted_string)
return formatted_string

View File

@ -5,7 +5,7 @@
{% block content %} {% block content %}
<div class="text-slate-300 mx-auto max-w-screen-lg text-center"> <div class="text-slate-300 mx-auto max-w-screen-lg text-center">
{% if session_count > 0 %} {% if session_count > 0 %}
You have played a total of {{ session_count }} sessions for a total of {{ total_duration }}. You have played a total of {{ session_count }} sessions.
{% else %} {% else %}
Start by clicking the links at the top. To track playtime, you need to have at least 1 owned game. Start by clicking the links at the top. To track playtime, you need to have at least 1 owned game.
{% endif %} {% endif %}

View File

@ -5,8 +5,7 @@ from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm
from datetime import datetime from datetime import datetime
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from django.conf import settings from django.conf import settings
from common.util.time import now as now_with_tz, format_duration from common.util.time import now as now_with_tz
from django.db.models import Sum
def model_counts(request): def model_counts(request):
@ -103,9 +102,4 @@ def add_platform(request):
def index(request): def index(request):
context = {} 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) return render(request, "index.html", context)