From adbb5685a00fabc44f0f17b086299fdc5be97dc8 Mon Sep 17 00:00:00 2001 From: Luca Date: Sat, 4 May 2024 00:06:58 +0200 Subject: [PATCH] feat(pages): populate navs from Page objects --- shiftregister/pages/admin.py | 19 ++++++++++-- shiftregister/pages/apps.py | 5 ++-- ...how_in_footer_nav_page_show_in_main_nav.py | 23 ++++++++++++++ shiftregister/pages/models.py | 2 ++ shiftregister/pages/signals.py | 30 +++++++++++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 shiftregister/pages/migrations/0003_page_show_in_footer_nav_page_show_in_main_nav.py create mode 100644 shiftregister/pages/signals.py diff --git a/shiftregister/pages/admin.py b/shiftregister/pages/admin.py index a981f45..3969121 100644 --- a/shiftregister/pages/admin.py +++ b/shiftregister/pages/admin.py @@ -15,6 +15,21 @@ def reimport(modeladmin, request, queryset): @admin.register(Page) class PageAdmin(admin.ModelAdmin): - fields = ("url", "content", "title", "visible", "kind") - list_display = ("url", "title", "visible", "kind") + fields = ( + "url", + "content", + "title", + "visible", + "kind", + "show_in_footer_nav", + "show_in_main_nav", + ) + list_display = ( + "url", + "title", + "visible", + "kind", + "show_in_footer_nav", + "show_in_main_nav", + ) actions = (reimport,) diff --git a/shiftregister/pages/apps.py b/shiftregister/pages/apps.py index 894cca1..2089c21 100644 --- a/shiftregister/pages/apps.py +++ b/shiftregister/pages/apps.py @@ -1,8 +1,9 @@ -from pathlib import Path - from django.apps import AppConfig class PagesConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "shiftregister.pages" + + def ready(self): + from . import signals diff --git a/shiftregister/pages/migrations/0003_page_show_in_footer_nav_page_show_in_main_nav.py b/shiftregister/pages/migrations/0003_page_show_in_footer_nav_page_show_in_main_nav.py new file mode 100644 index 0000000..5184b47 --- /dev/null +++ b/shiftregister/pages/migrations/0003_page_show_in_footer_nav_page_show_in_main_nav.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0.4 on 2024-05-03 21:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("pages", "0002_page_kind_alter_page_title"), + ] + + operations = [ + migrations.AddField( + model_name="page", + name="show_in_footer_nav", + field=models.BooleanField(default=True), + ), + migrations.AddField( + model_name="page", + name="show_in_main_nav", + field=models.BooleanField(default=False), + ), + ] diff --git a/shiftregister/pages/models.py b/shiftregister/pages/models.py index 0f9d14e..0ad3417 100644 --- a/shiftregister/pages/models.py +++ b/shiftregister/pages/models.py @@ -16,6 +16,8 @@ class Page(models.Model): visible = models.BooleanField(default=True) title = models.CharField(blank=True, max_length=200) kind = models.CharField(choices=KIND_CHOICES, default=REGULAR, max_length=8) + show_in_footer_nav = models.BooleanField(default=True) + show_in_main_nav = models.BooleanField(default=False) def __str__(self): return ( diff --git a/shiftregister/pages/signals.py b/shiftregister/pages/signals.py new file mode 100644 index 0000000..d8e584c --- /dev/null +++ b/shiftregister/pages/signals.py @@ -0,0 +1,30 @@ +from django.dispatch import receiver +from django.urls import reverse + +from shiftregister.core.signals import populate_footer_nav, populate_nav + +from .models import Page + + +@receiver(populate_footer_nav, dispatch_uid="populate_pages_footer_nav") +def populate_pages_footer_nav(sender, **kwargs): + return [ + { + "link": reverse("pages:view", args=(page.url,)), + "text": page.title or page.url, + } + for page in Page.objects.filter(visible=True) + if page.visible and page.show_in_footer_nav + ] + + +@receiver(populate_nav, dispatch_uid="populate_pages_nav") +def populate_pages_nav(sender, **kwargs): + return [ + { + "link": reverse("pages:view", args=(page.url,)), + "text": page.title or page.url, + } + for page in Page.objects.filter(visible=True) + if page.visible and page.show_in_main_nav + ]