diff --git a/content_migration/management/commands/import_all_content.py b/content_migration/management/commands/import_all_content.py index 65b1c01df..5701d3622 100644 --- a/content_migration/management/commands/import_all_content.py +++ b/content_migration/management/commands/import_all_content.py @@ -19,8 +19,8 @@ def handle(self, *args: tuple, **options: dict) -> None: call_command("import_molly_wingate_blog") call_command("import_news") call_command("import_online_worship") + call_command("import_pages") # TODO: add remaining importers - # - pages # - CiviCRM # - contacts, # - addresses, diff --git a/content_migration/management/commands/import_pages.py b/content_migration/management/commands/import_pages.py index f66ffcbb9..5f8b9794d 100644 --- a/content_migration/management/commands/import_pages.py +++ b/content_migration/management/commands/import_pages.py @@ -1,16 +1,13 @@ from django.core.management.base import BaseCommand -from content_migration.management.constants import ( - IMPORT_FILENAMES, - LOCAL_MIGRATION_DATA_DIRECTORY, -) from content_migration.management.import_pages_handler import handle_import_pages +from content_migration.management.shared import construct_import_file_path class Command(BaseCommand): help = "Import all Drupal pages" def handle(self, *args: tuple, **options: dict[str, str]) -> None: - file_name = LOCAL_MIGRATION_DATA_DIRECTORY + IMPORT_FILENAMES["pages"] + file_name = construct_import_file_path("pages") handle_import_pages(file_name) # type: ignore diff --git a/content_migration/management/import_pages_handler.py b/content_migration/management/import_pages_handler.py index 335f12a1e..adfb7d165 100644 --- a/content_migration/management/import_pages_handler.py +++ b/content_migration/management/import_pages_handler.py @@ -1,21 +1,71 @@ from tqdm import tqdm -from content_migration.management.shared import parse_csv_file - +from content_migration.management.shared import ( + parse_csv_file, + parse_media_blocks, + parse_media_string_to_list, +) +from wagtail.models import Page from home.models import HomePage -from news.models import NewsIndexPage +from wf_pages.models import WfPage +from content_migration.management.shared import ( + create_permanent_redirect, + parse_body_blocks, +) + + +def import_page_item(page_data: dict[str, str], home_page: HomePage) -> Page: + """Create or update a page instance from page data.""" + + page_exists = WfPage.objects.filter( + drupal_node_id=page_data["drupal_node_id"], + ).exists() + + page_body_blocks = parse_body_blocks(page_data["body"]) + + if page_data["media"] != "": + page_media_blocks = parse_media_blocks( + parse_media_string_to_list(page_data["media"]) + ) + else: + page_media_blocks = None + + if page_media_blocks: + page_body_blocks.extend(page_media_blocks) + + if page_exists: + page = WfPage.objects.get(drupal_node_id=page_data["drupal_node_id"]) + page.title = page_data["title"] + page.body = page_body_blocks + + page.save() + else: + page = home_page.add_child( + instance=WfPage( + title=page_data["title"], + drupal_node_id=page_data["drupal_node_id"], + body=page_body_blocks, + ) + ) + + return page def handle_import_pages(file_name: str) -> None: # Get references to relevant index pages - HomePage.objects.get() - NewsIndexPage.objects.get() + home_page = HomePage.objects.get() pages = parse_csv_file(file_name) - for page in tqdm( + for page_data in tqdm( pages, total=len(pages), desc="Pages", unit="row", ): - pass + page = import_page_item(page_data, home_page) + + # create permanent redirect from old path to new page + create_permanent_redirect( + redirect_path=page_data["url_path"], + redirect_entity=page, + )