2023-01-30 21:16:28 +00:00
|
|
|
from datetime import datetime, timedelta
|
2023-01-04 16:27:54 +00:00
|
|
|
from zoneinfo import ZoneInfo
|
2023-01-15 22:39:52 +00:00
|
|
|
|
2023-01-20 13:37:46 +00:00
|
|
|
from common.plots import playtime_over_time_chart
|
|
|
|
from common.time import now as now_with_tz
|
2023-01-15 22:39:52 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
|
2023-02-18 20:12:18 +00:00
|
|
|
from .forms import (
|
|
|
|
GameForm,
|
|
|
|
PlatformForm,
|
|
|
|
PurchaseForm,
|
|
|
|
SessionForm,
|
|
|
|
EditionForm,
|
|
|
|
DeviceForm,
|
|
|
|
)
|
2023-02-18 19:49:46 +00:00
|
|
|
from .models import Game, Platform, Purchase, Session, Edition
|
2023-01-04 16:27:54 +00:00
|
|
|
|
|
|
|
|
2023-01-04 16:19:40 +00:00
|
|
|
def model_counts(request):
|
|
|
|
return {
|
|
|
|
"game_available": Game.objects.count() != 0,
|
2023-02-18 19:49:46 +00:00
|
|
|
"edition_available": Edition.objects.count() != 0,
|
2023-01-04 16:19:40 +00:00
|
|
|
"platform_available": Platform.objects.count() != 0,
|
|
|
|
"purchase_available": Purchase.objects.count() != 0,
|
|
|
|
"session_count": Session.objects.count(),
|
|
|
|
}
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_session(request):
|
|
|
|
context = {}
|
2023-01-19 19:35:25 +00:00
|
|
|
initial = {}
|
|
|
|
|
2023-01-04 18:19:49 +00:00
|
|
|
now = now_with_tz()
|
2023-01-19 19:35:25 +00:00
|
|
|
initial["timestamp_start"] = now
|
|
|
|
|
2023-01-16 20:19:20 +00:00
|
|
|
last = Session.objects.all().last()
|
2023-01-19 19:35:25 +00:00
|
|
|
if last != None:
|
|
|
|
initial["purchase"] = last.purchase
|
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
form = SessionForm(request.POST or None, initial=initial)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2023-01-04 18:35:35 +00:00
|
|
|
return redirect("list_sessions")
|
2022-12-31 13:18:27 +00:00
|
|
|
|
2023-01-05 21:09:21 +00:00
|
|
|
context["title"] = "Add New Session"
|
2022-12-31 13:18:27 +00:00
|
|
|
context["form"] = form
|
2023-01-05 21:09:21 +00:00
|
|
|
return render(request, "add.html", context)
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
|
2023-01-04 18:19:49 +00:00
|
|
|
def update_session(request, session_id=None):
|
|
|
|
session = Session.objects.get(id=session_id)
|
|
|
|
session.finish_now()
|
|
|
|
session.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
|
|
|
|
|
2023-01-30 16:38:44 +00:00
|
|
|
def edit_session(request, session_id=None):
|
|
|
|
context = {}
|
|
|
|
session = Session.objects.get(id=session_id)
|
|
|
|
form = SessionForm(request.POST or None, instance=session)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
context["title"] = "Edit Session"
|
|
|
|
context["form"] = form
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
2023-02-18 20:44:19 +00:00
|
|
|
def edit_purchase(request, purchase_id=None):
|
|
|
|
context = {}
|
|
|
|
purchase = Purchase.objects.get(id=purchase_id)
|
|
|
|
form = PurchaseForm(request.POST or None, instance=purchase)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
context["title"] = "Edit Purchase"
|
|
|
|
context["form"] = form
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
2023-02-20 16:16:19 +00:00
|
|
|
def edit_game(request, game_id=None):
|
|
|
|
context = {}
|
|
|
|
purchase = Game.objects.get(id=game_id)
|
|
|
|
form = GameForm(request.POST or None, instance=purchase)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
context["title"] = "Edit Game"
|
|
|
|
context["form"] = form
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
|
|
|
def edit_platform(request, platform_id=None):
|
|
|
|
context = {}
|
|
|
|
purchase = Platform.objects.get(id=platform_id)
|
|
|
|
form = PlatformForm(request.POST or None, instance=purchase)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
context["title"] = "Edit Platform"
|
|
|
|
context["form"] = form
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
2023-02-18 20:47:25 +00:00
|
|
|
def edit_edition(request, edition_id=None):
|
|
|
|
context = {}
|
|
|
|
edition = Edition.objects.get(id=edition_id)
|
|
|
|
form = EditionForm(request.POST or None, instance=edition)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
context["title"] = "Edit Edition"
|
|
|
|
context["form"] = form
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
2023-02-18 20:43:51 +00:00
|
|
|
def start_session(request, last_session_id: int):
|
|
|
|
last_session = Session.objects.get(id=last_session_id)
|
|
|
|
session = SessionForm(
|
|
|
|
{
|
|
|
|
"purchase": last_session.purchase.id,
|
|
|
|
"timestamp_start": now_with_tz(),
|
|
|
|
"device": last_session.device,
|
|
|
|
}
|
|
|
|
)
|
2023-01-13 15:54:24 +00:00
|
|
|
session.save()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
|
|
|
|
|
2023-01-04 19:28:07 +00:00
|
|
|
def delete_session(request, session_id=None):
|
|
|
|
session = Session.objects.get(id=session_id)
|
|
|
|
session.delete()
|
|
|
|
return redirect("list_sessions")
|
|
|
|
|
|
|
|
|
2023-02-19 13:30:26 +00:00
|
|
|
def list_sessions(
|
|
|
|
request,
|
|
|
|
filter="",
|
|
|
|
purchase_id="",
|
|
|
|
platform_id="",
|
|
|
|
game_id="",
|
|
|
|
edition_id="",
|
|
|
|
ownership_type: str = "",
|
|
|
|
):
|
2022-12-31 13:18:27 +00:00
|
|
|
context = {}
|
2023-01-30 21:16:28 +00:00
|
|
|
context["title"] = "Sessions"
|
2023-01-03 18:03:30 +00:00
|
|
|
|
2023-01-15 22:14:28 +00:00
|
|
|
if filter == "purchase":
|
2023-01-03 18:03:30 +00:00
|
|
|
dataset = Session.objects.filter(purchase=purchase_id)
|
|
|
|
context["purchase"] = Purchase.objects.get(id=purchase_id)
|
2023-01-15 22:14:28 +00:00
|
|
|
elif filter == "platform":
|
|
|
|
dataset = Session.objects.filter(purchase__platform=platform_id)
|
|
|
|
context["platform"] = Platform.objects.get(id=platform_id)
|
2023-02-18 19:49:46 +00:00
|
|
|
elif filter == "edition":
|
|
|
|
dataset = Session.objects.filter(purchase__edition=edition_id)
|
|
|
|
context["edition"] = Edition.objects.get(id=edition_id)
|
2023-02-19 13:30:26 +00:00
|
|
|
elif filter == "game":
|
|
|
|
dataset = Session.objects.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)
|
|
|
|
context["ownership_type"] = dict(Purchase.OWNERSHIP_TYPES)[ownership_type]
|
2023-01-30 21:16:28 +00:00
|
|
|
elif filter == "recent":
|
|
|
|
dataset = Session.objects.filter(
|
|
|
|
timestamp_start__gte=datetime.now() - timedelta(days=30)
|
2023-01-31 15:37:44 +00:00
|
|
|
).order_by("-timestamp_start")
|
2023-01-30 21:16:28 +00:00
|
|
|
context["title"] = "Last 30 days"
|
2023-01-03 18:03:30 +00:00
|
|
|
else:
|
2023-01-18 15:52:40 +00:00
|
|
|
# by default, sort from newest to oldest
|
2023-01-15 22:20:43 +00:00
|
|
|
dataset = Session.objects.all().order_by("-timestamp_start")
|
2023-01-03 18:03:30 +00:00
|
|
|
|
2023-01-04 16:27:54 +00:00
|
|
|
for session in dataset:
|
2023-01-09 21:05:12 +00:00
|
|
|
if session.timestamp_end == None and session.duration_manual.seconds == 0:
|
2023-01-04 16:27:54 +00:00
|
|
|
session.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
|
|
|
|
session.unfinished = True
|
|
|
|
|
2023-01-09 17:57:22 +00:00
|
|
|
context["total_duration"] = dataset.total_duration()
|
2022-12-31 13:18:27 +00:00
|
|
|
context["dataset"] = dataset
|
2023-01-18 15:52:40 +00:00
|
|
|
# cannot use dataset[0] here because that might be only partial QuerySet
|
|
|
|
context["last"] = Session.objects.all().order_by("timestamp_start").last()
|
2023-01-16 16:26:10 +00:00
|
|
|
# charts are always oldest->newest
|
2023-02-19 13:55:06 +00:00
|
|
|
if dataset.count() >= 2:
|
2023-01-20 14:06:42 +00:00
|
|
|
context["chart"] = playtime_over_time_chart(dataset.order_by("timestamp_start"))
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
return render(request, "list_sessions.html", context)
|
|
|
|
|
|
|
|
|
|
|
|
def add_purchase(request):
|
|
|
|
context = {}
|
|
|
|
now = datetime.now()
|
|
|
|
initial = {"date_purchased": now}
|
|
|
|
form = PurchaseForm(request.POST or None, initial=initial)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2023-02-18 19:50:36 +00:00
|
|
|
return redirect("index")
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
context["form"] = form
|
2023-01-03 21:04:36 +00:00
|
|
|
context["title"] = "Add New Purchase"
|
|
|
|
return render(request, "add.html", context)
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_game(request):
|
|
|
|
context = {}
|
|
|
|
form = GameForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2023-02-18 19:50:36 +00:00
|
|
|
return redirect("index")
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
context["form"] = form
|
|
|
|
context["title"] = "Add New Game"
|
|
|
|
return render(request, "add.html", context)
|
2023-01-04 16:22:36 +00:00
|
|
|
|
2023-01-04 16:23:34 +00:00
|
|
|
|
2023-02-18 19:49:46 +00:00
|
|
|
def add_edition(request):
|
|
|
|
context = {}
|
|
|
|
form = EditionForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("index")
|
|
|
|
|
|
|
|
context["form"] = form
|
|
|
|
context["title"] = "Add New Edition"
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
2023-01-04 16:23:34 +00:00
|
|
|
def add_platform(request):
|
|
|
|
context = {}
|
|
|
|
form = PlatformForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2023-02-18 19:50:36 +00:00
|
|
|
return redirect("index")
|
2023-01-04 16:23:34 +00:00
|
|
|
|
|
|
|
context["form"] = form
|
|
|
|
context["title"] = "Add New Platform"
|
|
|
|
return render(request, "add.html", context)
|
2023-02-18 19:50:36 +00:00
|
|
|
|
|
|
|
|
2023-02-18 20:12:18 +00:00
|
|
|
def add_device(request):
|
|
|
|
context = {}
|
|
|
|
form = DeviceForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("index")
|
|
|
|
|
|
|
|
context["form"] = form
|
|
|
|
context["title"] = "Add New Device"
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
2023-02-18 19:50:36 +00:00
|
|
|
def index(request):
|
|
|
|
return redirect("list_sessions_recent")
|