1
0
Fork 0
mirror of https://github.com/Findus23/RPGnotes.git synced 2024-09-19 15:43:45 +02:00
RPGnotes/characters/models.py

81 lines
2.8 KiB
Python
Raw Normal View History

2022-06-19 13:53:21 +02:00
from django.contrib.postgres.fields import ArrayField
2021-09-05 18:52:26 +02:00
from django.core.exceptions import ValidationError
from django.core.validators import MinLengthValidator
2021-08-22 20:10:29 +02:00
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
2021-08-22 20:10:29 +02:00
from sorl.thumbnail import ImageField
2022-07-05 18:37:21 +02:00
from common.models import NameSlugModel, DescriptionModel, HistoryModel, AliasModel
2021-10-03 16:44:54 +02:00
from factions.models import Faction
2021-09-06 20:34:43 +02:00
from locations.models import Location
2021-08-29 16:45:40 +02:00
from rpg_notes.settings import AUTH_USER_MODEL
2022-04-11 22:43:06 +02:00
from search.utils import NameSearchIndex
2021-08-29 22:09:22 +02:00
from utils.colors import get_random_color, is_bright_color
2021-09-14 20:10:38 +02:00
from utils.random_filename import get_file_path
2021-08-22 20:10:29 +02:00
2021-09-05 18:52:26 +02:00
def validate_color_hex(value: str):
if not value.startswith("#"):
raise ValidationError("color hex has to start with a #")
2022-07-05 18:37:21 +02:00
class Character(NameSlugModel, DescriptionModel, AliasModel, HistoryModel):
2022-06-19 13:53:21 +02:00
aliases = ArrayField(
models.CharField(_("Nickname"), max_length=100),
verbose_name=_("Aliases"), blank=True, null=True
)
subtitle = models.CharField(_("Subtitle"), max_length=100, blank=True)
player = models.ForeignKey(
AUTH_USER_MODEL, on_delete=models.PROTECT, blank=True, null=True,
related_name="characters", verbose_name=_("Player"),
help_text=_("If no player is selected, this character is considered an NPC.")
)
2021-10-03 16:44:54 +02:00
faction = models.ForeignKey(
Faction, on_delete=models.PROTECT, blank=True, null=True,
related_name="characters",
verbose_name=_("Faction")
)
location = models.ForeignKey(
Location, on_delete=models.PROTECT, blank=True, null=True,
verbose_name=_("Location")
)
2021-11-25 14:00:11 +01:00
archived = models.BooleanField(_("Archived"), default=False)
color = models.CharField(_("Color"), max_length=7, default=get_random_color, validators=[
2021-09-05 18:52:26 +02:00
MinLengthValidator(7),
validate_color_hex
])
token_image = ImageField(_("Token Image"), upload_to=get_file_path, blank=True, null=True, help_text=_("round"))
large_image = ImageField(_("Large Image"), upload_to=get_file_path, blank=True, null=True)
2021-08-22 20:10:29 +02:00
class Meta:
2021-11-25 14:00:11 +01:00
ordering = ["archived", "name"]
verbose_name = _("Character")
verbose_name_plural = _("Characters")
2022-04-11 22:43:06 +02:00
indexes = [
NameSearchIndex
]
2021-08-22 20:10:29 +02:00
def get_absolute_url(self):
2021-08-29 00:20:02 +02:00
return reverse('characterdetail', args=[self.slug])
2021-08-22 20:10:29 +02:00
2021-09-26 19:02:25 +02:00
@property
2021-08-22 20:10:29 +02:00
def initials(self):
return "".join([word[0] for word in self.name.split()][:2]).upper()
2021-09-26 19:02:25 +02:00
@property
2021-08-22 20:10:29 +02:00
def text_color(self):
return "black" if is_bright_color(self.color) else "white"
2021-09-14 20:10:38 +02:00
2021-09-15 19:00:08 +02:00
@property
2021-09-14 20:10:38 +02:00
def larger_image(self):
if self.large_image:
return self.large_image
return self.token_image
2021-09-15 19:00:08 +02:00
@property
2021-09-14 20:10:38 +02:00
def smaller_image(self):
if self.token_image:
return self.token_image
return self.larger_image