diff --git a/CHANGELOG.md b/CHANGELOG.md index 82f52b7..6e141c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,3 +4,4 @@ * Add homepage, link to it from the logo * Make it possible to add a new platform * Save calculated duration to database if both timestamps are set +* Improve session listing \ No newline at end of file diff --git a/src/web/tracker/models.py b/src/web/tracker/models.py index 50db664..a6c421f 100644 --- a/src/web/tracker/models.py +++ b/src/web/tracker/models.py @@ -38,19 +38,33 @@ class Session(models.Model): def __str__(self): mark = ", manual" if self.duration_manual != None else "" - return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.total_duration()}{mark})" + return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.duration_any()}{mark})" - def calculated_duration(self): + def duration_seconds(self): if self.timestamp_end == None or self.timestamp_start == None: - return 0 + if self.duration_manual == None: + return 0 + else: + value = self.duration_manual else: - return self.timestamp_end - self.timestamp_start + value = self.timestamp_end - self.timestamp_start + return value.total_seconds() - def total_duration(self): + def duration_formatted(self): + seconds = self.duration_seconds() + if seconds == 0: + return seconds + hours, remainder = divmod(seconds, 3600) + minutes = remainder % 60 + 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): return ( - self.calculated_duration() + self.duration_formatted() if self.duration_manual == None - else self.duration_manual + self.calculated_duration() + else self.duration_manual ) def save(self, *args, **kwargs): diff --git a/src/web/tracker/templates/list_sessions.html b/src/web/tracker/templates/list_sessions.html index 672c6e8..82c61b1 100644 --- a/src/web/tracker/templates/list_sessions.html +++ b/src/web/tracker/templates/list_sessions.html @@ -17,8 +17,17 @@ {% for data in dataset %}
{{ data.purchase }}
{{ data.timestamp_start | date:"d/m/Y H:i" }}
-
{{ data.timestamp_end | date:"d/m/Y H:i" }}
-
{{ data.time_delta }}
+
+ {% if data.unfinished %} + Not finished yet. + {% elif data.duration_manual %} + MANUAL + {% else %} + {{ data.timestamp_end | date:"d/m/Y H:i" }} + {% endif %} +
+ {% load time %} +
{{ data.duration_formatted }}{% if data.duration_manual %} (M){% endif %}
{% endfor %} {% endblock content %} \ No newline at end of file diff --git a/src/web/tracker/views.py b/src/web/tracker/views.py index 8ea4e86..41e3c27 100644 --- a/src/web/tracker/views.py +++ b/src/web/tracker/views.py @@ -3,6 +3,10 @@ from django.shortcuts import render from .models import Game, Platform, Purchase, Session from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm from datetime import datetime +from zoneinfo import ZoneInfo +from django.conf import settings + + def model_counts(request): return { "game_available": Game.objects.count() != 0, @@ -33,11 +37,11 @@ def list_sessions(request, purchase_id=None): else: dataset = Session.objects.all() - dataset = dataset.annotate( - time_delta=ExpressionWrapper( - F("timestamp_end") - F("timestamp_start"), output_field=DurationField() - ) - ) + for session in dataset: + if session.timestamp_end == None and session.duration_manual == None: + session.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE)) + session.unfinished = True + context["dataset"] = dataset return render(request, "list_sessions.html", context)