from allauth.socialaccount.models import SocialAccount, SocialApp
from django.core.management.base import BaseCommand
from social_django.models import UserSocialAuth


class Command(BaseCommand):
    help = 'Migrate allauth social logins to social auth'

    def add_arguments(self, parser):
        parser.add_argument('--apps', action='store_true', dest='apps',
                            help='Print social app keys and secrets')
        parser.add_argument('--accounts', action='store_true', dest='accounts',
                            help='Migrate accounts')

    def migrate_accounts(self):
        self.stdout.write(self.style.SUCCESS('Going through all SocialAccount objects...'))
        # Retrieve existing objects
        providers = {}
        for usa in UserSocialAuth.objects.all():
            provider = providers.setdefault(usa.provider, {})
            provider[usa.user_id] = usa

        for sa in SocialAccount.objects.all():
            provider = providers.setdefault(sa.provider, {})
            if sa.user_id in provider:
                continue
            provider[sa.user_id] = UserSocialAuth.objects.create(
                user=sa.user,
                provider=sa.provider,
                uid=sa.uid,
                extra_data=sa.extra_data,
            )
            self.stdout.write(self.style.SUCCESS('Added. (provider: {}, uid: {})'.format(sa.provider, sa.uid)))

        self.stdout.write(self.style.SUCCESS('Done.'))

    def migrate_apps(self):
        for app in SocialApp.objects.all():
            app_id = app.provider.upper()
            print("SOCIAL_AUTH_%s_KEY = '%s'" % (app_id, app.client_id))
            print("SOCIAL_AUTH_%s_SECRET = '%s'" % (app_id, app.secret))
            print()

    def handle(self, *args, **options):
        if options['apps']:
            self.migrate_apps()
        if options['accounts']:
            self.migrate_accounts()

@@ -111,16 +111,50 @@
 Command%0A
+from django.db import transaction%0A
 from soc
@@ -911,28 +911,24 @@
 ovider%5Busa.u
-ser_
 id%5D = usa%0A%0A 
@@ -926,16 +926,84 @@
  = usa%0A%0A
+        with transaction.atomic():%0A            create_objs = %5B%5D%0A    
         
@@ -1033,32 +1033,36 @@
 .objects.all():%0A
+    
             prov
@@ -1122,19 +1122,19 @@
     
+   
+ 
 if sa.u
-ser_
 id i
@@ -1145,16 +1145,20 @@
 ovider:%0A
+    
         
@@ -1177,32 +1177,42 @@
 inue%0A           
+     usa =
  provider%5Bsa.use
@@ -1213,12 +1213,8 @@
 sa.u
-ser_
 id%5D 
@@ -1233,25 +1233,14 @@
 Auth
-.objects.create(%0A
+(%0A    
     
@@ -1273,32 +1273,36 @@
                 
+    
 provider=sa.prov
@@ -1323,16 +1323,20 @@
         
+ 
+   
 uid=sa.u
@@ -1339,16 +1339,20 @@
 sa.uid,%0A
+    
         
@@ -1397,18 +1397,66 @@
         
+    
 )%0A
+                create_objs.append(usa)%0A    
         
@@ -1504,21 +1504,20 @@
 'Add
-ed. (
+ing 
 provider
 : %7B%7D
@@ -1516,17 +1516,16 @@
 ider
-:
  %7B%7D, uid
 : %7B%7D
@@ -1524,13 +1524,11 @@
  uid
-:
  %7B%7D
-)
 '.fo
@@ -1554,16 +1554,327 @@
 a.uid)))
+%0A                if len(create_objs) == 1000:%0A                    self.stdout.write(self.style.SUCCESS('Saving'))%0A                    UserSocialAuth.objects.bulk_create(create_objs)%0A                    create_objs = %5B%5D%0A%0A            if create_objs:%0A                UserSocialAuth.objects.bulk_create(create_objs)
 %0A%0A      

Fix bug in social account migrator and speed it up