add manage -> sessions

This commit is contained in:
Lukáš Kucharczyk 2024-08-11 17:22:58 +02:00
parent bb0d24809e
commit 453b4fd922
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
5 changed files with 92 additions and 5 deletions

View File

@ -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)

86
games/sessionviews.py Normal file
View File

@ -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)

View File

@ -115,7 +115,7 @@
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Purchases</a>
</li>
<li>
<a href="{% url 'add_session' %}"
<a href="{% url 'list_sessions' %}"
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Sessions</a>
</li>
</ul>

View File

@ -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,

View File

@ -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]: