From 5172c38c16d8cade454e26a5cc7a1ee704cd6648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 5 Jan 2023 23:03:46 +0100 Subject: [PATCH] Refactor the calculated_sum and manual_sum --- Dockerfile | 2 +- src/web/tracker/models.py | 34 ++++++++++++++++++++++++++++++---- src/web/tracker/views.py | 10 +++++----- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index cdfa39f..4f038d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10-slim-bullseye -ENV VERSION_NUMBER 0.1.0-43-gd00bb1c +ENV VERSION_NUMBER 0.1.0-44-g9c56ed4 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 0f35feb..3cc1363 100644 --- a/src/web/tracker/models.py +++ b/src/web/tracker/models.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from django.conf import settings from zoneinfo import ZoneInfo from common.util.time import format_duration +from django.db.models import Sum class Game(models.Model): @@ -46,7 +47,7 @@ class Session(models.Model): def finish_now(self): self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE)) - def duration_seconds(self): + def duration_seconds(self) -> timedelta: if self.duration_manual == None: if self.timestamp_end == None or self.timestamp_start == None: return timedelta(0) @@ -54,11 +55,10 @@ class Session(models.Model): value = self.timestamp_end - self.timestamp_start else: value = self.duration_manual - return value.total_seconds() + return timedelta(seconds=value.total_seconds()) def duration_formatted(self) -> str: - dur = self.duration_seconds() - result = format_duration(dur, "%H:%m") + result = format_duration(self.duration_seconds(), "%H:%m") return result def duration_any(self): @@ -68,6 +68,32 @@ class Session(models.Model): else self.duration_manual ) + @staticmethod + def calculated_sum() -> timedelta: + calculated_sum_query = Session.objects.all().aggregate( + Sum("duration_calculated") + ) + calculated_sum = ( + timedelta(0) + if calculated_sum_query["duration_calculated__sum"] == None + else calculated_sum_query["duration_calculated__sum"] + ) + return calculated_sum + + @staticmethod + def manual_sum() -> timedelta: + manual_sum_query = Session.objects.all().aggregate(Sum("duration_manual")) + manual_sum = ( + timedelta(0) + if manual_sum_query["duration_manual__sum"] == None + else manual_sum_query["duration_manual__sum"] + ) + return manual_sum + + @staticmethod + def total_sum() -> timedelta: + return Session.manual_sum() + Session.calculated_sum() + def save(self, *args, **kwargs): if self.timestamp_start != None and self.timestamp_end != None: self.duration_calculated = self.timestamp_end - self.timestamp_start diff --git a/src/web/tracker/views.py b/src/web/tracker/views.py index 5f21a75..9db1fd6 100644 --- a/src/web/tracker/views.py +++ b/src/web/tracker/views.py @@ -2,11 +2,12 @@ from django.shortcuts import render, redirect from .models import Game, Platform, Purchase, Session from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm -from datetime import datetime +from datetime import datetime, timedelta from zoneinfo import ZoneInfo from django.conf import settings from common.util.time import now as now_with_tz, format_duration from django.db.models import Sum +import logging def model_counts(request): @@ -107,10 +108,9 @@ def index(request): if Session.objects.count() == 0: duration: str = "" else: - result = Session.objects.all().aggregate(Sum("duration_calculated")) - duration = format_duration( - result["duration_calculated__sum"], "%H hours %m minutes" + context["total_duration"] = format_duration( + Session.total_sum(), + "%H hours %m minutes", ) - context["total_duration"] = duration context["title"] = "Index" return render(request, "index.html", context)