From 6fe960bc04051ecf95cd975005915f47d64f8054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 4 Jan 2023 19:19:49 +0100 Subject: [PATCH] Make the "Finish now?" button on session list work --- CHANGELOG.md | 1 + Dockerfile | 2 +- src/web/common/util/__init__.py | 0 src/web/common/util/time.py | 7 +++++++ src/web/tracker/models.py | 7 ++++++- src/web/tracker/static/base.css | 20 -------------------- src/web/tracker/templates/list_sessions.html | 2 +- src/web/tracker/urls.py | 5 +++++ src/web/tracker/views.py | 14 +++++++++++--- 9 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 src/web/common/util/__init__.py create mode 100644 src/web/common/util/time.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 760e7ef..d7e2acb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Unreleased +* Make the "Finish now?" button on session list work * Hide navigation bar items if there are no games/purchases/sessions * Set default version to "git-main" to indicate development environment * Add homepage, link to it from the logo diff --git a/Dockerfile b/Dockerfile index 2f0905c..5b2f517 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,5 +12,5 @@ COPY entrypoint.sh / RUN chmod +x /entrypoint.sh USER timetracker EXPOSE 8000 -ENV VERSION_NUMBER 0.1.0-4-g166dd71 +ENV VERSION_NUMBER 0.1.0-14-g61d2e65 ENTRYPOINT [ "/entrypoint.sh" ] \ No newline at end of file diff --git a/src/web/common/util/__init__.py b/src/web/common/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/web/common/util/time.py b/src/web/common/util/time.py new file mode 100644 index 0000000..57c8885 --- /dev/null +++ b/src/web/common/util/time.py @@ -0,0 +1,7 @@ +from datetime import datetime +from django.conf import settings +from zoneinfo import ZoneInfo + + +def now(): + return datetime.now(ZoneInfo(settings.TIME_ZONE)) diff --git a/src/web/tracker/models.py b/src/web/tracker/models.py index a6c421f..a129c72 100644 --- a/src/web/tracker/models.py +++ b/src/web/tracker/models.py @@ -1,5 +1,7 @@ from django.db import models -from datetime import timedelta +from datetime import datetime +from django.conf import settings +from zoneinfo import ZoneInfo class Game(models.Model): @@ -40,6 +42,9 @@ class Session(models.Model): mark = ", manual" if self.duration_manual != None else "" return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.duration_any()}{mark})" + def finish_now(self): + self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE)) + def duration_seconds(self): if self.timestamp_end == None or self.timestamp_start == None: if self.duration_manual == None: diff --git a/src/web/tracker/static/base.css b/src/web/tracker/static/base.css index 0e7b594..dfd4ac6 100644 --- a/src/web/tracker/static/base.css +++ b/src/web/tracker/static/base.css @@ -819,11 +819,6 @@ select { border-color: rgb(229 231 235 / var(--tw-border-opacity)); } -.border-red-800 { - --tw-border-opacity: 1; - border-color: rgb(153 27 27 / var(--tw-border-opacity)); -} - .border-red-900 { --tw-border-opacity: 1; border-color: rgb(127 29 29 / var(--tw-border-opacity)); @@ -943,21 +938,6 @@ form input[type=submit] { border-color: rgb(255 255 255 / var(--tw-border-opacity)); } -.hover\:bg-red-600:hover { - --tw-bg-opacity: 1; - background-color: rgb(220 38 38 / var(--tw-bg-opacity)); -} - -.hover\:bg-red-500:hover { - --tw-bg-opacity: 1; - background-color: rgb(239 68 68 / var(--tw-bg-opacity)); -} - -.hover\:bg-yellow-700:hover { - --tw-bg-opacity: 1; - background-color: rgb(161 98 7 / var(--tw-bg-opacity)); -} - .hover\:bg-orange-700:hover { --tw-bg-opacity: 1; background-color: rgb(194 65 12 / var(--tw-bg-opacity)); diff --git a/src/web/tracker/templates/list_sessions.html b/src/web/tracker/templates/list_sessions.html index 3e78c21..270b0de 100644 --- a/src/web/tracker/templates/list_sessions.html +++ b/src/web/tracker/templates/list_sessions.html @@ -19,7 +19,7 @@
{{ data.timestamp_start | date:"d/m/Y H:i" }}
{% if data.unfinished %} - Not finished yet. + Not finished yet. {% elif data.duration_manual %} MANUAL {% else %} diff --git a/src/web/tracker/urls.py b/src/web/tracker/urls.py index 1fb56e8..51af331 100644 --- a/src/web/tracker/urls.py +++ b/src/web/tracker/urls.py @@ -7,6 +7,11 @@ urlpatterns = [ path("add-game/", views.add_game, name="add_game"), path("add-platform/", views.add_platform, name="add_platform"), path("add-session/", views.add_session, name="add_session"), + path( + "update-session/by-session/", + views.update_session, + name="update_session", + ), path("add-purchase/", views.add_purchase, name="add_purchase"), path("list-sessions/", views.list_sessions, name="list_sessions"), path( diff --git a/src/web/tracker/views.py b/src/web/tracker/views.py index 41e3c27..491dec4 100644 --- a/src/web/tracker/views.py +++ b/src/web/tracker/views.py @@ -1,10 +1,11 @@ -from django.shortcuts import render +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 zoneinfo import ZoneInfo from django.conf import settings +from common.util.time import now as now_with_tz def model_counts(request): @@ -18,8 +19,8 @@ def model_counts(request): def add_session(request): context = {} - now = datetime.now() - initial = {"timestamp_start": now, "timestamp_end": now} + now = now_with_tz() + initial = {"timestamp_start": now} form = SessionForm(request.POST or None, initial=initial) if form.is_valid(): form.save() @@ -28,6 +29,13 @@ def add_session(request): return render(request, "add_session.html", context) +def update_session(request, session_id=None): + session = Session.objects.get(id=session_id) + session.finish_now() + session.save() + return redirect("list_sessions") + + def list_sessions(request, purchase_id=None): context = {}