from django.db import models from django.utils.translation import gettext_lazy as _ # 1. Define choices globally so they can be reused class StatusColorChoices(models.TextChoices): RED = 'red', _('Red') GREEN = 'green', _('Green') BLUE = 'blue', _('Blue') YELLOW = 'yellow', _('Yellow') ORANGE = 'orange', _('Orange') PURPLE = 'purple', _('Purple') GRAY = 'gray', _('Gray') # 2. Create the Abstract Mixin class ColorPaletteMixin(models.Model): """ An abstract base class that provides color choice fields and hex code properties to any model that inherits it. """ COLOR_PALETTE = { 'red': {'main': '#D33A3A', 'light': '#FBEBEB'}, 'green': {'main': '#1DAC43', 'light': '#E8F7EC'}, 'blue': {'main': '#5172E1', 'light': '#FAFBFC'}, 'yellow': {'main': '#EDC130', 'light': '#FCF8EA'}, 'orange': {'main': '#E67E22', 'light': '#FDF1E6'}, 'purple': {'main': '#7C5CC4', 'light': '#F2EDFA'}, 'gray': {'main': '#374151', 'light': '#EEEFF2'}, } color = models.CharField( max_length=20, choices=StatusColorChoices.choices, verbose_name=_('color'), default=StatusColorChoices.GRAY ) class Meta: abstract = True # Tells Django NOT to create a database table for this class @property def color_hashes(self): return self.COLOR_PALETTE.get(self.color, {'main': '#000000', 'light': '#FFFFFF'}) @property def main_color_code(self): return self.color_hashes['main'] @property def light_color_code(self): return self.color_hashes['light']