From 453b4fd92240a000683f10dda4f97719d61b4d5e Mon Sep 17 00:00:00 2001 From: Lukas Kucharczyk Date: Sun, 11 Aug 2024 17:22:58 +0200 Subject: [PATCH] add manage -> sessions --- .pre-commit-config.yaml | 4 +- games/sessionviews.py | 86 +++++++++++++++++++++++++++++++++++++ games/templates/navbar.html | 2 +- games/urls.py | 4 +- games/views.py | 1 + 5 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 games/sessionviews.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 875403e..5762804 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,10 @@ repos: - repo: https://github.com/psf/black - rev: 24.3.0 + rev: 24.8.0 hooks: - id: black - repo: https://github.com/pycqa/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort name: isort (python) diff --git a/games/sessionviews.py b/games/sessionviews.py new file mode 100644 index 0000000..b86d97d --- /dev/null +++ b/games/sessionviews.py @@ -0,0 +1,86 @@ +from typing import Any + +from django.contrib.auth.decorators import login_required +from django.core.paginator import Paginator +from django.http import HttpRequest, HttpResponse +from django.shortcuts import render +from django.template.loader import render_to_string +from django.urls import reverse + +from common.time import format_duration +from games.models import Session +from games.views import dateformat, datetimeformat, timeformat + + +@login_required +def list_sessions(request: HttpRequest) -> HttpResponse: + context: dict[Any, Any] = {} + page_number = request.GET.get("page", 1) + limit = request.GET.get("limit", 10) + sessions = Session.objects.order_by("-created_at") + page_obj = None + if int(limit) != 0: + paginator = Paginator(sessions, limit) + page_obj = paginator.get_page(page_number) + sessions = page_obj.object_list + + context = { + "title": "Manage sessions", + "page_obj": page_obj or None, + "elided_page_range": ( + page_obj.paginator.get_elided_page_range( + page_number, on_each_side=1, on_ends=1 + ) + if page_obj + else None + ), + "data": { + "columns": [ + "Name", + "Date", + "Duration", + "Duration (manual)", + "Device", + "Created", + "Actions", + ], + "rows": [ + [ + session.purchase.edition.name, + f"{session.timestamp_start.strftime(datetimeformat)}{f" — {session.timestamp_end.strftime(timeformat)}" if session.timestamp_end else ""}", + ( + format_duration(session.duration_calculated, "%2.1H hours") + if session.duration_calculated + else "-" + ), + ( + format_duration(session.duration_manual) + if session.duration_manual + else "-" + ), + session.device, + session.created_at.strftime(dateformat), + render_to_string( + "components/button_group_sm.html", + { + "buttons": [ + { + "href": reverse("edit_session", args=[session.pk]), + "text": "Edit", + }, + { + "href": reverse( + "delete_session", args=[session.pk] + ), + "text": "Delete", + "color": "red", + }, + ] + }, + ), + ] + for session in sessions + ], + }, + } + return render(request, "list_purchases.html", context) diff --git a/games/templates/navbar.html b/games/templates/navbar.html index 41e743a..d33b407 100644 --- a/games/templates/navbar.html +++ b/games/templates/navbar.html @@ -115,7 +115,7 @@ class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Purchases
  • - Sessions
  • diff --git a/games/urls.py b/games/urls.py index 7bbb60f..e1df871 100644 --- a/games/urls.py +++ b/games/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from games import purchaseviews, views +from games import purchaseviews, sessionviews, views urlpatterns = [ path("", views.index, name="index"), @@ -76,7 +76,7 @@ urlpatterns = [ {"template": "list_sessions.html#session-row"}, name="list_sessions_end_session", ), - path("session/list", views.list_sessions, name="list_sessions"), + path("session/list", sessionviews.list_sessions, name="list_sessions"), path( "session/list/recent", views.list_sessions, diff --git a/games/views.py b/games/views.py index d4fe44a..87ad738 100644 --- a/games/views.py +++ b/games/views.py @@ -30,6 +30,7 @@ from .models import Edition, Game, Platform, Purchase, PurchaseQueryset, Session dateformat: str = "%d/%m/%Y" datetimeformat: str = "%d/%m/%Y %H:%M" +timeformat = "%H:%M" def model_counts(request: HttpRequest) -> dict[str, bool]: