timetracker/src/web/tracker/views.py

106 lines
2.8 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
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
from common.util.time import now as now_with_tz
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
context["form"] = form
return render(request, "add_session.html", context)
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()
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 = {}
return render(request, "index.html", context)