Created
July 29, 2012 18:52
-
-
Save surjikal/3201103 to your computer and use it in GitHub Desktop.
Script to restore the username hashes off django-email-as-usernames after ./manage.py dumpdata/loaddata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.management import setup_environ | |
import settings | |
setup_environ(settings) | |
###################################################################################### | |
from django.contrib.auth.models import User | |
from django.core.validators import email_re | |
from emailusernames.utils import _email_to_username as create_username_hash_from_email | |
def is_email(email): | |
return bool(email_re.match(email)) | |
def migrate_user(user): | |
user.username = create_username_hash_from_email(user.email) | |
user.save() | |
print 'Initiating migration process:' | |
for user in User.objects.all(): | |
if not user.email: | |
print 'WARNING: User #%d does not have an email address. Skipping.' % user.pk | |
elif not user.username: | |
print 'WARNING: User #%d does not have a username. Skipping.' % user.pk | |
elif not is_email(user.username): | |
print 'User #%d does not have an email as his username (%s). Skipping.' % (user.pk, user.username) | |
else: | |
migrate_user(user) | |
print 'Migrated %s -> %s' % (user.username, user.email) | |
print 'Migration complete.\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment