Skip to content

Commit

Permalink
Reformats usernames into urls
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Jul 18, 2024
1 parent 5ea0157 commit 9c60a28
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions ynr/apps/people/management/commands/reformat_person_indentifiers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from urllib.parse import urlparse

from django.core.management import call_command
from django.core.management.base import BaseCommand
from people.models import Person
Expand All @@ -8,33 +6,33 @@
class Command(BaseCommand):
def handle(self, *args, **options):
"""
Iterate over all PersonIdentifier objects and reformat the LinkedIn urls as needed.
Iterate over all PersonIdentifier objects and reformat values as needed.
"""

people = Person.objects.all()
for person in people:
person_identifiers = person.get_all_identifiers
linkedin_person_identifiers = [
person_identifiers = [
identifier
for identifier in person_identifiers
if identifier.value_type == "linkedin_url"
if identifier.value_type == "instagram_url"
]
# if the parsed_url netloc is uk.linkedin.com,
# then this is a redirect and we need to reformat it to www.linkedin.com
for identifier in linkedin_person_identifiers:
parsed_url = urlparse(identifier.value)

if parsed_url.netloc == "uk.linkedin.com":
# remove the uk. from the netloc
identifier.value = identifier.value.replace("uk.", "www.")
identifier.save()
self.stdout.write(
f"Reformatted LinkedIn URL for {person.name} to {identifier.value}"
)
else:
self.stdout.write(
f"LinkedIn URL for {person.name} is already in the correct format."
)
pass
for identifier in person_identifiers:
if identifier.value.startswith("http"):
self.stdout.write(
f"URL for {person.name} is already in the correct format."
)
pass
else:
if identifier.value.startswith("@"):
username = identifier.value[1:]
identifier.value = (
f"https://www.instagram.com/{username}"
)
identifier.save()
self.stdout.write(
f"Reformatted URL for {person.name} to {identifier.value}"
)

call_command("identify_inactive_person_links")

0 comments on commit 9c60a28

Please sign in to comment.