Skip to content

Commit

Permalink
Extract get_breadcrumbs_items_for_page from page_breadcrumbs template…
Browse files Browse the repository at this point in the history
… tag
  • Loading branch information
laymonage authored and lb- committed Mar 13, 2024
1 parent c844c54 commit a388e57
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
27 changes: 8 additions & 19 deletions wagtail/admin/templatetags/wagtailadmin_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
get_valid_next_url_from_request,
)
from wagtail.admin.views.bulk_action.registry import bulk_action_registry
from wagtail.admin.views.pages.utils import get_breadcrumbs_items_for_page
from wagtail.admin.widgets import Button, ButtonWithDropdown, PageListingButton
from wagtail.coreutils import (
accepts_kwarg,
Expand All @@ -55,7 +56,6 @@
Page,
PageViewRestriction,
)
from wagtail.permissions import page_permission_policy
from wagtail.telepath import JSContext
from wagtail.users.utils import get_gravatar_url
from wagtail.utils.deprecation import RemovedInWagtail70Warning
Expand Down Expand Up @@ -91,26 +91,15 @@ def page_breadcrumbs(
):
user = context["request"].user

# find the closest common ancestor of the pages that this user has direct explore permission
# (i.e. add/edit/publish/lock) over; this will be the root of the breadcrumb
cca = page_permission_policy.explorable_root_instance(user)
if not cca:
return {"items": Page.objects.none()}

pages = (
page.get_ancestors(inclusive=include_self)
.descendant_of(cca, inclusive=True)
.specific()
items = get_breadcrumbs_items_for_page(
page,
user,
url_name,
url_root_name,
include_self,
querystring_value,
)

items = []
for page in pages:
if page.is_root() and url_root_name:
url = reverse(url_root_name)
else:
url = reverse(url_name, args=(page.id,))
items.append({"url": url + querystring_value, "label": get_latest_str(page)})

if trailing_breadcrumb_title:
items.append({"label": trailing_breadcrumb_title})

Expand Down
39 changes: 38 additions & 1 deletion wagtail/admin/views/pages/utils.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
from django.urls import reverse

# Retain backwards compatibility for imports
from wagtail.admin.utils import get_valid_next_url_from_request # noqa: F401
from wagtail.admin.utils import ( # noqa: F401
get_latest_str,
get_valid_next_url_from_request,
)
from wagtail.permissions import page_permission_policy


def get_breadcrumbs_items_for_page(
page,
user,
url_name="wagtailadmin_explore",
root_url_name="wagtailadmin_explore_root",
include_self=True,
querystring_value="",
):
# find the closest common ancestor of the pages that this user has direct explore permission
# (i.e. add/edit/publish/lock) over; this will be the root of the breadcrumb
cca = page_permission_policy.explorable_root_instance(user)
if not cca:
return []

pages = (
page.get_ancestors(inclusive=include_self)
.descendant_of(cca, inclusive=True)
.specific()
)

items = []
for page in pages:
if page.is_root() and root_url_name:
url = reverse(root_url_name)
else:
url = reverse(url_name, args=(page.id,))
items.append({"url": url + querystring_value, "label": get_latest_str(page)})

return items

0 comments on commit a388e57

Please sign in to comment.