Add add_item_to_page_queryset method

This commit is contained in:
PCoder 2024-07-19 16:21:47 +02:00
parent 9618ac0094
commit 822fca63c3

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django import template from django import template
from django.utils import translation from django.utils import translation
from django.db.models import Case, When
from wagtail.core.models import Site, Page from wagtail.core.models import Site, Page
from ..models.forms import ContactForm from ..models.forms import ContactForm
@ -98,16 +99,36 @@ def menuitems_children(parent, context=None):
custom_page_id = 994 custom_page_id = 994
custom_page = Page.objects.get(id=custom_page_id) custom_page = Page.objects.get(id=custom_page_id)
menuitems_children = list(menuitems_children) 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] 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() menuitems_children = Page.objects.filter(id__in=page_ids).specific()
for menuitem in menuitems_children: for menuitem in menuitems_children:
try: try:
menuitem.title = menuitem.trans_title menuitem.title = menuitem.trans_title
print(menuitem.title)
except AttributeError as aee: except AttributeError as aee:
pass pass
menuitems_children = add_item_to_page_queryset(menuitems_children, custom_page)
return menuitems_children 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 # Retrieves the children of the top menu items for the drop downs
@register.inclusion_tag('tags/top_menu_children.html', takes_context=True) @register.inclusion_tag('tags/top_menu_children.html', takes_context=True)
def top_menu_children(context, parent): def top_menu_children(context, parent):