From f7ec07994f42ebba96172a89dbfcefedce9d2629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 5 Jan 2023 22:01:15 +0100 Subject: [PATCH] Improve duration handling in Session model --- Dockerfile | 2 +- src/web/tracker/models.py | 21 +++++++-------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7b3e4d6..37b3b98 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10-slim-bullseye -ENV VERSION_NUMBER 0.1.0-37-g76bf03b +ENV VERSION_NUMBER 0.1.0-38-g03e89a9 ENV PROD 1 RUN useradd --create-home --uid 1000 timetracker diff --git a/src/web/tracker/models.py b/src/web/tracker/models.py index 397a951..0f35feb 100644 --- a/src/web/tracker/models.py +++ b/src/web/tracker/models.py @@ -1,7 +1,8 @@ from django.db import models -from datetime import datetime +from datetime import datetime, timedelta from django.conf import settings from zoneinfo import ZoneInfo +from common.util.time import format_duration class Game(models.Model): @@ -48,25 +49,17 @@ class Session(models.Model): def duration_seconds(self): if self.duration_manual == None: if self.timestamp_end == None or self.timestamp_start == None: - return 0 + return timedelta(0) else: value = self.timestamp_end - self.timestamp_start else: value = self.duration_manual return value.total_seconds() - def duration_formatted(self): - seconds = self.duration_seconds() - if seconds == 0: - return seconds - hours, remainder = divmod(seconds, 3600) - minutes = remainder // 60 - if hours == 0 and minutes == 0: - return "less than a minute" - else: - hour_string = f"{int(hours)}h" if hours != 0 else "" - minute_string = f"{int(minutes)}m" if minutes != 0 else "" - return f"{hour_string}{minute_string}" + def duration_formatted(self) -> str: + dur = self.duration_seconds() + result = format_duration(dur, "%H:%m") + return result def duration_any(self): return (