mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 14:11:09 -05:00
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from django.db import models
|
|
from django.utils.text import slugify
|
|
from simple_history.models import HistoricalRecords
|
|
|
|
class Designer(models.Model):
|
|
name = models.CharField(max_length=255)
|
|
slug = models.SlugField(max_length=255, unique=True)
|
|
description = models.TextField(blank=True)
|
|
website = models.URLField(blank=True)
|
|
founded_date = models.DateField(null=True, blank=True)
|
|
headquarters = models.CharField(max_length=255, blank=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
history = HistoricalRecords()
|
|
|
|
class Meta:
|
|
ordering = ['name']
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not self.slug:
|
|
self.slug = slugify(self.name)
|
|
super().save(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def get_by_slug(cls, slug):
|
|
"""Get designer by current or historical slug"""
|
|
try:
|
|
return cls.objects.get(slug=slug), False
|
|
except cls.DoesNotExist:
|
|
# Check historical slugs
|
|
history = cls.history.filter(slug=slug).order_by('-history_date').first()
|
|
if history:
|
|
return cls.objects.get(id=history.id), True
|
|
raise cls.DoesNotExist("No designer found with this slug")
|