kopia lustrzana https://github.com/wagtail/wagtail
Allow ignoring a page when finding an available slug
This is useful for when using this function to update a pages slugpull/6426/head
rodzic
b817e869bf
commit
e508cbc9c9
|
@ -125,7 +125,9 @@ class TestFindAvailableSlug(TestCase):
|
|||
self.root_page = Page.objects.get(depth=1)
|
||||
self.home_page = Page.objects.get(depth=2)
|
||||
|
||||
self.root_page.add_child(instance=Page(title="Second homepage", slug="home-1"))
|
||||
self.second_home_page = self.root_page.add_child(
|
||||
instance=Page(title="Second homepage", slug="home-1")
|
||||
)
|
||||
|
||||
def test_find_available_slug(self):
|
||||
with self.assertNumQueries(1):
|
||||
|
@ -140,6 +142,12 @@ class TestFindAvailableSlug(TestCase):
|
|||
|
||||
self.assertEqual(slug, "home-2")
|
||||
|
||||
def test_find_available_slug_ignore_page_id(self):
|
||||
with self.assertNumQueries(1):
|
||||
slug = find_available_slug(self.root_page, "home", ignore_page_id=self.second_home_page.id)
|
||||
|
||||
self.assertEqual(slug, "home-1")
|
||||
|
||||
|
||||
@override_settings(
|
||||
USE_I18N=True,
|
||||
|
|
|
@ -176,7 +176,7 @@ class InvokeViaAttributeShortcut:
|
|||
return method(name)
|
||||
|
||||
|
||||
def find_available_slug(parent, requested_slug):
|
||||
def find_available_slug(parent, requested_slug, ignore_page_id=None):
|
||||
"""
|
||||
Finds an available slug within the specified parent.
|
||||
|
||||
|
@ -187,12 +187,17 @@ def find_available_slug(parent, requested_slug):
|
|||
- 'requested-slug-2'
|
||||
|
||||
And so on, until an available slug is found.
|
||||
|
||||
The `ignore_page_id` keyword argument is useful for when you are updating a page,
|
||||
you can pass the page being updated here so the page's current slug is not
|
||||
treated as in use by another page.
|
||||
"""
|
||||
existing_slugs = set(
|
||||
parent.get_children()
|
||||
.filter(slug__startswith=requested_slug)
|
||||
.values_list("slug", flat=True)
|
||||
)
|
||||
pages = parent.get_children().filter(slug__startswith=requested_slug)
|
||||
|
||||
if ignore_page_id:
|
||||
pages = pages.exclude(id=ignore_page_id)
|
||||
|
||||
existing_slugs = set(pages.values_list("slug", flat=True))
|
||||
slug = requested_slug
|
||||
number = 1
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue