def autocreate_redirects_on_page_move( instance: Page, url_path_after: str, url_path_before: str, **kwargs, ) -> None: if not getattr(settings, "WAGTAILREDIRECTS_AUTO_CREATE", True): return None if url_path_after == url_path_before: # Redirects are not needed for a page 'reorder' return None # NB: create_redirects expects specific instances instance = instance.specific # Simulate an 'old_page' by copying the specific instance and resetting # the in-memory `url_path` value to what it was before the move page_old = type(instance)() page_old.__dict__.update(instance.__dict__) page_old.url_path = url_path_before # This value is used to prevent creation redirects that link # from one site to another new_site_ids = set(item.site_id for item in instance._get_relevant_site_root_paths( cache_object=instance)) # Determine sites to create redirects for sites = Site.objects.exclude(root_page=instance).filter(id__in=[ item.site_id for item in page_old._get_relevant_site_root_paths( cache_object=instance) if item.site_id in new_site_ids ]) create_redirects(page=instance, page_old=page_old, sites=sites)
def autocreate_redirects_on_slug_change(instance_before: Page, instance: Page, **kwargs): # NB: `page_slug_changed` provides specific page instances, # so we do not need to 'upcast' them for create_redirects here if not getattr(settings, "WAGTAILREDIRECTS_AUTO_CREATE", True): return None # Determine sites to create redirects for sites = Site.objects.filter(id__in=[ option.site_id for option in instance._get_relevant_site_root_paths( cache_object=instance) ]).exclude(root_page=instance) create_redirects(page=instance, page_old=instance_before, sites=sites)