Darrel O'Pry 2024-04-27 12:40:00 +00:00 zatwierdzone przez GitHub
commit a4a03708a1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 32 dodań i 6 usunięć

Wyświetl plik

@ -2086,7 +2086,15 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
"wagtail_serve", args=(self.url_path[len(root_path) :],)
)
except NoReverseMatch:
return (site_id, None, None)
"""
wagtail_serve is not mounted, this is most likely a headless Wagtail,
use the path rather than processsing through wagtail_serve -1 maintains
the leading slash needed to create a root relative URL Path.
TODO: review whether additional work is needed to support Multisite and i18n
url generation in headless Wagtail.
"""
page_path = self.url_path[len(root_path) - 1 :]
# Remove the trailing slash from the URL reverse generates if
# WAGTAIL_APPEND_SLASH is False and we're not trying to serve

Wyświetl plik

@ -473,11 +473,22 @@ class TestRouting(TestCase):
default_site = Site.objects.get(is_default_site=True)
homepage = Page.objects.get(url_path="/home/")
# The page should not be routable because wagtail_serve is not registered
# However it is still associated with a site
self.assertEqual(homepage.get_url_parts(), (default_site.id, None, None))
self.assertIsNone(homepage.full_url)
self.assertIsNone(homepage.url)
# for now headless installations will return the url relative to the default site.
# additional work probably needs to be done to enable multisite in headless modes
# without this page links in richtext blocks have href=None
self.assertEqual(
homepage.get_url_parts(), (default_site.id, "http://localhost", "/")
)
self.assertEqual(homepage.full_url, "http://localhost/")
self.assertEqual(homepage.url, "/")
events_page = Page.objects.get(url_path="/home/events/")
self.assertEqual(
events_page.get_url_parts(),
(default_site.id, "http://localhost", "/events/"),
)
self.assertEqual(events_page.full_url, "http://localhost/events/")
self.assertEqual(events_page.url, "/events/")
def test_request_routing(self):
homepage = Page.objects.get(url_path="/home/")

Wyświetl plik

@ -23,6 +23,13 @@ class TestPageLinktypeHandler(TestCase):
)
self.assertEqual(result, '<a href="/events/christmas/">')
@override_settings(ROOT_URLCONF="wagtail.test.headless_urls")
def test_expand_db_attributes_headless(self):
result = PageLinkHandler.expand_db_attributes(
{"id": Page.objects.get(url_path="/home/events/christmas/").id}
)
self.assertEqual(result, '<a href="/events/christmas/">')
def test_expand_db_attributes_page_does_not_exist(self):
result = PageLinkHandler.expand_db_attributes({"id": 0})
self.assertEqual(result, "<a>")