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

Strip diacritics when comparing usernames in deduplicate_users command... #39

Merged
merged 1 commit into from
Apr 10, 2014
Merged
Changes from all 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
13 changes: 11 additions & 2 deletions apps/users/management/commands/deduplicate_users.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from datetime import datetime
import traceback
import unicodedata as unidata

from django.core.management.base import BaseCommand
from django.db import connection, transaction


MAX_DUPS = 16

def strip_diacritics(s):
if not isinstance(s, unicode):
s = s.decode('utf-8')

return u''.join(c for c in unidata.normalize('NFKD', s)
if not unidata.combining(c))

class Command(BaseCommand):
"""
Renames user accounts whose usernames would conflict with other
Expand Down Expand Up @@ -55,7 +63,7 @@ def handle(self, *args, **options):
# conflicts.
cursor.execute('''
CREATE TEMPORARY TABLE usernames (
username varchar(255) PRIMARY KEY,
username varchar(255) PRIMARY KEY COLLATE utf8_general_ci,
user_id int(11) UNSIGNED
) DEFAULT CHARSET = utf8
''')
Expand Down Expand Up @@ -111,7 +119,7 @@ def handle(self, *args, **options):
patterns)
self.log('%d rows returned' % cursor.rowcount)

usernames = [r[0] for r in cursor]
usernames = [strip_diacritics(r[0]) for r in cursor]

finally:
cursor.execute('DROP TEMPORARY TABLE usernames')
Expand All @@ -132,6 +140,7 @@ def unique_username(username, lower):
new_name = None
for id, username, lower in rows:
try:
lower = strip_diacritics(lower)
new_name = unique_username(username, lower)
self.log('Renaming %d "%s" -> "%s"' % (id, username, new_name))

Expand Down