Improve duration handling in Session model

This commit is contained in:
Lukáš Kucharczyk 2023-01-05 22:01:15 +01:00
parent 03e89a92c7
commit f7ec07994f
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
2 changed files with 8 additions and 15 deletions

View File

@ -6,7 +6,7 @@ RUN npm install && \
FROM python:3.10-slim-bullseye 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 ENV PROD 1
RUN useradd --create-home --uid 1000 timetracker RUN useradd --create-home --uid 1000 timetracker

View File

@ -1,7 +1,8 @@
from django.db import models from django.db import models
from datetime import datetime from datetime import datetime, timedelta
from django.conf import settings from django.conf import settings
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from common.util.time import format_duration
class Game(models.Model): class Game(models.Model):
@ -48,25 +49,17 @@ class Session(models.Model):
def duration_seconds(self): def duration_seconds(self):
if self.duration_manual == None: if self.duration_manual == None:
if self.timestamp_end == None or self.timestamp_start == None: if self.timestamp_end == None or self.timestamp_start == None:
return 0 return timedelta(0)
else: else:
value = self.timestamp_end - self.timestamp_start value = self.timestamp_end - self.timestamp_start
else: else:
value = self.duration_manual value = self.duration_manual
return value.total_seconds() return value.total_seconds()
def duration_formatted(self): def duration_formatted(self) -> str:
seconds = self.duration_seconds() dur = self.duration_seconds()
if seconds == 0: result = format_duration(dur, "%H:%m")
return seconds return result
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_any(self): def duration_any(self):
return ( return (