-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8762342
commit 5ea0157
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
ynr/apps/people/management/commands/reformat_person_indentifiers.py
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from urllib.parse import urlparse | ||
|
||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
from people.models import Person | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
""" | ||
Iterate over all PersonIdentifier objects and reformat the LinkedIn urls as needed. | ||
""" | ||
|
||
people = Person.objects.all() | ||
for person in people: | ||
person_identifiers = person.get_all_identifiers | ||
linkedin_person_identifiers = [ | ||
identifier | ||
for identifier in person_identifiers | ||
if identifier.value_type == "linkedin_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 | ||
|
||
call_command("identify_inactive_person_links") |