Filtering sessions by purchase

This commit is contained in:
Lukáš Kucharczyk 2023-01-03 19:03:30 +01:00
parent cd35af471a
commit aae05f23e7
4 changed files with 40 additions and 21 deletions

View File

@ -6,7 +6,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}No Title{% endblock title %}</title> <title>Timetracker - {% block title %}Untitled{% endblock title %}</title>
<link rel="stylesheet" href="https://rsms.me/inter/inter.css"> <link rel="stylesheet" href="https://rsms.me/inter/inter.css">
<link rel="stylesheet" href="{% static 'base.css' %}" /> <link rel="stylesheet" href="{% static 'base.css' %}" />
</head> </head>

View File

@ -1,15 +1,21 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block title %}Tracker Entry List{% 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">
<h1>Listing sessions only for purchase "{{ purchase }}"</h1>
<a class="dark:text-white hover:underline" href="{% url 'list_sessions' %}">View all sessions</a>
</div>
{% endif %}
<div class="grid grid-cols-4 gap-4 shadow rounded-xl max-w-screen-lg mx-auto dark:bg-slate-700 p-2 justify-center"> <div class="grid grid-cols-4 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">Start</div> <div class="dark:border-white dark:text-slate-300 text-lg">Start</div>
<div class="dark:border-white dark:text-slate-300 text-lg">End</div> <div class="dark:border-white dark:text-slate-300 text-lg">End</div>
<div class="dark:border-white dark:text-slate-300 text-lg">Duration</div> <div class="dark:border-white dark:text-slate-300 text-lg">Duration</div>
{% for data in dataset %} {% for data in dataset %}
<div class="dark:text-slate-400">{{ data.purchase }}</div> <div class=""><a class="dark:text-white hover:underline" href="{% url 'list_sessions' data.purchase.id %}">{{ data.purchase }}</a></div>
<div class="dark:text-slate-400">{{ data.timestamp_start }}</div> <div class="dark:text-slate-400">{{ data.timestamp_start }}</div>
<div class="dark:text-slate-400">{{ data.timestamp_end }}</div> <div class="dark:text-slate-400">{{ data.timestamp_end }}</div>
<div class="dark:text-slate-400">{{ data.time_delta }}</div> <div class="dark:text-slate-400">{{ data.time_delta }}</div>

View File

@ -7,4 +7,9 @@ urlpatterns = [
path("add-session/", views.add_session, name="add_session"), path("add-session/", views.add_session, name="add_session"),
path("add-purchase/", views.add_purchase, name="add_purchase"), path("add-purchase/", views.add_purchase, name="add_purchase"),
path("list-sessions/", views.list_sessions, name="list_sessions"), path("list-sessions/", views.list_sessions, name="list_sessions"),
path(
"list-sessions/by-purchase/<int:purchase_id>",
views.list_sessions,
name="list_sessions",
),
] ]

View File

@ -4,6 +4,7 @@ from .models import Game, Platform, Purchase, Session
from .forms import SessionForm, PurchaseForm, GameForm from .forms import SessionForm, PurchaseForm, GameForm
from datetime import datetime from datetime import datetime
from django.db.models import ExpressionWrapper, F, DurationField from django.db.models import ExpressionWrapper, F, DurationField
import logging
def add_session(request): def add_session(request):
@ -18,9 +19,16 @@ def add_session(request):
return render(request, "add_session.html", context) return render(request, "add_session.html", context)
def list_sessions(request): def list_sessions(request, purchase_id=None):
context = {} 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( time_delta=ExpressionWrapper(
F("timestamp_end") - F("timestamp_start"), output_field=DurationField() F("timestamp_end") - F("timestamp_start"), output_field=DurationField()
) )