Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
refactor: Improved handling of entity and person data
Browse files Browse the repository at this point in the history
  • Loading branch information
hepplerj committed May 14, 2024
1 parent 3fe04fb commit 525ea49
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 59 deletions.
82 changes: 45 additions & 37 deletions postcards/management/commands/fix_entity_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core.management.base import BaseCommand
from django.db import transaction

from postcards.models import Object, Person
from postcards.models import Person

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,45 +72,41 @@ def load_data(self, file_path, sheet_name=None):
)
)

# item_number = str(row["item number"])
sender_entity_name = str(row["entitiy"])
sender_entity_type = str(
row["sender correspondence type"]
).lower()
addressee_entity_name = str(row["addressee entity"])
addressee_entity_type = str(
row["addresse correspondence type"]
).lower()

# Get the sender's first_name and last_name
sender_first_name = str(row["sender first name"])
sender_last_name = str(row["sender last name"])
sender_entity_name = str(row["entitiy"])
sender_entity_type = str(row["sender correspondence type"])

sender = None
if sender_first_name or sender_last_name:
sender = Person.objects.filter(
first_name=sender_first_name, last_name=sender_last_name
).first()
self.stdout.write(
f"Found sender {sender}, {sender_first_name}, {sender_last_name} -- now updating their record:"
)
# if addressee_first_name and addressee_last_name are None, we then create an entity
# If both first_name and last_name are None, but entity_name exists, create a new Person object
if (
sender_first_name is None
and sender_last_name is None
and not sender_entity_name
sender_first_name == "None"
and sender_last_name == "None"
and sender_entity_name
):
sender = Person.objects.create(
entity_name=sender_entity_name,
entity_type=sender_entity_type,
)
# If either first_name or last_name exists, try to get the Person object
elif sender_first_name != "None" or sender_last_name != "None":
sender = Person.objects.filter(
first_name=sender_first_name, last_name=sender_last_name
).first()

# If there is no Person object and entity_name exists, create a new Person object
if not sender and sender_entity_name:
sender = Person.objects.create(
first_name=sender_first_name,
last_name=sender_last_name,
first_name=sender_first_name
if sender_first_name != "None"
else None,
last_name=sender_last_name
if sender_last_name != "None"
else None,
entity_name=sender_entity_name,
entity_type=sender_entity_type,
)
# If there is a Person object, update the entity_name and entity_type
elif sender:
sender.entity_name = sender_entity_name
sender.entity_type = sender_entity_type
Expand All @@ -126,30 +122,42 @@ def load_data(self, file_path, sheet_name=None):
# Get the addressee's first_name and last_name
addressee_first_name = str(row["addressee first name"])
addressee_last_name = str(row["addressee last name"])
addressee_entity_name = str(row["addressee entity"])
addressee_entity_type = str(row["addresse correspondence type"])

addressee = None
if addressee_first_name or addressee_last_name:
addressee = Person.objects.filter(
first_name=addressee_first_name,
last_name=addressee_last_name,
).first()
# if addressee_first_name and addressee_last_name are None, we then create an entity
# If both first_name and last_name are None, but entity_name exists, create a new Person object
if (
not addressee
and not addressee_first_name
and not addressee_last_name
addressee_first_name == "None"
and addressee_last_name == "None"
and addressee_entity_name
):
addressee = Person.objects.create(
entity_name=addressee_entity_name,
entity_type=addressee_entity_type,
)
if not addressee and addressee_entity_name:
addressee = Person.objects.create(
# If either first_name or last_name exists, try to get the Person object
elif (
addressee_first_name != "None"
or addressee_last_name != "None"
):
addressee = Person.objects.filter(
first_name=addressee_first_name,
last_name=addressee_last_name,
).first()

# If there is no Person object and entity_name exists, create a new Person object
if not addressee and addressee_entity_name:
addressee = Person.objects.create(
first_name=addressee_first_name
if addressee_first_name != "None"
else None,
last_name=addressee_last_name
if addressee_last_name != "None"
else None,
entity_name=addressee_entity_name,
entity_type=addressee_entity_type,
)
# If there is a Person object, update the entity_name and entity_type
elif addressee:
addressee.entity_name = addressee_entity_name
addressee.entity_type = addressee_entity_type
Expand Down
103 changes: 83 additions & 20 deletions postcards/management/commands/load_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def load_data(self, file_path, sheet_name=None):
)

