From 822fca63c3852968ca9fdb680756b33ce2997582 Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 19 Jul 2024 16:21:47 +0200 Subject: [PATCH] Add add_item_to_page_queryset method --- publichealth/home/templatetags/navigation.py | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/publichealth/home/templatetags/navigation.py b/publichealth/home/templatetags/navigation.py index 21d1d8a..3bb6ea9 100644 --- a/publichealth/home/templatetags/navigation.py +++ b/publichealth/home/templatetags/navigation.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from django import template from django.utils import translation +from django.db.models import Case, When from wagtail.core.models import Site, Page from ..models.forms import ContactForm @@ -98,16 +99,36 @@ def menuitems_children(parent, context=None): custom_page_id = 994 custom_page = Page.objects.get(id=custom_page_id) menuitems_children = list(menuitems_children) - menuitems_children.append(custom_page) + if custom_page not in menuitems_children: + #menuitems_children.append(custom_page) + print("custom_page == ") page_ids = [page.id for page in menuitems_children] + print("hostname is %s" % site.hostname) menuitems_children = Page.objects.filter(id__in=page_ids).specific() for menuitem in menuitems_children: try: menuitem.title = menuitem.trans_title + print(menuitem.title) except AttributeError as aee: pass + menuitems_children = add_item_to_page_queryset(menuitems_children, custom_page) return menuitems_children +def add_item_to_page_queryset(queryset, custom_page): + # Convert queryset to a list + pages_list = list(queryset) + + # Add the custom page to the list + pages_list.append(custom_page) + + # Create a new ordered queryset based on the list order + page_ids = [page.id for page in pages_list] + preserved_order = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(page_ids)]) + new_queryset = Page.objects.filter(id__in=page_ids).order_by(preserved_order).specific() + + return new_queryset + + # Retrieves the children of the top menu items for the drop downs @register.inclusion_tag('tags/top_menu_children.html', takes_context=True) def top_menu_children(context, parent):