From bf0f076d8304810614356d6579245c7b8b78ccca Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 12 Nov 2024 15:29:32 +0000 Subject: [PATCH 1/6] Teach CandidateBot how to edit names --- ynr/apps/candidatebot/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ynr/apps/candidatebot/helpers.py b/ynr/apps/candidatebot/helpers.py index 543cfd8ba..57789125a 100644 --- a/ynr/apps/candidatebot/helpers.py +++ b/ynr/apps/candidatebot/helpers.py @@ -116,8 +116,8 @@ def edit_field( if not ignore_edit: self.edits_made = True else: - if field_name == "biography": - self.person.biography = field_value + if field_name in ["biography", "name"]: + setattr(self.person, field_name, field_value) self.edits_made = True def clean_email(self, value): From e34528e3e76588190207e5ddb16117ae740fa55f Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 12 Nov 2024 15:44:02 +0000 Subject: [PATCH 2/6] Replace unwanted chars in person names --- .../commands/people_clean_person_names.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ynr/apps/people/management/commands/people_clean_person_names.py diff --git a/ynr/apps/people/management/commands/people_clean_person_names.py b/ynr/apps/people/management/commands/people_clean_person_names.py new file mode 100644 index 000000000..cdf268fd2 --- /dev/null +++ b/ynr/apps/people/management/commands/people_clean_person_names.py @@ -0,0 +1,35 @@ +from candidatebot.helpers import CandidateBot +from django.core.management.base import BaseCommand +from people.models import Person +from popolo.models import OtherName + + +class Command(BaseCommand): + help = "Replaces chars in person names that we don't want to be there." + + def handle(self, *args, **options): + chars_to_replace = {"\xa0": " ", "\u200e": ""} + + for char, replace in chars_to_replace.items(): + qs = Person.objects.filter(name__contains=char).only("pk", "name") + for person in qs: + for other_name in person.other_names.all(): + other_name.name = other_name.name.replace(char, replace) + other_name.save() + bot = CandidateBot(person_id=person.pk, update=True) + bot.edit_field("name", person.name.replace(char, replace)) + bot.save( + f"Replacing {repr(char)} with {repr(replace)} in person name" + ) + + qs = OtherName.objects.filter(name__contains=char) + for other_name in qs: + print(other_name) + existing_name = other_name.content_object.other_names.filter( + name=other_name.name.replace(char, replace) + ) + if existing_name: + other_name.delete() + continue + other_name.name = other_name.name.replace(char, replace) + other_name.save() From b698c21c9ed11c2946f997ee0549fce9d17b58be Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 26 Nov 2024 13:52:37 +0000 Subject: [PATCH 3/6] fixup! Replace unwanted chars in person names --- .../commands/people_clean_person_names.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ynr/apps/people/management/commands/people_clean_person_names.py b/ynr/apps/people/management/commands/people_clean_person_names.py index cdf268fd2..6e69c1c61 100644 --- a/ynr/apps/people/management/commands/people_clean_person_names.py +++ b/ynr/apps/people/management/commands/people_clean_person_names.py @@ -3,13 +3,31 @@ from people.models import Person from popolo.models import OtherName +chars_to_replace = { + " ": " ", + "\xa0": " ", + "\u1680": " ", + "\u2000": " ", + "\u2001": " ", + "\u2002": " ", + "\u2003": " ", + "\u2004": " ", + "\u2005": " ", + "\u2006": " ", + "\u2007": " ", + "\u2008": " ", + "\u2009": " ", + "\u200a": " ", + "\u202f": " ", + "\u205f": " ", + "\u3000": " ", +} + class Command(BaseCommand): help = "Replaces chars in person names that we don't want to be there." def handle(self, *args, **options): - chars_to_replace = {"\xa0": " ", "\u200e": ""} - for char, replace in chars_to_replace.items(): qs = Person.objects.filter(name__contains=char).only("pk", "name") for person in qs: From 751414840a9ef6572d57f3b2d61fe9b16247d31b Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 26 Nov 2024 15:12:09 +0000 Subject: [PATCH 4/6] fixup! Replace unwanted chars in person names --- .../commands/people_clean_person_names.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ynr/apps/people/management/commands/people_clean_person_names.py b/ynr/apps/people/management/commands/people_clean_person_names.py index 6e69c1c61..e2f22260e 100644 --- a/ynr/apps/people/management/commands/people_clean_person_names.py +++ b/ynr/apps/people/management/commands/people_clean_person_names.py @@ -4,7 +4,6 @@ from popolo.models import OtherName chars_to_replace = { - " ": " ", "\xa0": " ", "\u1680": " ", "\u2000": " ", @@ -24,6 +23,10 @@ } +def clean_name(name): + return " ".join(name.split()).strip() + + class Command(BaseCommand): help = "Replaces chars in person names that we don't want to be there." @@ -35,19 +38,25 @@ def handle(self, *args, **options): other_name.name = other_name.name.replace(char, replace) other_name.save() bot = CandidateBot(person_id=person.pk, update=True) - bot.edit_field("name", person.name.replace(char, replace)) + bot.edit_field( + "name", clean_name(person.name.replace(char, replace)) + ) bot.save( f"Replacing {repr(char)} with {repr(replace)} in person name" ) qs = OtherName.objects.filter(name__contains=char) for other_name in qs: - print(other_name) existing_name = other_name.content_object.other_names.filter( name=other_name.name.replace(char, replace) ) if existing_name: other_name.delete() continue - other_name.name = other_name.name.replace(char, replace) - other_name.save() + other_name.name = clean_name( + other_name.name.replace(char, replace) + ) + if other_name.name == other_name.content_object.name: + other_name.delete() + else: + other_name.save() From df9f1bc29d0fe4c0ca22a44758a63b8982c6adde Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 26 Nov 2024 15:23:42 +0000 Subject: [PATCH 5/6] fixup! Replace unwanted chars in person names --- .../management/commands/people_clean_person_names.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ynr/apps/people/management/commands/people_clean_person_names.py b/ynr/apps/people/management/commands/people_clean_person_names.py index e2f22260e..24fb92264 100644 --- a/ynr/apps/people/management/commands/people_clean_person_names.py +++ b/ynr/apps/people/management/commands/people_clean_person_names.py @@ -60,3 +60,10 @@ def handle(self, *args, **options): other_name.delete() else: other_name.save() + bot = CandidateBot( + person_id=other_name.content_object.pk, update=True + ) + bot.edits_made = True + bot.save( + f"Replacing {repr(char)} with {repr(replace)} in person other name" + ) From 1653635872c32cb6503916d35c872902f91f775c Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Wed, 27 Nov 2024 17:07:54 +0000 Subject: [PATCH 6/6] fixup! Replace unwanted chars in person names --- .../commands/people_clean_person_names.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/ynr/apps/people/management/commands/people_clean_person_names.py b/ynr/apps/people/management/commands/people_clean_person_names.py index 24fb92264..bdb96cfb7 100644 --- a/ynr/apps/people/management/commands/people_clean_person_names.py +++ b/ynr/apps/people/management/commands/people_clean_person_names.py @@ -23,7 +23,8 @@ } -def clean_name(name): +def replace_char(name, char, replace): + name = name.repalce(char, replace) return " ".join(name.split()).strip() @@ -35,12 +36,12 @@ def handle(self, *args, **options): qs = Person.objects.filter(name__contains=char).only("pk", "name") for person in qs: for other_name in person.other_names.all(): - other_name.name = other_name.name.replace(char, replace) + other_name.name = replace_char( + other_name.name, char, replace + ) other_name.save() bot = CandidateBot(person_id=person.pk, update=True) - bot.edit_field( - "name", clean_name(person.name.replace(char, replace)) - ) + bot.edit_field("name", replace_char(person.name, char, replace)) bot.save( f"Replacing {repr(char)} with {repr(replace)} in person name" ) @@ -48,14 +49,12 @@ def handle(self, *args, **options): qs = OtherName.objects.filter(name__contains=char) for other_name in qs: existing_name = other_name.content_object.other_names.filter( - name=other_name.name.replace(char, replace) + name=replace_char(other_name.name, char, replace) ) if existing_name: other_name.delete() continue - other_name.name = clean_name( - other_name.name.replace(char, replace) - ) + other_name.name = replace_char(other_name.name, char, replace) if other_name.name == other_name.content_object.name: other_name.delete() else: