2023-01-04 18:19:49 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
from .models import Game, Platform, Purchase, Session
|
2023-01-04 16:23:34 +00:00
|
|
|
from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm
|
2022-12-31 13:18:27 +00:00
|
|
|
from datetime import datetime
|
2023-01-04 16:27:54 +00:00
|
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from django.conf import settings
|
2023-01-05 10:24:07 +00:00
|
|
|
from common.util.time import now as now_with_tz, format_duration
|
|
|
|
from django.db.models import Sum
|
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,
|
|
|
|
"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-04 18:19:49 +00:00
|
|
|
now = now_with_tz()
|
|
|
|
initial = {"timestamp_start": now}
|
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
|
|
|
|
|
|
|
context["form"] = form
|
|
|
|
return render(request, "add_session.html", context)
|
|
|
|
|
|
|
|
|
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-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-01-03 18:03:30 +00:00
|
|
|
def list_sessions(request, purchase_id=None):
|
2022-12-31 13:18:27 +00:00
|
|
|
context = {}
|
2023-01-03 18:03:30 +00:00
|
|
|
|
|
|
|
if purchase_id != None:
|
|
|
|
dataset = Session.objects.filter(purchase=purchase_id)
|
|
|
|
context["purchase"] = Purchase.objects.get(id=purchase_id)
|
|
|
|
else:
|
|
|
|
dataset = Session.objects.all()
|
|
|
|
|
2023-01-04 16:27:54 +00:00
|
|
|
for session in dataset:
|
|
|
|
if session.timestamp_end == None and session.duration_manual == None:
|
|
|
|
session.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
|
|
|
|
session.unfinished = True
|
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
context["dataset"] = dataset
|
|
|
|
|
|
|
|
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-01-04 18:35:35 +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-01-04 18:35:35 +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
|
|
|
|
|
|
|
def add_platform(request):
|
|
|
|
context = {}
|
|
|
|
form = PlatformForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2023-01-04 18:35:35 +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-01-04 16:22:36 +00:00
|
|
|
def index(request):
|
|
|
|
context = {}
|
2023-01-05 15:33:32 +00:00
|
|
|
if Session.objects.count() == 0:
|
|
|
|
duration_value = 0
|
|
|
|
else:
|
|
|
|
result = Session.objects.all().aggregate(Sum("duration_calculated"))
|
|
|
|
context["total_duration"] = format_duration(
|
|
|
|
result["duration_calculated__sum"], "%H hours %m minutes"
|
|
|
|
)
|
|
|
|
context["total_duration"] = duration_value
|
2023-01-05 10:24:07 +00:00
|
|
|
context["title"] = "Index"
|
2023-01-04 16:22:36 +00:00
|
|
|
return render(request, "index.html", context)
|