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

67 lines
2.5 KiB
Python
Raw Normal View History

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
2021-09-15 16:51:43 +02:00
from common.models import NameSlugModel, DescriptionModel, HistoryModel
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
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 #")
2021-09-15 16:51:43 +02:00
class Character(NameSlugModel, DescriptionModel, HistoryModel):
subtitle = models.CharField(_("Subtitle"), max_length=100, blank=True)
2021-09-14 20:10:38 +02:00
player = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.PROTECT, blank=True, null=True,
related_name="characters", verbose_name=_("Player"))
2021-08-29 22:09:22 +02:00
# faction = models.ForeignKey(Faction, on_delete=models.PROTECT, blank=True, null=True)
location = models.ForeignKey(Location, on_delete=models.PROTECT, blank=True, null=True,
verbose_name=_("Location"),
help_text=_("If no player is selected, this character is considered an NPC.")
)
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:
ordering = ["name"]
verbose_name = _("Character")
verbose_name_plural = _("Characters")
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
def firstname(self):
return self.name.split()[0]
@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