# Generated by Django 2.2.11 on 2020-04-28 18:23

from django.db import migrations


def filter_dict(d, keep_keys):
    return {k: v for k, v in d.items() if k in keep_keys}


def forwards(apps, schema_editor):
    SocialAccount = apps.get_model("socialaccount", "SocialAccount")

    # remove unneeded data from SocialAccount extra_data
    for account in SocialAccount.objects.iterator():
        if isinstance(account.extra_data, dict):
            account.extra_data = filter_dict(
                account.extra_data,
                {
                    "organization_id",
                    "user_id",
                    "instance_url",
                    "language",
                    "organization_details",
                    "preferred_username",
                },
            )
            if isinstance(account.extra_data.get("organization_details"), dict):
                account.extra_data["organization_details"] = filter_dict(
                    account.extra_data["organization_details"],
                    {
                        "IsSandbox",
                        "Name",
                        "OrganizationType",
                        "SignupCountryIsoCode",
                        "TrialExpirationDate",
                    },
                )
            account.save()


def backwards(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("api", "0085_update_socialapps"),
    ]

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

# Generated by Django 2.2.11 on 2020-04-28 18:23

from django.db import migrations


def filter_dict(d, keep_keys):
    return {k: v for k, v in d.items() if k in keep_keys}


def forwards(apps, schema_editor):
    SocialAccount = apps.get_model("socialaccount", "SocialAccount")

    # remove unneeded data from SocialAccount extra_data
    for account in SocialAccount.objects.iterator():
        if isinstance(account.extra_data, dict):
            account.extra_data = filter_dict(
                account.extra_data,
                {
                    "organization_id",
                    "user_id",
                    "instance_url",
                    "language",
                    "organization_details",
                    "preferred_username",
                },
            )
            if isinstance(account.extra_data.get("organization_details"), dict):
                account.extra_data["organization_details"] = filter_dict(
                    account.extra_data["organization_details"],
                    {
                        "IsSandbox",
                        "Name",
                        "OrganizationType",
                        "SignupCountryIsoCode",
                        "TrialExpirationDate",
                    },
                )
            account.save()


def backwards(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("api", "0085_update_socialapps"),
    ]

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

# Generated by Django 2.2.11 on 2020-04-28 18:23 # remove unneeded data from SocialAccount extra_data