From aae05f23e7a5e2aca274bfb25eb90fc77ffc2161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Tue, 3 Jan 2023 19:03:30 +0100 Subject: [PATCH] Filtering sessions by purchase --- src/web/tracker/templates/base.html | 34 ++++++++++---------- src/web/tracker/templates/list_sessions.html | 10 ++++-- src/web/tracker/urls.py | 5 +++ src/web/tracker/views.py | 12 +++++-- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/web/tracker/templates/base.html b/src/web/tracker/templates/base.html index c591f5e..b1be850 100644 --- a/src/web/tracker/templates/base.html +++ b/src/web/tracker/templates/base.html @@ -6,31 +6,31 @@ - {% block title %}No Title{% endblock title %} + Timetracker - {% block title %}Untitled{% endblock title %}
-
- - {% block content %}No content here.{% endblock %} + + {% block content %}No content here.{% endblock %} diff --git a/src/web/tracker/templates/list_sessions.html b/src/web/tracker/templates/list_sessions.html index b09023c..daa8b49 100644 --- a/src/web/tracker/templates/list_sessions.html +++ b/src/web/tracker/templates/list_sessions.html @@ -1,15 +1,21 @@ {% extends 'base.html' %} -{% block title %}Tracker Entry List{% endblock title %} +{% block title %}Sessions{% endblock title %} {% block content %} +{% if purchase %} +
+

Listing sessions only for purchase "{{ purchase }}"

+ View all sessions +
+{% endif %}
Name
Start
End
Duration
{% for data in dataset %} -
{{ data.purchase }}
+
{{ data.purchase }}
{{ data.timestamp_start }}
{{ data.timestamp_end }}
{{ data.time_delta }}
diff --git a/src/web/tracker/urls.py b/src/web/tracker/urls.py index 28c8796..fb8944f 100644 --- a/src/web/tracker/urls.py +++ b/src/web/tracker/urls.py @@ -7,4 +7,9 @@ urlpatterns = [ path("add-session/", views.add_session, name="add_session"), path("add-purchase/", views.add_purchase, name="add_purchase"), path("list-sessions/", views.list_sessions, name="list_sessions"), + path( + "list-sessions/by-purchase/", + views.list_sessions, + name="list_sessions", + ), ] diff --git a/src/web/tracker/views.py b/src/web/tracker/views.py index 1f025ed..54f95f3 100644 --- a/src/web/tracker/views.py +++ b/src/web/tracker/views.py @@ -4,6 +4,7 @@ from .models import Game, Platform, Purchase, Session from .forms import SessionForm, PurchaseForm, GameForm from datetime import datetime from django.db.models import ExpressionWrapper, F, DurationField +import logging def add_session(request): @@ -18,9 +19,16 @@ def add_session(request): return render(request, "add_session.html", context) -def list_sessions(request): +def list_sessions(request, purchase_id=None): context = {} - dataset = Session.objects.annotate( + + if purchase_id != None: + dataset = Session.objects.filter(purchase=purchase_id) + context["purchase"] = Purchase.objects.get(id=purchase_id) + else: + dataset = Session.objects.all() + + dataset = dataset.annotate( time_delta=ExpressionWrapper( F("timestamp_end") - F("timestamp_start"), output_field=DurationField() )