diff --git a/ynr/apps/candidatebot/helpers.py b/ynr/apps/candidatebot/helpers.py index 46bf2ac64..7f457d697 100644 --- a/ynr/apps/candidatebot/helpers.py +++ b/ynr/apps/candidatebot/helpers.py @@ -1,3 +1,4 @@ +import contextlib import re import pypandoc @@ -196,3 +197,9 @@ def add_theyworkforyou_id(self, twfy_id): value = f"https://www.theyworkforyou.com/mp/{twfy_id}/" internal_id = f"uk.org.publicwhip/person/{twfy_id}" self.edit_field("theyworkforyou", value, internal_id=internal_id) + + def remove_person_identifier(self, identifier): + with contextlib.suppress(PersonIdentifier.DoesNotExist): + identifier.delete() + self.person.invalidate_identifier_cache() + return self.person diff --git a/ynr/apps/people/management/commands/remove_inactive_person_links.py b/ynr/apps/candidatebot/management/commands/candidatebot_remove_inactive_person_links.py similarity index 89% rename from ynr/apps/people/management/commands/remove_inactive_person_links.py rename to ynr/apps/candidatebot/management/commands/candidatebot_remove_inactive_person_links.py index e20ee3771..fd19c4e61 100644 --- a/ynr/apps/people/management/commands/remove_inactive_person_links.py +++ b/ynr/apps/candidatebot/management/commands/candidatebot_remove_inactive_person_links.py @@ -2,6 +2,7 @@ from urllib.parse import urlparse import requests +from candidatebot.helpers import CandidateBot from django.core.management.base import BaseCommand from people.models import Person from popolo.models import Membership @@ -66,9 +67,9 @@ def handle(self, *args, **options): ] ) # delete the identifier from the person identifiers + bot = CandidateBot(person.pk, ignore_errors=True) identifier.person.get_all_identifiers.remove(identifier) - identifier.delete() - identifier.person.save() + bot.remove_person_identifier(identifier) print( - f"Deleted {identifier.value_type}:{identifier.value} from {person.name}" + f"Candidatebot deleted {identifier.value_type}:{identifier.value} from {person.name}" ) diff --git a/ynr/apps/candidates/tests/test_remove_inactive_person_identifiers.py b/ynr/apps/candidates/tests/test_remove_inactive_person_identifiers.py index 0aaa962d3..e344c7cba 100644 --- a/ynr/apps/candidates/tests/test_remove_inactive_person_identifiers.py +++ b/ynr/apps/candidates/tests/test_remove_inactive_person_identifiers.py @@ -56,7 +56,7 @@ def test_remove_inactive_person_identifiers(self): self.person.get_all_identifiers[1].value, "http://www.conservatives.com/about/our-team/example.com", ) - call_command("remove_inactive_person_links") + call_command("candidatebot_remove_inactive_person_links") self.person.refresh_from_db() self.assertEqual(len(self.person.get_all_identifiers), 1) self.assertEqual(