This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug 1253269: Add fixaccount command
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
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
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
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!') |