Improve session listing

This commit is contained in:
Lukáš Kucharczyk 2023-01-04 17:27:54 +01:00
parent b3842504af
commit 40810256aa
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
4 changed files with 42 additions and 14 deletions

View File

@ -4,3 +4,4 @@
* Add homepage, link to it from the logo
* Make it possible to add a new platform
* Save calculated duration to database if both timestamps are set
* Improve session listing

View File

@ -38,19 +38,33 @@ class Session(models.Model):
def __str__(self):
mark = ", manual" if self.duration_manual != None else ""
return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.total_duration()}{mark})"
return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.duration_any()}{mark})"
def calculated_duration(self):
def duration_seconds(self):
if self.timestamp_end == None or self.timestamp_start == None:
return 0
if self.duration_manual == None:
return 0
else:
value = self.duration_manual
else:
return self.timestamp_end - self.timestamp_start
value = self.timestamp_end - self.timestamp_start
return value.total_seconds()
def total_duration(self):
def duration_formatted(self):
seconds = self.duration_seconds()
if seconds == 0:
return seconds
hours, remainder = divmod(seconds, 3600)
minutes = remainder % 60
hour_string = f"{int(hours)}h" if hours != 0 else ""
minute_string = f"{int(minutes)}m" if minutes != 0 else ""
return f"{hour_string}{minute_string}"
def duration_any(self):
return (
self.calculated_duration()
self.duration_formatted()
if self.duration_manual == None
else self.duration_manual + self.calculated_duration()
else self.duration_manual
)
def save(self, *args, **kwargs):

View File

@ -17,8 +17,17 @@
{% for data in dataset %}
<div class=""><a class="dark:text-white hover:underline" href="{% url 'list_sessions' data.purchase.id %}">{{ data.purchase }}</a></div>
<div class="dark:text-slate-400">{{ data.timestamp_start | date:"d/m/Y H:i" }}</div>
<div class="dark:text-slate-400">{{ data.timestamp_end | date:"d/m/Y H:i" }}</div>
<div class="dark:text-slate-400">{{ data.time_delta }}</div>
<div class="dark:text-slate-400">
{% if data.unfinished %}
Not finished yet. <button class="bg-red-700 hover:bg-orange-700 border border-red-900 hover:border-dotted hover:border-white rounded p-1 text-white text-sm">Finish now?</button>
{% elif data.duration_manual %}
MANUAL
{% else %}
{{ data.timestamp_end | date:"d/m/Y H:i" }}
{% endif %}
</div>
{% load time %}
<div class="dark:text-slate-400">{{ data.duration_formatted }}{% if data.duration_manual %} (M){% endif %}</div>
{% endfor %}
</div>
{% endblock content %}

View File

@ -3,6 +3,10 @@ from django.shortcuts import render
from .models import Game, Platform, Purchase, Session
from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm
from datetime import datetime
from zoneinfo import ZoneInfo
from django.conf import settings
def model_counts(request):
return {
"game_available": Game.objects.count() != 0,
@ -33,11 +37,11 @@ def list_sessions(request, purchase_id=None):
else:
dataset = Session.objects.all()
dataset = dataset.annotate(
time_delta=ExpressionWrapper(
F("timestamp_end") - F("timestamp_start"), output_field=DurationField()
)
)
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
context["dataset"] = dataset
return render(request, "list_sessions.html", context)