# Generated by Django 2.2 on 2019-05-02 19:23

from django.db import migrations


def forwards(apps, schema_editor):
    # The unique id for a Salesforce social account
    # needs to include both the org id and user id
    SocialAccount = apps.get_model("socialaccount", "SocialAccount")
    for acct in SocialAccount.objects.all():
        data = acct.extra_data
        # update the uid
        acct.uid = "{}/{}".format(data["organization_id"], data["user_id"])
        acct.save()
        # make sure the user has the right user id
        if acct.user.username != data["preferred_username"]:
            acct.user.username = data["preferred_username"]
            acct.user.save()


def backwards(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [("api", "0070_blank_allowedlist_orgtype")]

    operations = [migrations.RunPython(forwards, backwards)]
