timetracker/src/web/tracker/views.py

118 lines
3.3 KiB
Python
Raw Normal View History

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
from datetime import datetime, timedelta
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
import logging
2023-01-04 16:27:54 +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 = {}
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()
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
def update_session(request, session_id=None):
session = Session.objects.get(id=session_id)
session.finish_now()
session.save()
return redirect("list_sessions")
def start_session(request, purchase_id=None):
session = SessionForm({"purchase": purchase_id, "timestamp_start": now_with_tz()})
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:
2023-01-05 22:30:13 +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
context["total_duration"] = dataset.total_duration()
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()
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()
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()
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 = {}
context["total_duration"] = Session().duration_sum
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)