Files
toptrumps/cards/models.py
T

62 lines
2.0 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)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-created_at', 'name']
def __str__(self):
return self.name
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()