Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean person names #2471

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ynr/apps/candidatebot/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
69 changes: 69 additions & 0 deletions ynr/apps/people/management/commands/people_clean_person_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from candidatebot.helpers import CandidateBot
from django.core.management.base import BaseCommand
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": " ",
}


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."

def handle(self, *args, **options):
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)
chris48s marked this conversation as resolved.
Show resolved Hide resolved
symroe marked this conversation as resolved.
Show resolved Hide resolved
other_name.save()
bot = CandidateBot(person_id=person.pk, update=True)
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:
existing_name = other_name.content_object.other_names.filter(
name=other_name.name.replace(char, replace)
symroe marked this conversation as resolved.
Show resolved Hide resolved
)
if existing_name:
other_name.delete()
chris48s marked this conversation as resolved.
Show resolved Hide resolved
continue
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()
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"
)