Skip to content

Commit

Permalink
Find and identify iactive person identifier urls
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed May 23, 2024
1 parent 4c2b67f commit 64d77fc
Showing 1 changed file with 49 additions and 0 deletions.
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

0 comments on commit 64d77fc

Please sign in to comment.