Skip to content

Commit

Permalink
Merge pull request #725 from WesternFriend/add-redirects-to-all-impor…
Browse files Browse the repository at this point in the history
…t-scripts

Add redirects to import scripts
  • Loading branch information
brylie authored Jun 19, 2023
2 parents f0927c8 + c115196 commit 2001279
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 deletions.
61 changes: 44 additions & 17 deletions content_migration/management/import_archive_issues_handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
from tqdm import tqdm
from content_migration.management.shared import parse_csv_file

from content_migration.management.shared import (
parse_csv_file,
create_permanent_redirect,
)
from magazine.models import ArchiveIssue, DeepArchiveIndexPage


def create_or_update_archive_issue(
issue_data: dict,
deep_archive_index_page: DeepArchiveIndexPage,
) -> ArchiveIssue:
issue_exists = ArchiveIssue.objects.filter(
internet_archive_identifier=issue_data["internet_archive_identifier"]
).exists()

if issue_exists:
# update existing issue
archive_issue = ArchiveIssue.objects.get(
internet_archive_identifier=issue_data["internet_archive_identifier"]
)
archive_issue.title = issue_data["title"]
archive_issue.publication_date = issue_data["publication_date"]
archive_issue.western_friend_volume = issue_data["western_friend_volume"]

archive_issue.save()
else:
archive_issue = ArchiveIssue(
title=issue_data["title"],
publication_date=issue_data["publication_date"],
internet_archive_identifier=issue_data["internet_archive_identifier"],
western_friend_volume=issue_data["western_friend_volume"],
)

# Add issue to site page hiererchy
deep_archive_index_page.add_child(instance=archive_issue)
deep_archive_index_page.save()

return archive_issue


def handle_import_archive_issues(file_name: str) -> None:
# Get the only instance of Deep Archive Index Page
deep_archive_index_page = DeepArchiveIndexPage.objects.get()
Expand All @@ -15,20 +50,12 @@ def handle_import_archive_issues(file_name: str) -> None:
desc="Archive issues",
unit="row",
):
issue_exists = ArchiveIssue.objects.filter(
internet_archive_identifier=issue["internet_archive_identifier"]
).exists()

if issue_exists:
continue

import_issue = ArchiveIssue(
title=issue["title"],
publication_date=issue["publication_date"],
internet_archive_identifier=issue["internet_archive_identifier"],
western_friend_volume=issue["western_friend_volume"],
archive_issue = create_or_update_archive_issue(
issue_data=issue,
deep_archive_index_page=deep_archive_index_page,
)

# Add issue to site page hiererchy
deep_archive_index_page.add_child(instance=import_issue)
deep_archive_index_page.save()
create_permanent_redirect(
redirect_path=issue["url_path"],
redirect_entity=archive_issue,
)
12 changes: 10 additions & 2 deletions content_migration/management/import_magazine_articles_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)

from content_migration.management.shared import (
create_permanent_redirect,
get_existing_magazine_author_from_db,
parse_csv_file,
parse_media_blocks,
Expand Down Expand Up @@ -68,8 +69,10 @@ def assign_article_to_issue(
related_issue = MagazineIssue.objects.get(
drupal_node_id=drupal_issue_node_id,
)
except ObjectDoesNotExist:
print("Can't find issue: ", drupal_issue_node_id)
except ObjectDoesNotExist as error:
error_message = f"Could not find issue from Drupal ID: { drupal_issue_node_id }"
logger.error(error_message)
raise ObjectDoesNotExist(error_message) from error

related_issue.add_child(
instance=article,
Expand Down Expand Up @@ -139,3 +142,8 @@ def handle_import_magazine_articles(file_name: str) -> None:
article.tags.add(keyword)

article.save()

create_permanent_redirect(
redirect_path=row["url_path"],
redirect_entity=article,
)
16 changes: 12 additions & 4 deletions content_migration/management/import_magazine_authors_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
Person,
PersonIndexPage,
)
from content_migration.management.shared import parse_csv_file
from content_migration.management.shared import (
parse_csv_file,
)

logging.basicConfig(
filename="import_log_magazine_authors.log",
Expand All @@ -24,7 +26,7 @@
def create_meeting(
author: dict,
meeting_index_page: MeetingIndexPage,
) -> None:
) -> Meeting:
# check if meeting already exists
# if it does, update it
# if it doesn't, create it
Expand Down Expand Up @@ -56,11 +58,13 @@ def create_meeting(

meeting_index_page.save()

return meeting


def create_organization(
author: dict,
organization_index_page: OrganizationIndexPage,
) -> None:
) -> Organization:
organization_exists = Organization.objects.filter(
drupal_author_id=author["drupal_author_id"]
).exists()
Expand Down Expand Up @@ -89,11 +93,13 @@ def create_organization(

organization_index_page.save()

return organization


def create_person(
author: dict,
person_index_page: PersonIndexPage,
) -> None:
) -> Person:
person_exists = Person.objects.filter(
drupal_author_id=author["drupal_author_id"]
).exists()
Expand Down Expand Up @@ -124,6 +130,8 @@ def create_person(

person_index_page.save()

return person


def import_author_records(authors_list: list[dict]) -> None:
meeting_index_page = MeetingIndexPage.objects.get()
Expand Down
10 changes: 9 additions & 1 deletion content_migration/management/import_magazine_issues_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from django.utils.timezone import make_aware
from tqdm import tqdm
from wagtail.images.models import Image
from content_migration.management.shared import parse_csv_file
from content_migration.management.shared import (
create_permanent_redirect,
parse_csv_file,
)

from magazine.models import MagazineIndexPage, MagazineIssue

Expand Down Expand Up @@ -56,3 +59,8 @@ def handle_import_magazine_issues(file_name: str) -> None:
# Add issue to site page hiererchy
magazine_index_page.add_child(instance=import_issue)
magazine_index_page.save()

create_permanent_redirect(
redirect_path=issue["url_path"],
redirect_entity=import_issue,
)
7 changes: 7 additions & 0 deletions content_migration/management/import_memorials_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DuplicateContactError,
)
from content_migration.management.shared import (
create_permanent_redirect,
get_existing_magazine_author_from_db,
parse_csv_file,
)
Expand Down Expand Up @@ -128,3 +129,9 @@ def handle_import_memorials(file_name: str) -> None:

else:
memorial.save()

# Create a permanent redirect from the old URL to the new one
create_permanent_redirect(
redirect_path=memorial_data["url_path"],
redirect_entity=memorial,
)

0 comments on commit 2001279

Please sign in to comment.