Example #1
0
def _page_urls_for_sites(page: Page, sites: Tuple[Site],
                         cache_target: Page) -> Set[Tuple[Site, str, str]]:
    urls = set()
    for site in sites:

        # use a `HttpRequest` to influence the return value
        request = get_dummy_request(site=site)
        # reuse cached site root paths if available
        if hasattr(cache_target, "_wagtail_cached_site_root_paths"):
            request._wagtail_cached_site_root_paths = (
                cache_target._wagtail_cached_site_root_paths)

        url_parts = page.get_url_parts(request)
        if url_parts is None:
            continue
        site_id, root_url, page_path = url_parts

        if page_path:
            for route_path in page.get_route_paths():
                normalized_route_path = Redirect.normalise_page_route_path(
                    route_path)
                old_path = Redirect.normalise_path(
                    page_path.rstrip("/") + (normalized_route_path or "/"))
                urls.add((site, old_path, normalized_route_path))

        # copy cached site root paths to `cache_target` to retain benefits
        cache_target._wagtail_cached_site_root_paths = (
            request._wagtail_cached_site_root_paths)

    return urls
Example #2
0
    def test_non_standard_port(self):
        site = Site.objects.first()
        site.hostname = "other.example.com"
        site.port = 8888
        site.save()

        request = get_dummy_request(site=site)
        self.assertEqual(request.get_host(), "other.example.com:8888")
Example #3
0
    def test_golden_path(self):
        # the page we'll be triggering the change for here is...
        test_subject = self.event_index

        # identify 'draft' pages in this section
        drafts = test_subject.get_descendants().not_live()
        self.assertEqual(len(drafts), 4)

        # gather urls for 'live' pages in this branch
        request = get_dummy_request()
        branch_urls = []
        for page in (test_subject.get_descendants(
                inclusive=True).live().specific(defer=True).iterator()):
            main_url = page.get_url(request).rstrip("/")
            branch_urls.extend(main_url + path.rstrip("/")
                               for path in page.get_cached_paths())

        self.trigger_page_slug_changed_signal(test_subject)

        # gather all of the redirects that were created
        redirects = Redirect.objects.all()
        redirect_page_ids = {r.redirect_page_id for r in redirects}

        # a redirect should have been created for the page itself
        self.assertIn(test_subject.id, redirect_page_ids)

        # as well as for each of its live descendants
        for descendant in test_subject.get_descendants().live().iterator():
            self.assertIn(descendant.id, redirect_page_ids)

        # but not for the draft pages
        for page in drafts:
            self.assertNotIn(page.id, redirect_page_ids)

        # for each redirect created:
        for r in redirects:
            # the old_path accurately matches a url from this branch
            self.assertIn(r.old_path, branch_urls)
            # the automatically_created flag should have been set to True
            self.assertTrue(r.automatically_created)