Remove cruft
Django CI/CD / test (push) Successful in 1m3s Details
Django CI/CD / build-and-push (push) Successful in 1m29s Details

This commit is contained in:
Lukáš Kucharczyk 2024-01-10 17:13:59 +01:00
parent 1f1ed79ee5
commit 2d8eb32e90
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
1 changed files with 26 additions and 19 deletions

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,41 +285,40 @@ def list_sessions(
context = {} context = {}
context["title"] = "Sessions" context["title"] = "Sessions"
dataset = Session.objects.prefetch_related( all_sessions = Session.objects.prefetch_related(
"purchase", "purchase__edition", "purchase__edition__game" "purchase", "purchase__edition", "purchase__edition__game"
).order_by("-timestamp_start") ).order_by("-timestamp_start")
if filter == "purchase": if filter == "purchase":
dataset = dataset.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 = dataset.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 = dataset.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 = dataset.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 = dataset.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 = dataset.filter(timestamp_start__gte=first_day_of_year).order_by( dataset = all_sessions.filter(timestamp_start__gte=first_day_of_year).order_by(
"-timestamp_start" "-timestamp_start"
) )
context["title"] = "This year" context["title"] = "This year"
else:
dataset = all_sessions
context["dataset"] = dataset context = {
context["dataset_count"] = dataset.count() "dataset": dataset,
try: "dataset_count": dataset.count(),
context["last"] = Session.objects.prefetch_related( "last": Session.objects.prefetch_related("purchase__platform").latest(),
"purchase__platform" }
).latest()
except ObjectDoesNotExist:
context["last"] = None
return render(request, "list_sessions.html", context) return render(request, "list_sessions.html", context)