Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Fix bug 1253269: Add fixaccount command
Browse files Browse the repository at this point in the history
This command fixes an account in an anonymized database so that you
can use it to log in with in a dev environment.
  • Loading branch information
willkg committed Mar 3, 2016
1 parent af672d5 commit 556b84a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions kuma/core/management/commands/fixaccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError

from allauth.account.models import EmailAddress
from allauth.socialaccount.models import SocialAccount


class Command(BaseCommand):
help = 'Reconnects an MDN account (Persona) to an email address'

def add_arguments(self, parser):
parser.add_argument('username', nargs=1,
help='MDN account username.')
parser.add_argument('email', nargs=1,
help='Email address to connect.')

def handle(self, *args, **options):
username = options['username'][0]
email = options['email'][0]

User = get_user_model()

try:
user = User.objects.get(username=username)

except User.DoesNotExist:
raise CommandError('User %s does not exist.' % username)

if user.email != email:
self.stdout.write('Fixing email address in auth_user record.')
user.email = email
user.save()

# Fix the django-allauth EmailAddress record
emailaddress = EmailAddress.objects.get(user=user)
if emailaddress.email != email:
self.stdout.write('Fixing email address in account_emailaddress '
'record.')
emailaddress.change(None, email, confirm=False)
emailaddress.verified = True
emailaddress.save()

try:
sa = SocialAccount.objects.get(user=user)
except SocialAccount.DoesNotExist:
self.stdout.write('Creating a socialaccount_socialaccount record.')
sa = SocialAccount.objects.create(user=user, provider='Persona', uid=email)
sa.save()

self.stdout.write('Done!')

0 comments on commit 556b84a

Please sign in to comment.