70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
from django.db import models
|
|
|
|
|
|
class Dinosaur(models.Model):
|
|
ERA_CHOICES = [
|
|
('triassic', 'Triassic'),
|
|
('jurassic', 'Jurassic'),
|
|
('cretaceous', 'Cretaceous'),
|
|
]
|
|
|
|
DIET_CHOICES = [
|
|
('herbivore', 'Herbivore'),
|
|
('carnivore', 'Carnivore'),
|
|
('omnivore', 'Omnivore'),
|
|
]
|
|
|
|
name = models.CharField(max_length=120)
|
|
era = models.CharField(max_length=32, choices=ERA_CHOICES, default='jurassic')
|
|
diet = models.CharField(max_length=32, choices=DIET_CHOICES, default='herbivore')
|
|
description = models.TextField(blank=True)
|
|
image = models.ImageField(upload_to='dinosaurs/', blank=True, null=True)
|
|
|
|
# Top-trump style stats (0-100)
|
|
speed = models.PositiveSmallIntegerField(default=50)
|
|
weight = models.PositiveSmallIntegerField(default=50)
|
|
intelligence = models.PositiveSmallIntegerField(default=50)
|
|
height = models.PositiveSmallIntegerField(default=50)
|
|
facts = models.TextField(blank=True, help_text='One fact per line; shown as bullet points on the card')
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ['-created_at', 'name']
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
@property
|
|
def facts_list(self):
|
|
"""Return facts as a list of non-empty lines."""
|
|
if not self.facts:
|
|
return []
|
|
return [f.strip() for f in self.facts.splitlines() if f.strip()]
|
|
|
|
|
|
class BackgroundImage(models.Model):
|
|
"""Background images used behind card renders.
|
|
|
|
Admins can upload multiple backgrounds and mark one as active. Templates
|
|
will use BackgroundImage.get_active() to obtain the active background.
|
|
"""
|
|
title = models.CharField(max_length=200, blank=True)
|
|
image = models.ImageField(upload_to='backgrounds/')
|
|
is_active = models.BooleanField(default=False, help_text='Mark as the active background')
|
|
uploaded_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ['-is_active', '-uploaded_at']
|
|
|
|
def __str__(self):
|
|
return self.title or f"Background #{self.pk}"
|
|
|
|
@classmethod
|
|
def get_active(cls):
|
|
"""Return the active BackgroundImage if set, otherwise the most recent one or None."""
|
|
active = cls.objects.filter(is_active=True).order_by('-uploaded_at').first()
|
|
if active:
|
|
return active
|
|
return cls.objects.order_by('-uploaded_at').first()
|