Skip to content

Commit 5395206

Browse files
fixup! Remove inactive person links
1 parent 5c3a25b commit 5395206

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

ynr/apps/candidatebot/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import re
23

34
import pypandoc
@@ -196,3 +197,10 @@ def add_theyworkforyou_id(self, twfy_id):
196197
value = f"https://www.theyworkforyou.com/mp/{twfy_id}/"
197198
internal_id = f"uk.org.publicwhip/person/{twfy_id}"
198199
self.edit_field("theyworkforyou", value, internal_id=internal_id)
200+
201+
def remove_person_identifier(self, identifier):
202+
with contextlib.suppress(PersonIdentifier.DoesNotExist):
203+
self.person.get_all_identifiers.remove(identifier)
204+
self.person.save()
205+
identifier.delete()
206+
return self.person

ynr/apps/people/management/commands/remove_inactive_person_links.py renamed to ynr/apps/candidatebot/management/commands/candidatebot_remove_inactive_person_links.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from urllib.parse import urlparse
33

44
import requests
5+
from candidatebot.helpers import CandidateBot
56
from django.core.management.base import BaseCommand
67
from people.models import Person
78
from popolo.models import Membership
@@ -22,6 +23,12 @@ class Command(BaseCommand):
2223
Test and remove inactive or dead links from Person objects.
2324
"""
2425

26+
def add_arguments(self, parser):
27+
parser.add_argument(
28+
"--person-id",
29+
help="Person ID to test",
30+
)
31+
2532
def handle(self, *args, **options):
2633
"""
2734
Iterate over all Person objects and check if the
@@ -66,9 +73,8 @@ def handle(self, *args, **options):
6673
]
6774
)
6875
# delete the identifier from the person identifiers
69-
identifier.person.get_all_identifiers.remove(identifier)
70-
identifier.delete()
71-
identifier.person.save()
76+
bot = CandidateBot(person.pk, ignore_errors=True)
77+
bot.remove_person_identifier(identifier)
7278
print(
73-
f"Deleted {identifier.value_type}:{identifier.value} from {person.name}"
79+
f"Candidatebot deleted {identifier.value_type}:{identifier.value} from {person.name}"
7480
)

ynr/apps/candidates/tests/test_remove_inactive_person_identifiers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ def test_remove_inactive_person_identifiers(self):
5656
self.person.get_all_identifiers[1].value,
5757
"http://www.conservatives.com/about/our-team/example.com",
5858
)
59-
call_command("remove_inactive_person_links")
59+
60+
call_command(
61+
"candidatebot_remove_inactive_person_links",
62+
"--person-id",
63+
self.person.id,
64+
)
6065
self.person.refresh_from_db()
6166
self.assertEqual(len(self.person.get_all_identifiers), 1)
6267
self.assertEqual(

0 commit comments

Comments
 (0)