2023-01-05 21:01:15 +00:00
|
|
|
from datetime import datetime, timedelta
|
2023-01-15 22:39:52 +00:00
|
|
|
from typing import Any
|
2023-01-04 18:19:49 +00:00
|
|
|
from zoneinfo import ZoneInfo
|
2023-01-15 22:39:52 +00:00
|
|
|
|
2023-01-20 13:37:46 +00:00
|
|
|
from common.time import format_duration
|
2023-01-15 22:39:52 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models import F, Manager, Sum
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Game(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
wikidata = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2023-02-18 19:49:46 +00:00
|
|
|
class Edition(models.Model):
|
2022-12-31 13:18:27 +00:00
|
|
|
game = models.ForeignKey("Game", on_delete=models.CASCADE)
|
2023-02-18 19:49:46 +00:00
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
platform = models.ForeignKey("Platform", on_delete=models.CASCADE)
|
2023-02-20 16:13:21 +00:00
|
|
|
year_released = models.IntegerField(default=datetime.today().year)
|
|
|
|
wikidata = models.CharField(max_length=50, null=True, blank=True, default=None)
|
2023-02-18 19:49:46 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class Purchase(models.Model):
|
2023-02-18 19:57:03 +00:00
|
|
|
PHYSICAL = "ph"
|
|
|
|
DIGITAL = "di"
|
|
|
|
DIGITALUPGRADE = "du"
|
|
|
|
RENTED = "re"
|
|
|
|
BORROWED = "bo"
|
|
|
|
TRIAL = "tr"
|
|
|
|
DEMO = "de"
|
|
|
|
PIRATED = "pi"
|
|
|
|
OWNERSHIP_TYPES = [
|
|
|
|
(PHYSICAL, "Physical"),
|
|
|
|
(DIGITAL, "Digital"),
|
|
|
|
(DIGITALUPGRADE, "Digital Upgrade"),
|
|
|
|
(RENTED, "Rented"),
|
|
|
|
(BORROWED, "Borrowed"),
|
|
|
|
(TRIAL, "Trial"),
|
|
|
|
(DEMO, "Demo"),
|
|
|
|
(PIRATED, "Pirated"),
|
|
|
|
]
|
|
|
|
|
2023-02-18 19:49:46 +00:00
|
|
|
edition = models.ForeignKey("Edition", on_delete=models.CASCADE)
|
2022-12-31 13:18:27 +00:00
|
|
|
platform = models.ForeignKey("Platform", on_delete=models.CASCADE)
|
|
|
|
date_purchased = models.DateField()
|
|
|
|
date_refunded = models.DateField(blank=True, null=True)
|
2023-02-18 19:53:47 +00:00
|
|
|
price = models.IntegerField(default=0)
|
|
|
|
price_currency = models.CharField(max_length=3, default="USD")
|
2023-02-18 19:57:03 +00:00
|
|
|
ownership_type = models.CharField(
|
|
|
|
max_length=2, choices=OWNERSHIP_TYPES, default=DIGITAL
|
|
|
|
)
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-02-20 16:13:21 +00:00
|
|
|
platform_info = self.platform
|
|
|
|
if self.platform != self.edition.platform:
|
|
|
|
platform_info = f"{self.edition.platform} version on {self.platform}"
|
|
|
|
return f"{self.edition} ({platform_info}, {self.edition.year_released}, {self.get_ownership_type_display()})"
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Platform(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
group = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2023-01-09 17:57:22 +00:00
|
|
|
class SessionQuerySet(models.QuerySet):
|
2023-10-08 22:00:45 +00:00
|
|
|
def total_duration_formatted(self):
|
|
|
|
return format_duration(self.total_duration_unformatted())
|
|
|
|
|
|
|
|
def total_duration_unformatted(self):
|
2023-01-09 17:57:22 +00:00
|
|
|
result = self.aggregate(
|
|
|
|
duration=Sum(F("duration_calculated") + F("duration_manual"))
|
|
|
|
)
|
2023-10-08 22:00:45 +00:00
|
|
|
return result["duration"]
|
2023-01-09 17:57:22 +00:00
|
|
|
|
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
class Session(models.Model):
|
|
|
|
purchase = models.ForeignKey("Purchase", on_delete=models.CASCADE)
|
|
|
|
timestamp_start = models.DateTimeField()
|
2023-01-02 23:13:26 +00:00
|
|
|
timestamp_end = models.DateTimeField(blank=True, null=True)
|
2023-01-09 17:57:22 +00:00
|
|
|
duration_manual = models.DurationField(blank=True, null=True, default=timedelta(0))
|
2022-12-31 13:32:02 +00:00
|
|
|
duration_calculated = models.DurationField(blank=True, null=True)
|
2023-02-18 20:12:18 +00:00
|
|
|
device = models.ForeignKey("Device", on_delete=models.CASCADE, null=True)
|
2022-12-31 13:32:02 +00:00
|
|
|
note = models.TextField(blank=True, null=True)
|
2022-12-31 13:18:27 +00:00
|
|
|
|
2023-01-09 17:57:22 +00:00
|
|
|
objects = SessionQuerySet.as_manager()
|
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
def __str__(self):
|
2023-01-02 18:36:22 +00:00
|
|
|
mark = ", manual" if self.duration_manual != None else ""
|
2023-01-09 17:57:22 +00:00
|
|
|
return f"{str(self.purchase)} {str(self.timestamp_start.date())} ({self.duration_formatted()}{mark})"
|
2022-12-31 13:18:27 +00:00
|
|
|
|
2023-01-04 18:19:49 +00:00
|
|
|
def finish_now(self):
|
|
|
|
self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
|
|
|
|
|
2023-01-13 15:54:24 +00:00
|
|
|
def start_now():
|
|
|
|
self.timestamp_start = datetime.now(ZoneInfo(settings.TIME_ZONE))
|
|
|
|
|
2023-01-05 22:03:46 +00:00
|
|
|
def duration_seconds(self) -> timedelta:
|
2023-01-09 17:57:22 +00:00
|
|
|
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())
|
2023-01-02 18:36:22 +00:00
|
|
|
|
2023-01-05 21:01:15 +00:00
|
|
|
def duration_formatted(self) -> str:
|
2023-01-05 22:03:46 +00:00
|
|
|
result = format_duration(self.duration_seconds(), "%H:%m")
|
2023-01-05 21:01:15 +00:00
|
|
|
return result
|
2023-01-04 16:27:54 +00:00
|
|
|
|
2023-01-09 17:57:22 +00:00
|
|
|
@property
|
|
|
|
def duration_sum(self) -> str:
|
2023-10-08 22:00:45 +00:00
|
|
|
return Session.objects.all().total_duration_formatted()
|
2023-01-05 22:03:46 +00:00
|
|
|
|
2023-01-04 16:25:19 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.timestamp_start != None and self.timestamp_end != None:
|
|
|
|
self.duration_calculated = self.timestamp_end - self.timestamp_start
|
2023-01-09 17:57:22 +00:00
|
|
|
else:
|
|
|
|
self.duration_calculated = timedelta(0)
|
2023-01-04 16:25:19 +00:00
|
|
|
super(Session, self).save(*args, **kwargs)
|
2023-02-18 20:12:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Device(models.Model):
|
|
|
|
PC = "pc"
|
|
|
|
CONSOLE = "co"
|
|
|
|
HANDHELD = "ha"
|
|
|
|
MOBILE = "mo"
|
|
|
|
SBC = "sbc"
|
|
|
|
DEVICE_TYPES = [
|
|
|
|
(PC, "PC"),
|
|
|
|
(CONSOLE, "Console"),
|
|
|
|
(HANDHELD, "Handheld"),
|
|
|
|
(MOBILE, "Mobile"),
|
|
|
|
(SBC, "Single-board computer"),
|
|
|
|
]
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
type = models.CharField(max_length=3, choices=DEVICE_TYPES, default=PC)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.name} ({self.get_type_display()})"
|