Skip to content

Commit

Permalink
Use an orderable for the categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-Crow committed Mar 20, 2024
1 parent f12cfb9 commit 2045b69
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 31 deletions.
32 changes: 26 additions & 6 deletions content_manager/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-18 15:40+0100\n"
"PO-Revision-Date: 2024-03-18 15:40+0100\n"
"POT-Creation-Date: 2024-03-20 15:31+0100\n"
"PO-Revision-Date: 2024-03-20 15:31+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
Expand Down Expand Up @@ -86,23 +86,43 @@ msgstr "Page"
msgid "External URL"
msgstr "URL externe"

#: content_manager/models.py:19
#: content_manager/models.py:21
msgid "Content page"
msgstr "Page de contenu"

#: content_manager/models.py:161
msgid "Category"
msgstr "Catégorie"

#: content_manager/models.py:164
msgid "Mega menu category"
msgstr "Catégorie de méga menu"

#: content_manager/models.py:165
msgid "Mega menu categories"
msgstr "Catégories de méga menu"

#: content_manager/models.py:170
msgid "Name"
msgstr "Nom"

#: content_manager/models.py:165
#: content_manager/models.py:174
msgid "Description"
msgstr "Description"

#: content_manager/models.py:166
#: content_manager/models.py:175
msgid "Main link"
msgstr "Lien principal"

#: content_manager/models.py:181
#: content_manager/models.py:185
msgid "Categories"
msgstr "Catégories"

#: content_manager/models.py:186
msgid "Maximum 4 categories, each with maximum 8 links."
msgstr "Maximum 4 catégories, chacune contenant maximum 8 liens."

#: content_manager/models.py:197
msgid "Mega menu"
msgstr "Méga menu"

Expand Down
23 changes: 12 additions & 11 deletions content_manager/management/commands/create_demo_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from wagtailmenus.models.menus import FlatMenu, MainMenu

from blog.models import BlogIndexPage
from content_manager.models import ContentPage, MegaMenu
from content_manager.models import ContentPage, MegaMenu, MegaMenuCategory


ALL_ALLOWED_SLUGS = ["blog_index", "publications"]
Expand Down Expand Up @@ -69,17 +69,20 @@ def handle(self, *args, **kwargs):
)

# Create a set of publications sub-pages
for i in range(1, 5):
menu_category, _created = FlatMenu.objects.get_or_create(
for i in range(4):
menu_category_menu, _created = FlatMenu.objects.get_or_create(
site_id=site.id,
title=f"Menu publications > Catégorie {i}",
handle=f"mega_menu_section_{i}",
heading=f"Colonne {i}",
title=f"Menu publications > Catégorie {i + 1}",
handle=f"mega_menu_section_{i + 1}",
heading=f"Colonne {i + 1}",
)

menu_category, _created = MegaMenuCategory.objects.get_or_create(
mega_menu=publications_mega_menu, sort_order=i, category=menu_category_menu
)

for j in range(8):
title = " ".join(fake.words(nb=3, unique=True)).capitalize()
title = f"{title} ({i} - {j + 1})"
title = f"Page {i + 1} - {j + 1}"

body = []
text = ""
Expand All @@ -91,9 +94,7 @@ def handle(self, *args, **kwargs):
slug=slugify(title), title=title, body=body, parent_page=publications_page
)

FlatMenuItem.objects.get_or_create(
link_page_id=new_page.id, menu_id=menu_category.id, sort_order=j
)
FlatMenuItem.objects.get_or_create(link_page=new_page, menu=menu_category_menu, sort_order=j)

publications_mega_menu.categories.add(menu_category)
publications_mega_menu.save()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 5.0.3 on 2024-03-18 16:02
# Generated by Django 5.0.3 on 2024-03-20 14:30

import django.db.models.deletion
import modelcluster.fields
from django.db import migrations, models


