-
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.
Find and identify iactive person identifier urls
- Loading branch information
1 parent
4c2b67f
commit 64d77fc
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
ynr/apps/people/management/commands/remove_inactive_person_links.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,49 @@ | ||
import requests | ||
from django.core.management.base import BaseCommand | ||
from people.models import Person | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Test and remove inactive or dead links from Person objects. | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Iterate over all Person objects and check if the | ||
person identifier urls return a 200 status code. | ||
""" | ||
inactive_links = [] | ||
people = Person.objects.all() | ||
for person in people: | ||
person_identifiers = person.get_all_identifiers | ||
person_identifiers = [ | ||
identifier | ||
for identifier in person_identifiers | ||
if identifier.value.startswith("http") | ||
] | ||
|
||
if person_identifiers: | ||
for identifier in person_identifiers: | ||
resp = None | ||
try: | ||
resp = requests.head(identifier.value).status_code | ||
except requests.exceptions.RequestException as e: | ||
self.stdout.write( | ||
f"Request exception: {e} for {person.name}" | ||
) | ||
pass | ||
if resp in [404, 500, 502, 503, 504]: | ||
self.stdout.write( | ||
f"Status code: {resp} for {person.name} {identifier.value}" | ||
) | ||
inactive_links.append(identifier) | ||
for i, ident in enumerate(person_identifiers): | ||
if ident == identifier: | ||
person_identifiers.pop(i) | ||
print( | ||
"Inactive links found for {person.name} and deleted" | ||
) | ||
break | ||
else: | ||
continue |