Compare commits

...

3 Commits

Author SHA1 Message Date
Lukáš Kucharczyk 2d8eb32e90
Remove cruft
Django CI/CD / test (push) Successful in 1m3s Details
Django CI/CD / build-and-push (push) Successful in 1m29s Details
2024-01-10 17:13:59 +01:00
Lukáš Kucharczyk 1f1ed79ee5
Optimize session listing 2024-01-10 16:57:01 +01:00
Lukáš Kucharczyk 01fd7bad69
Remove cruft 2024-01-10 15:55:08 +01:00
3 changed files with 35 additions and 23 deletions

View File

@ -3,7 +3,9 @@
## Improved ## Improved
* game overview: improve how editions and purchases are displayed * game overview: improve how editions and purchases are displayed
* add purchase: only allow choosing purchases of selected edition * add purchase: only allow choosing purchases of selected edition
* session list: starting and ending sessions is much faster/doest not reload the page * session list:
* starting and ending sessions is much faster/doest not reload the page
* listing sessions is much faster
## 1.5.1 / 2023-11-14 21:10+01:00 ## 1.5.1 / 2023-11-14 21:10+01:00

View File

@ -4,7 +4,7 @@
{{ title }} {{ title }}
{% endblock title %} {% endblock title %}
{% block content %} {% block content %}
{% if dataset.count >= 1 %} {% if dataset_count >= 1 %}
<div class="mx-auto text-center my-4"> <div class="mx-auto text-center my-4">
<a id="last-session-start" <a id="last-session-start"
href="{% url 'start_session_same_as_last' last.id %}" href="{% url 'start_session_same_as_last' last.id %}"
@ -17,7 +17,7 @@
</a> </a>
</div> </div>
{% endif %} {% endif %}
{% if dataset.count != 0 %} {% if dataset_count != 0 %}
<table class="responsive-table"> <table class="responsive-table">
<thead> <thead>
<tr> <tr>

View File

@ -1,9 +1,17 @@
from datetime import datetime, timedelta from datetime import datetime
from typing import Any, Callable from typing import Any, Callable
from django.core.exceptions import ObjectDoesNotExist from django.db.models import (
from django.db.models import Avg, Count, ExpressionWrapper, F, Prefetch, Q, Sum, fields Avg,
from django.db.models.functions import Extract, TruncDate Count,
ExpressionWrapper,
F,
Prefetch,
Q,
Sum,
fields,
)
from django.db.models.functions import TruncDate
from django.http import ( from django.http import (
HttpRequest, HttpRequest,
HttpResponse, HttpResponse,
@ -277,38 +285,40 @@ def list_sessions(
context = {} context = {}
context["title"] = "Sessions" context["title"] = "Sessions"
all_sessions = Session.objects.prefetch_related(
"purchase", "purchase__edition", "purchase__edition__game"
).order_by("-timestamp_start")
if filter == "purchase": if filter == "purchase":
dataset = Session.objects.filter(purchase=purchase_id) dataset = all_sessions.filter(purchase=purchase_id)
context["purchase"] = Purchase.objects.get(id=purchase_id) context["purchase"] = Purchase.objects.get(id=purchase_id)
elif filter == "platform": elif filter == "platform":
dataset = Session.objects.filter(purchase__platform=platform_id) dataset = all_sessions.filter(purchase__platform=platform_id)
context["platform"] = Platform.objects.get(id=platform_id) context["platform"] = Platform.objects.get(id=platform_id)
elif filter == "edition": elif filter == "edition":
dataset = Session.objects.filter(purchase__edition=edition_id) dataset = all_sessions.filter(purchase__edition=edition_id)
context["edition"] = Edition.objects.get(id=edition_id) context["edition"] = Edition.objects.get(id=edition_id)
elif filter == "game": elif filter == "game":
dataset = Session.objects.filter(purchase__edition__game=game_id) dataset = all_sessions.filter(purchase__edition__game=game_id)
context["game"] = Game.objects.get(id=game_id) context["game"] = Game.objects.get(id=game_id)
elif filter == "ownership_type": elif filter == "ownership_type":
dataset = Session.objects.filter(purchase__ownership_type=ownership_type) dataset = all_sessions.filter(purchase__ownership_type=ownership_type)
context["ownership_type"] = dict(Purchase.OWNERSHIP_TYPES)[ownership_type] context["ownership_type"] = dict(Purchase.OWNERSHIP_TYPES)[ownership_type]
elif filter == "recent": elif filter == "recent":
current_year = timezone.now().year current_year = timezone.now().year
first_day_of_year = timezone.make_aware(datetime(current_year, 1, 1)) first_day_of_year = timezone.make_aware(datetime(current_year, 1, 1))
dataset = Session.objects.filter( dataset = all_sessions.filter(timestamp_start__gte=first_day_of_year).order_by(
timestamp_start__gte=first_day_of_year "-timestamp_start"
).order_by("-timestamp_start") )
context["title"] = "This year" context["title"] = "This year"
else: else:
# by default, sort from newest to oldest dataset = all_sessions
dataset = Session.objects.order_by("-timestamp_start")
context["total_duration"] = dataset.total_duration_formatted() context = {
context["dataset"] = dataset "dataset": dataset,
try: "dataset_count": dataset.count(),
context["last"] = Session.objects.latest() "last": Session.objects.prefetch_related("purchase__platform").latest(),
except ObjectDoesNotExist: }
context["last"] = None
return render(request, "list_sessions.html", context) return render(request, "list_sessions.html", context)