# Generated by Django 3.2 on 2023-07-10 03:03

from django.db import migrations, models


def copy_scopes(apps, schema_editor):
    AuthorizedClient = apps.get_model('oauth2', 'AuthorizedClient')
    Grant = apps.get_model('oauth2', 'Grant')
    AccessToken = apps.get_model('oauth2', 'AccessToken')

    TempScope = apps.get_model('oauth2', 'TempScope')
    Scope = apps.get_model('oauth2', 'Scope')
    scopes = dict()
    for scope in Scope.objects.all():
        scopes[scope.name] = TempScope.objects.create(
            name=scope.name,
            description=scope.description
        )

    for client in AuthorizedClient.objects.all():
        for scope_name in client.scope.all().values_list('name', flat=True):
            client.temp_scope.add(scopes[scope_name].pk)

    for grant in Grant.objects.all():
        for scope_name in grant.scope.all().values_list('name', flat=True):
            grant.temp_scope.add(scopes[scope_name].pk)

    for token in AccessToken.objects.all():
        for scope_name in token.scope.all().values_list('name', flat=True):
            token.temp_scope.add(scopes[scope_name].pk)


def reverse_temp_scopes(apps, schema_editor):
    AuthorizedClient = apps.get_model('oauth2', 'AuthorizedClient')
    Grant = apps.get_model('oauth2', 'Grant')
    AccessToken = apps.get_model('oauth2', 'AccessToken')

    TempScope = apps.get_model('oauth2', 'TempScope')
    Scope = apps.get_model('oauth2', 'Scope')

    scopes = dict()
    for scope in TempScope.objects.all():
        scopes[scope.name] = Scope.objects.create(
            name=scope.name,
            description=scope.description,
        )

    for client in AuthorizedClient.objects.all():
        for scope_name in client.temp_scope.all().values_list('name', flat=True):
            client.scope.add(scopes[scope_name])

    for grant in Grant.objects.all():
        for scope_name in grant.temp_scope.all().values_list('name', flat=True):
            grant.scope.add(scopes[scope_name])

    for token in AccessToken.objects.all():
        for scope_name in token.temp_scope.all().values_list('name', flat=True):
            token.scope.add(scopes[scope_name])


class Migration(migrations.Migration):

    dependencies = [
        ('oauth2', '0003_public_client_options'),
    ]

    operations = [
        migrations.CreateModel(
            name='TempScope',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50)),
                ('description', models.CharField(blank=True, default='', max_length=256)),
            ],
            options={
                'db_table': 'oauth2_temp_scope',
            },
        ),
        migrations.AddField(
            model_name='accesstoken',
            name='temp_scope',
            field=models.ManyToManyField(to='oauth2.TempScope'),
        ),
        migrations.AddField(
            model_name='authorizedclient',
            name='temp_scope',
            field=models.ManyToManyField(to='oauth2.TempScope'),
        ),
        migrations.AddField(
            model_name='grant',
            name='temp_scope',
            field=models.ManyToManyField(to='oauth2.TempScope'),
        ),
        migrations.RunPython(copy_scopes, reverse_temp_scopes),
    ]
