Show playtime total on session list
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Fixes #6 Fixes #25
This commit is contained in:
@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
||||
from django.conf import settings
|
||||
from zoneinfo import ZoneInfo
|
||||
from common.util.time import format_duration
|
||||
from django.db.models import Sum
|
||||
from django.db.models import Sum, F
|
||||
|
||||
|
||||
class Game(models.Model):
|
||||
@ -32,69 +32,51 @@ class Platform(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
class SessionQuerySet(models.QuerySet):
|
||||
def total_duration(self):
|
||||
result = self.aggregate(
|
||||
duration=Sum(F("duration_calculated") + F("duration_manual"))
|
||||
)
|
||||
return format_duration(result["duration"])
|
||||
|
||||
|
||||
class Session(models.Model):
|
||||
purchase = models.ForeignKey("Purchase", on_delete=models.CASCADE)
|
||||
timestamp_start = models.DateTimeField()
|
||||
timestamp_end = models.DateTimeField(blank=True, null=True)
|
||||
duration_manual = models.DurationField(blank=True, null=True)
|
||||
duration_manual = models.DurationField(blank=True, null=True, default=timedelta(0))
|
||||
duration_calculated = models.DurationField(blank=True, null=True)
|
||||
note = models.TextField(blank=True, null=True)
|
||||
|
||||
objects = SessionQuerySet.as_manager()
|
||||
|
||||
def __str__(self):
|
||||
mark = ", manual" if self.duration_manual != None else ""
|
||||
return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.duration_any()}{mark})"
|
||||
return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.duration_formatted()}{mark})"
|
||||
|
||||
def finish_now(self):
|
||||
self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
|
||||
|
||||
def duration_seconds(self) -> timedelta:
|
||||
if self.duration_manual == None:
|
||||
if self.timestamp_end == None or self.timestamp_start == None:
|
||||
return timedelta(0)
|
||||
else:
|
||||
value = self.timestamp_end - self.timestamp_start
|
||||
else:
|
||||
value = self.duration_manual
|
||||
return timedelta(seconds=value.total_seconds())
|
||||
manual = timedelta(0)
|
||||
calculated = timedelta(0)
|
||||
if not self.duration_manual in (None, 0, timedelta(0)):
|
||||
manual = self.duration_manual
|
||||
if self.timestamp_end != None and self.timestamp_start != None:
|
||||
calculated = self.timestamp_end - self.timestamp_start
|
||||
return timedelta(seconds=(manual + calculated).total_seconds())
|
||||
|
||||
def duration_formatted(self) -> str:
|
||||
result = format_duration(self.duration_seconds(), "%H:%m")
|
||||
return result
|
||||
|
||||
def duration_any(self):
|
||||
return (
|
||||
self.duration_formatted()
|
||||
if self.duration_manual == None
|
||||
else self.duration_manual
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def calculated_sum() -> timedelta:
|
||||
calculated_sum_query = Session.objects.all().aggregate(
|
||||
Sum("duration_calculated")
|
||||
)
|
||||
calculated_sum = (
|
||||
timedelta(0)
|
||||
if calculated_sum_query["duration_calculated__sum"] == None
|
||||
else calculated_sum_query["duration_calculated__sum"]
|
||||
)
|
||||
return calculated_sum
|
||||
|
||||
@staticmethod
|
||||
def manual_sum() -> timedelta:
|
||||
manual_sum_query = Session.objects.all().aggregate(Sum("duration_manual"))
|
||||
manual_sum = (
|
||||
timedelta(0)
|
||||
if manual_sum_query["duration_manual__sum"] == None
|
||||
else manual_sum_query["duration_manual__sum"]
|
||||
)
|
||||
return manual_sum
|
||||
|
||||
@staticmethod
|
||||
def total_sum() -> timedelta:
|
||||
return Session.manual_sum() + Session.calculated_sum()
|
||||
@property
|
||||
def duration_sum(self) -> str:
|
||||
return Session.objects.all().total_duration()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.timestamp_start != None and self.timestamp_end != None:
|
||||
self.duration_calculated = self.timestamp_end - self.timestamp_start
|
||||
else:
|
||||
self.duration_calculated = timedelta(0)
|
||||
super(Session, self).save(*args, **kwargs)
|
||||
|
Reference in New Issue
Block a user