Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importer: support reparenting pages by following wp_post_parent #176

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions wagtail_wordpress_import/importers/wordpress.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import json
import uuid
from datetime import datetime

from wagtail import VERSION as WAGTAIL_VERSION
Expand Down Expand Up @@ -141,6 +142,17 @@ def run(self, *args, **kwargs):
page, body
) # if the body streamfield is invalid, exit with a ValueError

# If the page has a parent then we might need to reparent it later
if cleaned_data["wp_post_meta"]["wp_post_parent"] != 0:
# If the page has not been created yet or the parent is still the root
# then store the current slug for later and create a uuid for now.
#
# Note we need to use a uuid as there might be duplicate slugs
# between the pages as they are not supposed to siblings right now
if not page.id or page.get_parent() == self.parent_page_obj:
cleaned_data["wp_post_meta"]["original_slug"] = cleaned_data["slug"]
cleaned_data["slug"] = str(uuid.uuid4())

page.import_wordpress_data(cleaned_data)

if item.get("wp:status") == "draft":
Expand Down Expand Up @@ -213,6 +225,8 @@ def run(self, *args, **kwargs):

self.connect_richtext_page_links(self.imported_pages)

self.reparent_pages(self.imported_pages)

"""Run all hooks in settings.WORDPRESS_IMPORT_HOOKS_ITEMS_TO_CACHE"""
for hook, actions in getattr(
settings, "WORDPRESS_IMPORT_HOOKS_ITEMS_TO_CACHE", {}
Expand Down Expand Up @@ -329,6 +343,26 @@ def connect_page_categories(self, page, category_model, item):

page.categories = page_categories

def reparent_pages(self, imported_pages):
"""
Reparent any pages that have a wp_post_parent and update their slug
"""
for page in imported_pages:
wp_post_parent_id = page.wp_post_meta.get("wp_post_parent", 0)
original_slug = page.wp_post_meta.get("original_slug", None)

# If this page has a parent and still has an original slug then reparent it
if wp_post_parent_id != 0 and original_slug is not None:
wp_post_parent_page = page.__class__.objects.get(wp_post_id = wp_post_parent_id)
# Check that the parent isn't the same already
if page.get_parent() != wp_post_parent_page:
page.move(wp_post_parent_page, pos="last-child")
page = page.specific_class.objects.get(pk=page.pk)
# Restore the correct slug
page.slug = original_slug
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that if there are pages and posts under a wp_post_parent there could be issues as we don't consider the slug clash resolution in #175

del page.wp_post_meta["original_slug"]
page.save()


def default_prefilters():
return [
Expand Down