Skip to content

Commit

Permalink
Bump Wagtail version to 2.16.3 (#7401)
Browse files Browse the repository at this point in the history
* Bump Wagtail version to 2.16.3

This commit bumps the version of Wagtail from 2.15.5 to 2.16.3.

* Fix Cypress tests that rely on Wagtail admin

The Wagtail admin layout changed in 2.16 and so the Cypress tests that
rely on certain CSS classes need to be changed.

https://docs.wagtail.org/en/stable/releases/2.16.html#slim-sidebar

* Fix megamenu-related test failures

Properly implement bulk_to_python, see conversation here:

wagtail/wagtail#5976 (comment)
  • Loading branch information
chosak committed Dec 21, 2022
1 parent c249284 commit c991948
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 54 deletions.
60 changes: 12 additions & 48 deletions cfgov/mega_menu/blocks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from itertools import chain

from wagtail.core import blocks
from wagtail.core.models import Page

Expand All @@ -9,6 +7,18 @@ class LinkBlock(blocks.StructBlock):
text = blocks.CharBlock(required=False)
url = blocks.CharBlock(required=False)

def bulk_to_python(self, values):
page_ids = (value.get("page") for value in values)

pages = Page.objects.in_bulk(page_ids)

for value in values:
page_id = value.get("page")
if page_id is not None:
value["page"] = pages[page_id]

return values


class LinkWithIconBlock(LinkBlock):
icon = blocks.CharBlock()
Expand All @@ -26,52 +36,6 @@ class SubmenuBlock(blocks.StructBlock):
other_links = blocks.ListBlock(LinkWithIconBlock(), default=[])
columns = blocks.ListBlock(SubmenuColumnBlock(), default=[])

def bulk_to_python(self, values):
"""Support bulk page retrieval to reduce database queries."""
page_ids = set(chain(*map(self.get_referenced_page_ids, values)))
pages = Page.objects.in_bulk(page_ids)

for value in values:
self.replace_referenced_page_ids_with_pages(value, pages)

return [blocks.StructValue(self, value) for value in values]

def get_referenced_page_ids(self, value):
"""Collect all page IDs referenced by this block."""
page_ids = list()

# The submenu overview page.
page_ids.append(value.get("overview_page"))

# Any pages in featured, other, and column links.
page_ids.extend(link.get("page") for link in self.link_iterator(value))

# Return the unique set of non-null page IDs.
return set(page_id for page_id in page_ids if page_id is not None)

def replace_referenced_page_ids_with_pages(self, value, pages):
"""Replace page ID references with Page instances."""
# The submenu overview page.
if "overview_page" in value:
value["overview_page"] = pages.get(value["overview_page"])

# Any pages in featured, other, and column links.
for link in self.link_iterator(value):
if "page" in link:
link["page"] = pages.get(link["page"])

def link_iterator(self, value):
return chain(
value.get("featured_links") or [],
value.get("other_links") or [],
chain(
*(
(column.get("links") or [])
for column in (value.get("columns") or [])
)
),
)


class MenuStreamBlock(blocks.StreamBlock):
submenu = SubmenuBlock()
8 changes: 5 additions & 3 deletions cfgov/mega_menu/tests/test_frontend_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ def test_conversion_database_queries(self):

self.menu.refresh_from_db()

# We expect to see two queries here:
# We expect to see four queries here:
#
# 1. Wagtail's site root lookup.
# 2. Single query to retrieve all pages at once.
with self.assertNumQueries(2):
# 2. Single query to retrieve all LinkBlock page links.
# 3. Single query to retrieve all overview page links.
# 4. Single query to retrieve all other page links.
with self.assertNumQueries(4):
self.do_conversion(self.menu, request)

def test_conversion_output(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements/wagtail.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wagtail==2.15.5
wagtail==2.16.3
4 changes: 2 additions & 2 deletions test/cypress/integration/admin/admin-helpers.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export class AdminPage {
}

openNavigationTab(name) {
cy.get('.nav-main').contains(name).click();
cy.get('.sidebar-menu-item').contains(name).click();
}

selectSubMenu(name) {
cy.get('.menu-item').contains(name).click();
cy.get('.sidebar-menu-item--in-sub-menu').contains(name).click();
}

openRegulations() {
Expand Down

0 comments on commit c991948

Please sign in to comment.