mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 11:11:10 -05:00
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from django.db import models
|
|
from django.utils.text import slugify
|
|
from history_tracking.models import TrackedModel
|
|
import pghistory
|
|
|
|
@pghistory.track()
|
|
class Designer(TrackedModel):
|
|
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)
|
|
|
|
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 using pghistory
|
|
history_model = cls.get_history_model()
|
|
history = (
|
|
history_model.objects.filter(slug=slug)
|
|
.order_by('-pgh_created_at')
|
|
.first()
|
|
)
|
|
if history:
|
|
return cls.objects.get(id=history.pgh_obj_id), True
|
|
raise cls.DoesNotExist("No designer found with this slug")
|