Skip to content

Commit

Permalink
Merge pull request #723 from WesternFriend/page-importer
Browse files Browse the repository at this point in the history
Page importer
  • Loading branch information
brylie committed Jun 17, 2023
2 parents 949a1f8 + 60e3961 commit 9d380ab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions content_migration/management/commands/import_pages.py
Original file line number Diff line number Diff line change
@@ -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
64 changes: 57 additions & 7 deletions content_migration/management/import_pages_handler.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit 9d380ab

Please sign in to comment.