8 Commits

Author SHA1 Message Date
465d958d9b Start sessions of last purchase from list
All checks were successful
continuous-integration/drone/push Build is passing
Fixes #19
2023-01-13 16:54:24 +01:00
d8ece979a8 Revert make dev to plain runserver
The runserver_plus has problems with cache not being invalidated
2023-01-13 16:52:05 +01:00
2defdd4657 List number of sessions when filtering on session list
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-10 20:47:33 +01:00
078f87687f Make format_duration more robust
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-09 22:48:09 +01:00
49723831e9 Fix displaying finish button
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-09 22:05:12 +01:00
025ea0dd4e Fix migration
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-09 19:09:31 +01:00
97467c7a52 Also set duration_manual to zero
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-09 19:05:47 +01:00
7842d6f45d Remove debugging statement 2023-01-09 19:00:03 +01:00
11 changed files with 113 additions and 14 deletions

View File

@ -1,6 +1,12 @@
## Unreleased ## 0.2.1 / 2023-01-13 16:53+01:00
* List number of sessions when filtering on session list
* Start sessions of last purchase from list (https://git.kucharczyk.xyz/lukas/timetracker/issues/19)
## 0.2.0 / 2023-01-09 22:42+01:00
* Show playtime total on session list (https://git.kucharczyk.xyz/lukas/timetracker/issues/6) * Show playtime total on session list (https://git.kucharczyk.xyz/lukas/timetracker/issues/6)
* Make formatting durations more robust, change default duration display to "X hours" (https://git.kucharczyk.xyz/lukas/timetracker/issues/26)
## 0.1.4 / 2023-01-08 15:45+01:00 ## 0.1.4 / 2023-01-08 15:45+01:00

View File

@ -6,7 +6,7 @@ RUN npm install && \
FROM python:3.10.9-alpine FROM python:3.10.9-alpine
ENV VERSION_NUMBER 0.1.2-7-g24f4459 ENV VERSION_NUMBER 0.2.0-2-gd8ece97
ENV PROD 1 ENV PROD 1
RUN apk add \ RUN apk add \

View File

@ -20,7 +20,7 @@ migrate: makemigrations
poetry run python src/web/manage.py migrate poetry run python src/web/manage.py migrate
dev: migrate sethookdir dev: migrate sethookdir
poetry run python src/web/manage.py runserver_plus poetry run python src/web/manage.py runserver
caddy: caddy:
caddy run --watch caddy run --watch

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "timetracker" name = "timetracker"
version = "0.1.2" version = "0.2.0"
description = "A simple time tracker." description = "A simple time tracker."
authors = ["Lukáš Kucharczyk <lukas@kucharczyk.xyz>"] authors = ["Lukáš Kucharczyk <lukas@kucharczyk.xyz>"]
license = "GPL" license = "GPL"

View File

@ -18,7 +18,7 @@ def _safe_timedelta(duration: timedelta | int | None):
def format_duration( def format_duration(
duration: timedelta | int | None, format_string: str = "%H hours %m minutes" duration: timedelta | int | None, format_string: str = "%H hours"
) -> str: ) -> str:
""" """
Format timedelta into the specified format_string. Format timedelta into the specified format_string.
@ -27,6 +27,10 @@ def format_duration(
- %m minutes - %m minutes
- %s seconds - %s seconds
- %r total seconds - %r total seconds
Values don't change into higher units if those units are missing
from the formatting string. For example:
- 61 seconds as "%s" = 61 seconds
- 61 seconds as "%m %s" = 1 minutes 1 seconds"
""" """
minute_seconds = 60 minute_seconds = 60
hour_seconds = 60 * minute_seconds hour_seconds = 60 * minute_seconds
@ -37,8 +41,13 @@ def format_duration(
# timestamps where end is before start # timestamps where end is before start
if seconds_total < 0: if seconds_total < 0:
seconds_total = 0 seconds_total = 0
days = hours = minutes = seconds = 0
remainder = seconds = seconds_total
if "%d" in format_string:
days, remainder = divmod(seconds_total, day_seconds) days, remainder = divmod(seconds_total, day_seconds)
if "%H" in format_string:
hours, remainder = divmod(remainder, hour_seconds) hours, remainder = divmod(remainder, hour_seconds)
if "%m" in format_string:
minutes, seconds = divmod(remainder, minute_seconds) minutes, seconds = divmod(remainder, minute_seconds)
literals = { literals = {
"%d": str(days), "%d": str(days),

View File

@ -0,0 +1,34 @@
# Generated by Django 4.1.5 on 2023-01-09 18:04
from django.db import migrations
from datetime import timedelta
def set_duration_manual_none_to_zero(apps, schema_editor):
Session = apps.get_model("tracker", "Session")
for session in Session.objects.all():
if session.duration_manual == None:
session.duration_manual = timedelta(0)
session.save()
def revert_set_duration_manual_none_to_zero(apps, schema_editor):
Session = apps.get_model("tracker", "Session")
for session in Session.objects.all():
if session.duration_manual == timedelta(0):
session.duration_manual = None
session.save()
class Migration(migrations.Migration):
dependencies = [
("tracker", "0005_auto_20230109_1843"),
]
operations = [
migrations.RunPython(
set_duration_manual_none_to_zero,
revert_set_duration_manual_none_to_zero,
)
]

View File

@ -4,6 +4,8 @@ from django.conf import settings
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from common.util.time import format_duration from common.util.time import format_duration
from django.db.models import Sum, F from django.db.models import Sum, F
from django.db.models import Manager
from typing import Any
class Game(models.Model): class Game(models.Model):
@ -57,6 +59,9 @@ class Session(models.Model):
def finish_now(self): def finish_now(self):
self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE)) self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
def start_now():
self.timestamp_start = datetime.now(ZoneInfo(settings.TIME_ZONE))
def duration_seconds(self) -> timedelta: def duration_seconds(self) -> timedelta:
manual = timedelta(0) manual = timedelta(0)
calculated = timedelta(0) calculated = timedelta(0)
@ -74,6 +79,10 @@ class Session(models.Model):
def duration_sum(self) -> str: def duration_sum(self) -> str:
return Session.objects.all().total_duration() return Session.objects.all().total_duration()
@property
def last(self) -> Manager[Any]:
return Session.objects.all().order_by("timestamp_start")[:-1]
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.timestamp_start != None and self.timestamp_end != None: if self.timestamp_start != None and self.timestamp_end != None:
self.duration_calculated = self.timestamp_end - self.timestamp_start self.duration_calculated = self.timestamp_end - self.timestamp_start

View File

@ -3,12 +3,18 @@
{% block title %}Sessions{% endblock title %} {% block title %}Sessions{% endblock title %}
{% block content %} {% block content %}
{% if purchase %}
<div class="text-center text-xl mb-4 dark:text-slate-400"> <div class="text-center text-xl mb-4 dark:text-slate-400">
<h1>Listing sessions only for purchaseee "{{ purchase }}" (total playtime: {{ total_duration }})</h1> <a href="{% url 'start_session' dataset.last.purchase.id %}">
<button type="button" title="Track last tracked" class="py-1 px-2 bg-green-600 hover:bg-green-700 focus:ring-green-500 focus:ring-offset-blue-200 text-white transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 w-12 h-6 rounded-lg ">
New session of {{ dataset.last.purchase }}
</button>
</a>
{% if purchase %}
<h1>Listing sessions only for purchase "{{ purchase }}"</h1>
<h2>Total playtime: {{ total_duration }} over {{ dataset.count }} sessions.</h2>
<a class="dark:text-white hover:underline" href="{% url 'list_sessions' %}">View all sessions</a> <a class="dark:text-white hover:underline" href="{% url 'list_sessions' %}">View all sessions</a>
</div>
{% endif %} {% endif %}
</div>
<div id="session-table" class="gap-4 shadow rounded-xl max-w-screen-lg mx-auto dark:bg-slate-700 p-2 justify-center"> <div id="session-table" class="gap-4 shadow rounded-xl max-w-screen-lg mx-auto dark:bg-slate-700 p-2 justify-center">
<div class="dark:border-white dark:text-slate-300 text-lg">Name</div> <div class="dark:border-white dark:text-slate-300 text-lg">Name</div>
<div class="dark:border-white dark:text-slate-300 text-lg text-center">Start</div> <div class="dark:border-white dark:text-slate-300 text-lg text-center">Start</div>

View File

@ -12,6 +12,11 @@ urlpatterns = [
views.update_session, views.update_session,
name="update_session", name="update_session",
), ),
path(
"start-session/<int:purchase_id>",
views.start_session,
name="start_session",
),
path( path(
"delete_session/by-id/<int:session_id>", "delete_session/by-id/<int:session_id>",
views.delete_session, views.delete_session,

View File

@ -40,6 +40,12 @@ def update_session(request, session_id=None):
return redirect("list_sessions") return redirect("list_sessions")
def start_session(request, purchase_id=None):
session = SessionForm({"purchase": purchase_id, "timestamp_start": now_with_tz()})
session.save()
return redirect("list_sessions")
def delete_session(request, session_id=None): def delete_session(request, session_id=None):
session = Session.objects.get(id=session_id) session = Session.objects.get(id=session_id)
session.delete() session.delete()
@ -56,7 +62,7 @@ def list_sessions(request, purchase_id=None):
dataset = Session.objects.all().order_by("timestamp_start") dataset = Session.objects.all().order_by("timestamp_start")
for session in dataset: for session in dataset:
if session.timestamp_end == None and session.duration_manual == None: if session.timestamp_end == None and session.duration_manual.seconds == 0:
session.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE)) session.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
session.unfinished = True session.unfinished = True

View File

@ -18,15 +18,40 @@ class FormatDurationTest(unittest.TestCase):
result = format_duration(delta, "%H hours") result = format_duration(delta, "%H hours")
self.assertEqual(result, "1 hours") self.assertEqual(result, "1 hours")
def test_overflow_hours(self):
delta = timedelta(hours=25)
result = format_duration(delta, "%H hours")
self.assertEqual(result, "25 hours")
def test_overflow_hours_into_days(self):
delta = timedelta(hours=25)
result = format_duration(delta, "%d days, %H hours")
self.assertEqual(result, "1 days, 1 hours")
def test_only_minutes(self): def test_only_minutes(self):
delta = timedelta(minutes=34) delta = timedelta(minutes=34)
result = format_duration(delta, "%m minutes") result = format_duration(delta, "%m minutes")
self.assertEqual(result, "34 minutes") self.assertEqual(result, "34 minutes")
def test_only_overflow_minutes(self):
delta = timedelta(minutes=61)
result = format_duration(delta, "%m minutes")
self.assertEqual(result, "61 minutes")
def test_overflow_minutes_into_hours(self):
delta = timedelta(minutes=61)
result = format_duration(delta, "%H hours, %m minutes")
self.assertEqual(result, "1 hours, 1 minutes")
def test_only_overflow_seconds(self): def test_only_overflow_seconds(self):
delta = timedelta(seconds=61) delta = timedelta(seconds=61)
result = format_duration(delta, "%s seconds") result = format_duration(delta, "%s seconds")
self.assertEqual(result, "1 seconds") self.assertEqual(result, "61 seconds")
def test_overflow_seconds_into_minutes(self):
delta = timedelta(seconds=61)
result = format_duration(delta, "%m minutes, %s seconds")
self.assertEqual(result, "1 minutes, 1 seconds")
def test_only_rawseconds(self): def test_only_rawseconds(self):
delta = timedelta(seconds=5690) delta = timedelta(seconds=5690)
@ -65,4 +90,3 @@ class FormatDurationTest(unittest.TestCase):
def test_number(self): def test_number(self):
self.assertEqual(format_duration(3600, "%H hour"), "1 hour") self.assertEqual(format_duration(3600, "%H hour"), "1 hour")