2023-11-15 16:47:51 +00:00
|
|
|
from datetime import timedelta
|
2023-11-15 18:36:22 +00:00
|
|
|
|
2023-11-15 12:04:47 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2023-11-15 18:36:22 +00:00
|
|
|
from django.db import models
|
2024-08-08 19:19:43 +00:00
|
|
|
from django.db.models import F, Sum
|
2024-09-14 09:07:38 +00:00
|
|
|
from django.template.defaultfilters import slugify
|
2023-11-15 18:36:22 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from common.time import format_duration
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Game(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2023-11-09 13:49:00 +00:00
|
|
|
sort_name = models.CharField(max_length=255, null=True, blank=True, default=None)
|
2023-11-06 18:48:12 +00:00
|
|
|
year_released = models.IntegerField(null=True, blank=True, default=None)
|
|
|
|
wikidata = models.CharField(max_length=50, null=True, blank=True, default=None)
|
2023-11-15 16:47:51 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2022-12-31 13:18:27 +00:00
|
|
|
|
2024-08-08 19:19:43 +00:00
|
|
|
session_average: float | int | timedelta | None
|
|
|
|
session_count: int | None
|
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2024-08-08 18:22:25 +00:00
|
|
|
class Platform(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
group = models.CharField(max_length=255, null=True, blank=True, default=None)
|
2024-09-14 09:07:38 +00:00
|
|
|
icon = models.SlugField(blank=True)
|
2024-08-08 18:22:25 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2024-09-14 09:07:38 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.icon:
|
|
|
|
self.icon = slugify(self.name)
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2024-08-08 18:22:25 +00:00
|
|
|
|
2024-10-16 16:06:40 +00:00
|
|
|
def get_sentinel_platform():
|
|
|
|
return Platform.objects.get_or_create(
|
|
|
|
name="Unspecified", icon="unspecified", group="Unspecified"
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
|
2023-02-18 19:49:46 +00:00
|
|
|
class Edition(models.Model):
|
2023-11-06 18:48:12 +00:00
|
|
|
class Meta:
|
2023-11-28 13:44:11 +00:00
|
|
|
unique_together = [["name", "platform", "year_released"]]
|
2023-11-06 18:48:12 +00:00
|
|
|
|
2024-08-08 18:22:25 +00:00
|
|
|
game = models.ForeignKey(Game, on_delete=models.CASCADE)
|
2023-02-18 19:49:46 +00:00
|
|
|
name = models.CharField(max_length=255)
|
2023-11-09 13:49:00 +00:00
|
|
|
sort_name = models.CharField(max_length=255, null=True, blank=True, default=None)
|
2023-11-06 18:48:12 +00:00
|
|
|
platform = models.ForeignKey(
|
2024-08-11 18:21:27 +00:00
|
|
|
Platform, on_delete=models.SET_DEFAULT, null=True, blank=True, default=None
|
2023-11-06 18:48:12 +00:00
|
|
|
)
|
|
|
|
year_released = models.IntegerField(null=True, blank=True, default=None)
|
2023-02-20 16:13:21 +00:00
|
|
|
wikidata = models.CharField(max_length=50, null=True, blank=True, default=None)
|
2023-11-15 16:47:51 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2023-02-18 19:49:46 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-11-09 13:49:00 +00:00
|
|
|
return self.sort_name
|
|
|
|
|
2024-10-16 16:06:40 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.platform is None:
|
|
|
|
self.platform = get_sentinel_platform()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2023-02-18 19:49:46 +00:00
|
|
|
|
2023-11-08 17:13:48 +00:00
|
|
|
class PurchaseQueryset(models.QuerySet):
|
|
|
|
def refunded(self):
|
|
|
|
return self.filter(date_refunded__isnull=False)
|
|
|
|
|
|
|
|
def not_refunded(self):
|
|
|
|
return self.filter(date_refunded__isnull=True)
|
|
|
|
|
|
|
|
def finished(self):
|
|
|
|
return self.filter(date_finished__isnull=False)
|
|
|
|
|
2023-11-14 18:27:00 +00:00
|
|
|
def games_only(self):
|
|
|
|
return self.filter(type=Purchase.GAME)
|
|
|
|
|
2023-11-08 17:13:48 +00:00
|
|
|
|
2023-02-18 19:49:46 +00:00
|
|
|
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-11-14 18:27:00 +00:00
|
|
|
GAME = "game"
|
|
|
|
DLC = "dlc"
|
|
|
|
SEASONPASS = "season_pass"
|
|
|
|
BATTLEPASS = "battle_pass"
|
|
|
|
TYPES = [
|
|
|
|
(GAME, "Game"),
|
|
|
|
(DLC, "DLC"),
|
|
|
|
(SEASONPASS, "Season Pass"),
|
|
|
|
(BATTLEPASS, "Battle Pass"),
|
|
|
|
]
|
2023-02-18 19:57:03 +00:00
|
|
|
|
2023-11-08 17:13:48 +00:00
|
|
|
objects = PurchaseQueryset().as_manager()
|
|
|
|
|
2024-08-08 18:22:25 +00:00
|
|
|
edition = models.ForeignKey(Edition, on_delete=models.CASCADE)
|
2023-11-06 18:48:12 +00:00
|
|
|
platform = models.ForeignKey(
|
2024-08-08 18:22:25 +00:00
|
|
|
Platform, on_delete=models.CASCADE, default=None, null=True, blank=True
|
2023-11-06 18:48:12 +00:00
|
|
|
)
|
2022-12-31 13:18:27 +00:00
|
|
|
date_purchased = models.DateField()
|
|
|
|
date_refunded = models.DateField(blank=True, null=True)
|
2023-11-06 18:48:12 +00:00
|
|
|
date_finished = models.DateField(blank=True, null=True)
|
2024-01-03 21:35:39 +00:00
|
|
|
date_dropped = models.DateField(blank=True, null=True)
|
|
|
|
infinite = models.BooleanField(default=False)
|
2024-10-04 09:36:46 +00:00
|
|
|
price = models.FloatField(default=0)
|
2023-02-18 19:53:47 +00:00
|
|
|
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
|
|
|
|
)
|
2023-11-14 18:27:00 +00:00
|
|
|
type = models.CharField(max_length=255, choices=TYPES, default=GAME)
|
2023-11-15 12:04:47 +00:00
|
|
|
name = models.CharField(max_length=255, default="", null=True, blank=True)
|
2023-11-14 18:27:00 +00:00
|
|
|
related_purchase = models.ForeignKey(
|
2024-08-08 18:22:25 +00:00
|
|
|
"self",
|
2023-11-14 22:45:41 +00:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
default=None,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
related_name="related_purchases",
|
2023-11-14 18:27:00 +00:00
|
|
|
)
|
2023-11-15 16:47:51 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2022-12-31 13:18:27 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-11-14 18:55:56 +00:00
|
|
|
additional_info = [
|
|
|
|
self.get_type_display() if self.type != Purchase.GAME else "",
|
2024-07-09 21:04:14 +00:00
|
|
|
(
|
|
|
|
f"{self.edition.platform} version on {self.platform}"
|
|
|
|
if self.platform != self.edition.platform
|
|
|
|
else self.platform
|
|
|
|
),
|
2023-11-14 18:55:56 +00:00
|
|
|
self.edition.year_released,
|
|
|
|
self.get_ownership_type_display(),
|
|
|
|
]
|
|
|
|
return f"{self.edition} ({', '.join(filter(None, map(str, additional_info)))})"
|
2022-12-31 13:18:27 +00:00
|
|
|
|
2023-11-14 18:27:00 +00:00
|
|
|
def is_game(self):
|
|
|
|
return self.type == self.GAME
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.type == Purchase.GAME:
|
|
|
|
self.name = ""
|
2023-11-15 12:04:47 +00:00
|
|
|
elif self.type != Purchase.GAME and not self.related_purchase:
|
|
|
|
raise ValidationError(
|
|
|
|
f"{self.get_type_display()} must have a related purchase."
|
|
|
|
)
|
2023-11-14 18:27:00 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
|
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
|
|
|
|
2024-07-02 15:14:56 +00:00
|
|
|
def calculated_duration_formatted(self):
|
|
|
|
return format_duration(self.calculated_duration_unformatted())
|
|
|
|
|
|
|
|
def calculated_duration_unformatted(self):
|
|
|
|
result = self.aggregate(duration=Sum(F("duration_calculated")))
|
|
|
|
return result["duration"]
|
|
|
|
|
|
|
|
def without_manual(self):
|
|
|
|
return self.exclude(duration_calculated__iexact=0)
|
|
|
|
|
|
|
|
def only_manual(self):
|
|
|
|
return self.filter(duration_calculated__iexact=0)
|
|
|
|
|
2023-01-09 17:57:22 +00:00
|
|
|
|
2022-12-31 13:18:27 +00:00
|
|
|
class Session(models.Model):
|
2023-11-15 16:47:51 +00:00
|
|
|
class Meta:
|
|
|
|
get_latest_by = "timestamp_start"
|
|
|
|
|
2024-08-08 18:22:25 +00:00
|
|
|
purchase = models.ForeignKey(Purchase, on_delete=models.CASCADE)
|
2022-12-31 13:18:27 +00:00
|
|
|
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-11-06 18:48:12 +00:00
|
|
|
device = models.ForeignKey(
|
|
|
|
"Device",
|
2024-08-11 15:53:36 +00:00
|
|
|
on_delete=models.SET_DEFAULT,
|
2023-11-06 18:48:12 +00:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
default=None,
|
|
|
|
)
|
2022-12-31 13:32:02 +00:00
|
|
|
note = models.TextField(blank=True, null=True)
|
2023-11-15 16:47:51 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified_at = models.DateTimeField(auto_now=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-11-11 14:02:28 +00:00
|
|
|
mark = ", manual" if self.is_manual() 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):
|
2023-11-15 16:47:51 +00:00
|
|
|
self.timestamp_end = timezone.now()
|
2023-01-04 18:19:49 +00:00
|
|
|
|
2023-01-13 15:54:24 +00:00
|
|
|
def start_now():
|
2023-11-15 16:47:51 +00:00
|
|
|
self.timestamp_start = timezone.now()
|
2023-01-13 15:54:24 +00:00
|
|
|
|
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)
|
2024-07-22 06:36:36 +00:00
|
|
|
if self.is_manual() and isinstance(self.duration_manual, timedelta):
|
2023-01-09 17:57:22 +00:00
|
|
|
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-10-13 14:32:12 +00:00
|
|
|
result = format_duration(self.duration_seconds(), "%02.0H:%02.0m")
|
2023-01-05 21:01:15 +00:00
|
|
|
return result
|
2023-01-04 16:27:54 +00:00
|
|
|
|
2023-11-11 14:02:28 +00:00
|
|
|
def is_manual(self) -> bool:
|
|
|
|
return not self.duration_manual == timedelta(0)
|
|
|
|
|
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
|
|
|
|
2024-08-08 19:19:43 +00:00
|
|
|
def save(self, *args, **kwargs) -> None:
|
2023-01-04 16:25:19 +00:00
|
|
|
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-11-06 18:48:12 +00:00
|
|
|
|
2024-07-22 06:36:36 +00:00
|
|
|
if not isinstance(self.duration_manual, timedelta):
|
|
|
|
self.duration_manual = timedelta(0)
|
|
|
|
|
2023-11-06 18:48:12 +00:00
|
|
|
if not self.device:
|
|
|
|
default_device, _ = Device.objects.get_or_create(
|
|
|
|
type=Device.UNKNOWN, defaults={"name": "Unknown"}
|
|
|
|
)
|
|
|
|
self.device = default_device
|
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"
|
2023-11-06 18:48:12 +00:00
|
|
|
UNKNOWN = "un"
|
2023-02-18 20:12:18 +00:00
|
|
|
DEVICE_TYPES = [
|
|
|
|
(PC, "PC"),
|
|
|
|
(CONSOLE, "Console"),
|
|
|
|
(HANDHELD, "Handheld"),
|
|
|
|
(MOBILE, "Mobile"),
|
|
|
|
(SBC, "Single-board computer"),
|
2023-11-06 18:48:12 +00:00
|
|
|
(UNKNOWN, "Unknown"),
|
2023-02-18 20:12:18 +00:00
|
|
|
]
|
|
|
|
name = models.CharField(max_length=255)
|
2023-11-06 18:48:12 +00:00
|
|
|
type = models.CharField(max_length=3, choices=DEVICE_TYPES, default=UNKNOWN)
|
2023-11-15 16:47:51 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2023-02-18 20:12:18 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.name} ({self.get_type_display()})"
|