-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #723 from WesternFriend/page-importer
Page importer
- Loading branch information
Showing
3 changed files
with
60 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |