From 1f1ed79ee5c97d6c7d62298ae12c26494be511e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 10 Jan 2024 16:57:01 +0100 Subject: [PATCH] Optimize session listing --- CHANGELOG.md | 4 +++- games/templates/list_sessions.html | 4 ++-- games/views.py | 28 ++++++++++++++++------------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86605b2..29a2e9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ ## Improved * game overview: improve how editions and purchases are displayed * 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 diff --git a/games/templates/list_sessions.html b/games/templates/list_sessions.html index 8d3449e..26b1670 100644 --- a/games/templates/list_sessions.html +++ b/games/templates/list_sessions.html @@ -4,7 +4,7 @@ {{ title }} {% endblock title %} {% block content %} - {% if dataset.count >= 1 %} + {% if dataset_count >= 1 %}
{% endif %} - {% if dataset.count != 0 %} + {% if dataset_count != 0 %} diff --git a/games/views.py b/games/views.py index 25e30ee..be2b8a9 100644 --- a/games/views.py +++ b/games/views.py @@ -277,35 +277,39 @@ def list_sessions( context = {} context["title"] = "Sessions" + dataset = Session.objects.prefetch_related( + "purchase", "purchase__edition", "purchase__edition__game" + ).order_by("-timestamp_start") + if filter == "purchase": - dataset = Session.objects.filter(purchase=purchase_id) + dataset = dataset.filter(purchase=purchase_id) context["purchase"] = Purchase.objects.get(id=purchase_id) elif filter == "platform": - dataset = Session.objects.filter(purchase__platform=platform_id) + dataset = dataset.filter(purchase__platform=platform_id) context["platform"] = Platform.objects.get(id=platform_id) elif filter == "edition": - dataset = Session.objects.filter(purchase__edition=edition_id) + dataset = dataset.filter(purchase__edition=edition_id) context["edition"] = Edition.objects.get(id=edition_id) elif filter == "game": - dataset = Session.objects.filter(purchase__edition__game=game_id) + dataset = dataset.filter(purchase__edition__game=game_id) context["game"] = Game.objects.get(id=game_id) elif filter == "ownership_type": - dataset = Session.objects.filter(purchase__ownership_type=ownership_type) + dataset = dataset.filter(purchase__ownership_type=ownership_type) context["ownership_type"] = dict(Purchase.OWNERSHIP_TYPES)[ownership_type] elif filter == "recent": current_year = timezone.now().year first_day_of_year = timezone.make_aware(datetime(current_year, 1, 1)) - dataset = Session.objects.filter( - timestamp_start__gte=first_day_of_year - ).order_by("-timestamp_start") + dataset = dataset.filter(timestamp_start__gte=first_day_of_year).order_by( + "-timestamp_start" + ) context["title"] = "This year" - else: - # by default, sort from newest to oldest - dataset = Session.objects.order_by("-timestamp_start") context["dataset"] = dataset + context["dataset_count"] = dataset.count() try: - context["last"] = Session.objects.latest() + context["last"] = Session.objects.prefetch_related( + "purchase__platform" + ).latest() except ObjectDoesNotExist: context["last"] = None