Expand All @@ -18,7 +19,6 @@ class Migration(migrations.Migration):
("name", models.CharField(max_length=255, verbose_name="Name")),
("description", models.TextField(blank=True, verbose_name="Description")),
("main_link", models.URLField(blank=True, null=True, verbose_name="Main link")),
("categories", models.ManyToManyField(blank=True, null=True, to="wagtailmenus.flatmenu")),
(
"parent_menu_item",
models.ForeignKey(
Expand All @@ -32,4 +32,31 @@ class Migration(migrations.Migration):
"verbose_name": "Mega menu",
},
),
migrations.CreateModel(
name="MegaMenuCategory",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("sort_order", models.IntegerField(blank=True, editable=False, null=True)),
(
"category",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="wagtailmenus.flatmenu",
verbose_name="Category",
),
),
(
"mega_menu",
modelcluster.fields.ParentalKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="categories",
to="content_manager.megamenu",
),
),
],
options={
"verbose_name": "Mega menu category",
"verbose_name_plural": "Mega menu categories",
},
),
]
25 changes: 18 additions & 7 deletions content_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from django.forms.widgets import Textarea
from django.utils.translation import gettext_lazy as _
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel
from modelcluster.tags import ClusterTaggableManager
from taggit.models import Tag as TaggitTag, TaggedItemBase
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.contrib.settings.models import BaseSiteSetting, register_setting
from wagtail.models import Orderable
from wagtail.snippets.models import register_snippet

from content_manager.abstract import SitesFacilesBasePage
Expand Down Expand Up @@ -154,33 +156,42 @@ class Meta:


# Mega-Menus
class MegaMenuCategory(Orderable):
mega_menu = ParentalKey("content_manager.MegaMenu", related_name="categories", on_delete=models.CASCADE)
category = models.ForeignKey("wagtailmenus.FlatMenu", on_delete=models.CASCADE, verbose_name=_("Category"))

class Meta:
verbose_name = _("Mega menu category")
verbose_name_plural = _("Mega menu categories")


@register_snippet
class MegaMenu(models.Model):
class MegaMenu(ClusterableModel):
name = models.CharField(_("Name"), max_length=255)
parent_menu_item = models.ForeignKey(
"wagtailmenus.MainMenuItem", on_delete=models.CASCADE, related_name="megamenu_parent_menu_items"
)
description = models.TextField(_("Description"), blank=True)
main_link = models.URLField(_("Main link"), blank=True, null=True)

categories = models.ManyToManyField(
"wagtailmenus.FlatMenu", blank=True, null=True, help_text=_("Maximum 4 categories, each with maximum 8 links.")
)
panels = [
FieldPanel("name"),
FieldPanel("parent_menu_item"),
FieldPanel("description"),
FieldPanel("main_link"),
FieldPanel("categories"),
InlinePanel(
"categories",
max_num=4,
heading=_("Categories"),
help_text=_("Maximum 4 categories, each with maximum 8 links."),
),
]

def __str__(self): # type: ignore
return self.name

def get_categories(self):
return self.categories.order_by("handle")
return self.categories.order_by("sort_order")

class Meta:
verbose_name = _("Mega menu")
10 changes: 5 additions & 5 deletions content_manager/templates/content_manager/menus/mega_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ <h4 class="fr-h4 fr-mb-2v">{{ menu.title }}</h4>
{% endif %}
</div>
</div>
{% for category in menu.get_categories|slice:":4" %}
{% for category in menu.get_categories %}
<div class="fr-col-12 fr-col-lg-3">
<h5 class="fr-mega-menu__category">
<a class="fr-nav__link" href="#" target="_self">
{% if category.heading %}
{{ category.heading }}
{% if category.category.heading %}
{{ category.category.heading }}
{% else %}
{{ category.title }}
{{ category.category.title }}
{% endif %}
</a>
</h5>
{% flat_menu category.handle template="content_manager/menus/mega_menu_category.html" %}
{% flat_menu category.category.handle template="content_manager/menus/mega_menu_category.html" %}
</div>
{% endfor %}
</div>
Expand Down

0 comments on commit 2045b69

Please sign in to comment.