guild-journal/guild/models.py

201 lines
5.6 KiB
Python
Raw Normal View History

2023-06-09 19:51:18 +00:00
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext as _
2023-06-11 19:25:30 +00:00
class Player(models.Model):
2023-06-10 19:33:06 +00:00
name = models.CharField(max_length=255, unique=True)
2023-06-09 19:51:18 +00:00
2023-06-19 18:23:15 +00:00
address = models.TextField(_("address"), blank=True)
2023-06-20 19:22:56 +00:00
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
2023-06-09 19:51:18 +00:00
class Meta:
verbose_name = _("player")
verbose_name_plural = _("players")
def __str__(self):
return self.name
def get_absolute_url(self):
2023-06-10 19:33:06 +00:00
return reverse("guild:player_detail", kwargs={"pk": self.pk})
2023-06-09 19:51:18 +00:00
2023-06-11 19:25:30 +00:00
class Resource(models.Model):
2023-06-20 19:22:56 +00:00
name = models.CharField(max_length=255, unique=True)
2023-06-09 19:51:18 +00:00
description = models.TextField()
2023-06-20 19:22:56 +00:00
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
2023-06-09 19:51:18 +00:00
class Meta:
verbose_name = _("resource")
verbose_name_plural = _("resources")
def __str__(self):
return self.name
def get_absolute_url(self):
2023-06-10 19:33:06 +00:00
return reverse("guild:resource_detail", kwargs={"pk": self.pk})
2023-06-09 19:51:18 +00:00
class Adventure(models.Model):
name = models.CharField(max_length=255)
2023-06-11 19:25:30 +00:00
description = models.TextField()
2023-06-09 19:51:18 +00:00
2023-06-11 19:25:30 +00:00
master = models.ForeignKey(
"Player",
verbose_name=_("master"),
on_delete=models.SET_NULL,
null=True,
blank=True,
)
2023-06-09 19:51:18 +00:00
2023-06-20 19:22:56 +00:00
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
2023-06-13 20:09:25 +00:00
@property
def last_session(self):
return self.playsession_set.order_by("-date").first()
2023-06-09 19:51:18 +00:00
class Meta:
verbose_name = _("Adventure")
verbose_name_plural = _("Adventures")
def __str__(self):
return self.name
def get_absolute_url(self):
2023-06-10 19:33:06 +00:00
return reverse("guild:adventure_detail", kwargs={"pk": self.pk})
2023-06-09 19:51:18 +00:00
class Character(models.Model):
2023-06-19 18:01:33 +00:00
class Status(models.TextChoices):
ALIVE = "ALIVE", _("Alive")
DEAD = "DEAD", _("Dead")
RETIRED = "RETIRED", _("Retired")
UNKNOWN = "UNKNOWN", _("Unknown")
2023-06-09 19:51:18 +00:00
name = models.CharField(max_length=255)
2023-06-11 19:25:30 +00:00
description = models.TextField()
2023-06-19 18:01:33 +00:00
status = models.CharField(
max_length=16, choices=Status.choices, default=Status.ALIVE
)
2023-06-09 19:51:18 +00:00
2023-06-11 19:25:30 +00:00
player = models.ForeignKey(
"Player",
verbose_name=_("player"),
on_delete=models.SET_NULL,
null=True,
blank=True,
)
2023-08-24 18:58:22 +00:00
picture = models.ImageField(
_("Character Picture"), null=True, blank=True, upload_to="uploads/"
)
2023-06-09 19:51:18 +00:00
2023-06-20 19:22:56 +00:00
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
2023-06-09 19:51:18 +00:00
class Meta:
verbose_name = _("character")
verbose_name_plural = _("characters")
def __str__(self):
return self.name
def get_absolute_url(self):
2023-06-10 19:33:06 +00:00
return reverse("guild:character_detail", kwargs={"pk": self.pk})
2023-06-09 19:51:18 +00:00
2023-06-11 19:25:30 +00:00
class PlaySession(models.Model):
2023-06-09 19:51:18 +00:00
date = models.DateField()
2023-06-11 19:25:30 +00:00
adventure = models.ForeignKey(
"Adventure", verbose_name=_("adventure"), on_delete=models.CASCADE
)
2023-06-09 19:51:18 +00:00
characters = models.ManyToManyField("Character", verbose_name=_("characters"))
2023-06-11 19:25:30 +00:00
summary = models.TextField()
2023-06-09 19:51:18 +00:00
2023-06-20 19:22:56 +00:00
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
2023-06-09 19:51:18 +00:00
class Meta:
verbose_name = _("playsession")
verbose_name_plural = _("playsessions")
2023-06-19 18:01:33 +00:00
ordering = ["-date"]
2023-06-09 19:51:18 +00:00
def __str__(self):
2023-06-13 20:09:25 +00:00
return str(self.date)
2023-06-09 19:51:18 +00:00
def get_absolute_url(self):
2023-06-11 19:25:30 +00:00
return (
reverse("guild:adventure_detail", kwargs={"pk": self.adventure.pk})
+ "#playsession"
+ str(self.pk)
)
2023-06-09 19:51:18 +00:00
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
PlaySessionSummaryVersion.objects.create(
playsession=self,
summary=self.summary,
)
class PlaySessionSummaryVersion(models.Model):
playsession = models.ForeignKey(
"PlaySession",
verbose_name=_("playsession"),
on_delete=models.SET_NULL,
null=True,
)
summary = models.TextField()
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
class Meta:
verbose_name = _("playsession summary version")
verbose_name_plural = _("playsession summary versions")
ordering = ["-created_at"]
def __str__(self):
return f"{self.playsession} - {self.created_at}"
def get_absolute_url(self):
return reverse("guild:playsession_detail", kwargs={"pk": self.playsession.pk})
2023-06-09 19:51:18 +00:00
2023-06-20 19:22:56 +00:00
class Reward(models.Model):
2023-06-11 19:25:30 +00:00
resource = models.ForeignKey(
"Resource", verbose_name=_("resource"), on_delete=models.CASCADE
)
character = models.ForeignKey(
"Character",
verbose_name=_("character"),
2023-06-20 19:22:56 +00:00
on_delete=models.SET_NULL,
2023-06-11 19:25:30 +00:00
null=True,
blank=True,
)
2023-06-20 19:22:56 +00:00
playsession = models.ForeignKey(
"PlaySession",
verbose_name=_("playsession"),
on_delete=models.CASCADE,
null=False,
)
2023-06-09 19:51:18 +00:00
amount = models.IntegerField()
2023-06-20 19:22:56 +00:00
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
2023-06-09 19:51:18 +00:00
class Meta:
2023-06-20 19:22:56 +00:00
verbose_name = _("reward")
verbose_name_plural = _("rewards")
2023-06-09 19:51:18 +00:00
def __str__(self):
2023-06-20 19:22:56 +00:00
return f"{self.resource.name} ({self.amount}) - {self.character}"
2023-06-09 19:51:18 +00:00
def get_absolute_url(self):
2023-06-20 19:22:56 +00:00
return reverse("guild:reward_detail", kwargs={"pk": self.pk})