# Extract data from the row
addressee_correspodnence_type = (
row["addresse correspondence type"] or "Person"
addressee_correspodnence_type = str(
row["addresse correspondence type"]
).lower()
sender_correspondence_type = (
row["sender correspondence type"] or "Person"
sender_correspondence_type = str(
row["sender correspondence type"]
).lower()
item_number = str(row["item number"])
collection_location = str(row["location in collection"])
Expand Down Expand Up @@ -129,14 +129,15 @@ def load_data(self, file_path, sheet_name=None):
reason_for_return_translated = str(
row["reason for return (english)"]
)
translated = str(row["translated"]).lower()
# if reason_for_return_original or reason_for_return_translated includes the string "NA"
# then set the value to None
if "NA" in reason_for_return_original:
reason_for_return_original = None
if "NA" in reason_for_return_translated:
reason_for_return_translated = None

regime_censor = str(row["censor"])
regime_censor = str(row["censor"]).lower()

# Create or update Persons
addressee_title = str(row["addressee title"])
Expand Down Expand Up @@ -221,28 +222,59 @@ def load_data(self, file_path, sheet_name=None):
postmark_2_location = None

addressee = None
if addressee_first_name or addressee_last_name:
if (
addressee_first_name == "None"
and addressee_last_name == "None"
and addressee_entity_name
):
addressee = Person.objects.create(
entity_name=addressee_entity_name,
entity_type=addressee_correspodnence_type,
title=addressee_title,
house_number=addressee_house_number,
street=addressee_street,
location=Location.objects.filter(
town_city=addressee_town_city,
province_state=addressee_province_state,
).first(),
)
elif (
addressee_first_name != "None"
or addressee_last_name != "None"
):
addressee = Person.objects.filter(
first_name=addressee_first_name,
last_name=addressee_last_name,
).first()

if not addressee and addressee_entity_name:
addressee = Person.objects.create(
first_name=addressee_first_name
if addressee_first_name != "None"
else None,
last_name=addressee_last_name
if addressee_last_name != "None"
else None,
entity_name=addressee_entity_name,
entity_type=addressee_correspodnence_type,
title=addressee_title,
first_name=addressee_first_name,
last_name=addressee_last_name,
entity_name=addressee_entity_name,
house_number=addressee_house_number,
street=addressee_street,
location=Location.objects.filter(
town_city=addressee_town_city,
province_state=addressee_province_state,
country=addressee_country,
).first(),
)
elif addressee:
addressee.entity_name = addressee_entity_name
addressee.entity_type = addressee_correspodnence_type
addressee.title = addressee_title
addressee.house_number = addressee_house_number
addressee.street = addressee_street
addressee.location = Location.objects.filter(
town_city=addressee_town_city,
province_state=addressee_province_state,
).first()
addressee.save()

self.stdout.write(
Expand All @@ -252,17 +284,39 @@ def load_data(self, file_path, sheet_name=None):
)

sender = None
if sender_first_name or sender_last_name:
if (
sender_first_name == "None"
and sender_last_name == "None"
and sender_entity_name
):
sender = Person.objects.create(
entity_name=sender_entity_name,
entity_type=sender_correspondence_type,
title=sender_title,
house_number=sender_house_number,
street=sender_street,
location=Location.objects.filter(
town_city=sender_town_city,
province_state=sender_province_state,
country=sender_country,
).first(),
)
elif sender_first_name != "None" or sender_last_name != "None":
sender = Person.objects.filter(
first_name=sender_first_name, last_name=sender_last_name
).first()

if not sender and sender_entity_name:
sender = Person.objects.create(
first_name=sender_first_name
if sender_first_name != "None"
else None,
last_name=sender_last_name
if sender_last_name != "None"
else None,
entity_name=sender_entity_name,
entity_type=sender_correspondence_type,
title=sender_title,
first_name=sender_first_name,
last_name=sender_last_name,
entity_name=sender_entity_name,
house_number=sender_house_number,
street=sender_street,
location=Location.objects.filter(
Expand All @@ -273,8 +327,16 @@ def load_data(self, file_path, sheet_name=None):
)
elif sender:
sender.entity_name = sender_entity_name
sender.entity_type = sender_correspondence_type
sender.title = sender_title
sender.house_number = sender_house_number
sender.street = sender_street
sender.location = Location.objects.filter(
town_city=sender_town_city,
province_state=sender_province_state,
country=sender_country,
).first()
sender.save()

self.stdout.write(
self.style.SUCCESS(
f"Creating sender {sender_first_name} {sender_last_name}"
Expand Down Expand Up @@ -328,6 +390,7 @@ def load_data(self, file_path, sheet_name=None):
addressee_name=addressee,
sender_name=sender,
letter_type=letter_type,
translated=translated,
date_of_correspondence=date_of_correspondence,
other=other,
public_notes=public_notes,
Expand Down Expand Up @@ -359,10 +422,10 @@ def load_data(self, file_path, sheet_name=None):
)

print(
f"Initial postmark_1_date: {postmark_1_date} (type: {type(postmark_1_date)})",
f"Initial postmark_1_location: {postmark_1_location} (type: {type(postmark_1_location)})",
f"Initial postmark_2_date: {postmark_2_date} (type: {type(postmark_2_date)})",
f"Initial postmark_2_location: {postmark_2_location} (type: {type(postmark_2_location)})",
f"|- Initial postmark_1_date: {postmark_1_date} (type: {type(postmark_1_date)})\n",
f"|- Initial postmark_1_location: {postmark_1_location} (type: {type(postmark_1_location)})\n",
f"|- Initial postmark_2_date: {postmark_2_date} (type: {type(postmark_2_date)})\n",
f"|- Initial postmark_2_location: {postmark_2_location} (type: {type(postmark_2_location)})",
)
# Create or update postmarks and associate them with the object
if postmark_1_location:
Expand Down Expand Up @@ -393,7 +456,7 @@ def load_data(self, file_path, sheet_name=None):
postmark_1_date = None

print(
f"Final postmark_1_date: {postmark_1_date} (type: {type(postmark_1_date)})"
f"|-> Final postmark_1_date: {postmark_1_date} (type: {type(postmark_1_date)})"
)
postmark_1, _ = Postmark.objects.get_or_create(
date=postmark_1_date,
Expand Down
7 changes: 5 additions & 2 deletions postcards/management/commands/update_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@


class Command(BaseCommand):
help = "Update the names in the Person model from the Object model."
help = "Removes None values from names."

def handle(slef, *args, **options):
people = Person.objects.filter(
Q(first_name="None") | Q(last_name="None") | Q(title="None")
Q(first_name="None")
| Q(last_name="None")
| Q(title="None")
| Q(entity_name="None")
)

for person in people:
Expand Down

0 comments on commit 525ea49

Please sign in to comment.