Browse Source
Refactor Hadis and Transmitter models to utilize ColorPaletteMixin for color management
Refactor Hadis and Transmitter models to utilize ColorPaletteMixin for color management
- Introduced `ColorPaletteMixin` to centralize color choice fields and hex code properties across `HadisStatus`, `TransmitterReliability`, and `OpinionStatus` models. - Updated the `color` field in these models to use the mixin, enhancing code reusability and maintainability. - Modified serializers to include `main_color_code` and `light_color_code` properties for better color representation in API responses. - Added a new migration to adjust the database schema for the updated models.master
6 changed files with 111 additions and 64 deletions
-
28apps/hadis/migrations/0007_alter_hadisstatus_color_alter_opinionstatus_color_and_more.py
-
37apps/hadis/models/hadis.py
-
24apps/hadis/models/transmitter.py
-
32apps/hadis/serializers/hadis.py
-
4apps/hadis/urls.py
-
50utils/mixins.py
@ -0,0 +1,28 @@ |
|||||
|
# Generated by Django 4.2.27 on 2026-02-18 14:30 |
||||
|
|
||||
|
from django.db import migrations, models |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
|
||||
|
dependencies = [ |
||||
|
('hadis', '0006_hadis_embedded_in_hadiscorrection_embedded_in_and_more'), |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.AlterField( |
||||
|
model_name='hadisstatus', |
||||
|
name='color', |
||||
|
field=models.CharField(choices=[('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ('yellow', 'Yellow'), ('orange', 'Orange'), ('purple', 'Purple'), ('gray', 'Gray')], default='gray', max_length=20, verbose_name='color'), |
||||
|
), |
||||
|
migrations.AlterField( |
||||
|
model_name='opinionstatus', |
||||
|
name='color', |
||||
|
field=models.CharField(choices=[('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ('yellow', 'Yellow'), ('orange', 'Orange'), ('purple', 'Purple'), ('gray', 'Gray')], default='gray', max_length=20, verbose_name='color'), |
||||
|
), |
||||
|
migrations.AlterField( |
||||
|
model_name='transmitterreliability', |
||||
|
name='color', |
||||
|
field=models.CharField(choices=[('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ('yellow', 'Yellow'), ('orange', 'Orange'), ('purple', 'Purple'), ('gray', 'Gray')], default='gray', max_length=20, verbose_name='color'), |
||||
|
), |
||||
|
] |
||||
@ -0,0 +1,50 @@ |
|||||
|
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'] |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue