Compare commits

..

No commits in common. "2d8eb32e90e8807804418dd4532edf8072a60695" and "44f49e597483a34b9a8792841254505987260949" have entirely different histories.

3 changed files with 23 additions and 35 deletions

View File

@ -3,9 +3,7 @@
## 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: * session list: starting and ending sessions is much faster/doest not reload the page
* 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,17 +1,9 @@
from datetime import datetime from datetime import datetime, timedelta
from typing import Any, Callable from typing import Any, Callable
from django.db.models import ( from django.core.exceptions import ObjectDoesNotExist
Avg, from django.db.models import Avg, Count, ExpressionWrapper, F, Prefetch, Q, Sum, fields
Count, from django.db.models.functions import Extract, TruncDate
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,
@ -285,40 +277,38 @@ 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 = all_sessions.filter(purchase=purchase_id) dataset = Session.objects.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 = all_sessions.filter(purchase__platform=platform_id) dataset = Session.objects.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 = all_sessions.filter(purchase__edition=edition_id) dataset = Session.objects.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 = all_sessions.filter(purchase__edition__game=game_id) dataset = Session.objects.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 = all_sessions.filter(purchase__ownership_type=ownership_type) dataset = Session.objects.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 = all_sessions.filter(timestamp_start__gte=first_day_of_year).order_by( dataset = Session.objects.filter(
"-timestamp_start" timestamp_start__gte=first_day_of_year
) ).order_by("-timestamp_start")
context["title"] = "This year" context["title"] = "This year"
else: else:
dataset = all_sessions # by default, sort from newest to oldest
dataset = Session.objects.order_by("-timestamp_start")
context = { context["total_duration"] = dataset.total_duration_formatted()
"dataset": dataset, context["dataset"] = dataset
"dataset_count": dataset.count(), try:
"last": Session.objects.prefetch_related("purchase__platform").latest(), context["last"] = Session.objects.latest()
} except ObjectDoesNotExist:
context["last"] = None
return render(request, "list_sessions.html", context) return render(request, "list_sessions.html", context)