Skip to content

Commit

Permalink
refactor(banner_config): simplify BannerConfig fields and remove uniq…
Browse files Browse the repository at this point in the history
…ue constraint

- Renames fields to remove the `banner_` prefix for clarity.
- Removes the unique constraint on `is_active`, allowing the model's save()
  logic to automatically deactivate other active banners from the Django admin.
- Consolidates all changes into a single migration.
  • Loading branch information
elisa-a-v committed Dec 10, 2024
1 parent 7d13836 commit 8cef80e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 75 deletions.
10 changes: 5 additions & 5 deletions bc/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

@admin.register(BannerConfig)
class BannerConfigAdmin(admin.ModelAdmin):
list_display = ("__str__", "is_active", "banner_title", "banner_text")
list_display = ("__str__", "is_active", "title", "text")
list_filter = ("is_active",)
search_fields = (
"banner_title",
"banner_text",
"banner_button_text",
"banner_button_link",
"title",
"text",
"button_text",
"button_link",
)
25 changes: 11 additions & 14 deletions bc/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.1 on 2024-12-06 16:36
# Generated by Django 5.1.1 on 2024-12-10 15:31

from django.db import migrations, models

Expand Down Expand Up @@ -28,19 +28,16 @@ class Migration(migrations.Migration):
help_text="If another config is currently active, enabling this one will deactivate the first one.",
),
),
("banner_title", models.CharField(max_length=255)),
("banner_text", models.TextField()),
("banner_button_text", models.CharField(max_length=40)),
("banner_button_link", models.URLField()),
(
"title",
models.CharField(blank=True, max_length=255, null=True),
),
("text", models.TextField(blank=True, null=True)),
(
"button_text",
models.CharField(blank=True, max_length=40, null=True),
),
("button_link", models.URLField(blank=True, null=True)),
],
options={
"constraints": [
models.UniqueConstraint(
condition=models.Q(("is_active", True)),
fields=("is_active",),
name="only_one_active_banner",
)
],
},
),
]

This file was deleted.

21 changes: 5 additions & 16 deletions bc/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,14 @@ class BannerConfig(models.Model):
default=False,
help_text="If another config is currently active, enabling this one will deactivate the first one.",
)
banner_title = models.CharField(max_length=255, null=True, blank=True)
banner_text = models.TextField(null=True, blank=True)
banner_button_text = models.CharField(max_length=40, null=True, blank=True)
banner_button_link = models.URLField(null=True, blank=True)

class Meta:
# This constraint ensures that only one BannerConfig
# can have is_active = True at any given time.
constraints = [
models.UniqueConstraint(
fields=["is_active"],
condition=Q(is_active=True),
name="only_one_active_banner",
)
]
title = models.CharField(max_length=255, null=True, blank=True)
text = models.TextField(null=True, blank=True)
button_text = models.CharField(max_length=40, null=True, blank=True)
button_link = models.URLField(null=True, blank=True)

def __str__(self):
status = "active" if self.is_active else "inactive"
title = self.banner_title or "Banner"
title = self.title or "Banner"
return f"{self.pk}: {title} ({status})"

def save(self, *args, **kwargs):
Expand Down
12 changes: 4 additions & 8 deletions bc/core/utils/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ def banner_settings(request):
active_banner = BannerConfig.objects.filter(is_active=True).first()
return {
"HEADER_BANNER_ENABLED": bool(active_banner),
"HEADER_BANNER_TITLE": (
active_banner.banner_title if active_banner else ""
),
"HEADER_BANNER_TEXT": (
active_banner.banner_text if active_banner else ""
),
"HEADER_BANNER_TITLE": (active_banner.title if active_banner else ""),
"HEADER_BANNER_TEXT": (active_banner.text if active_banner else ""),
"HEADER_BANNER_BUTTON_TEXT": (
active_banner.banner_button_text if active_banner else ""
active_banner.button_text if active_banner else ""
),
"HEADER_BANNER_BUTTON_LINK": (
active_banner.banner_button_link if active_banner else ""
active_banner.button_link if active_banner else ""
),
}

0 comments on commit 8cef80e

Please sign in to comment.