# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-11-04 20:35
from __future__ import unicode_literals

from django.db import migrations, models


def move_quota_around(apps, schema_editor):
    Identity = apps.get_model('core', "Identity")
    Quota = apps.get_model('core', "Quota")
    for ident in Identity.objects.all():
        membership = ident.identity_memberships.first()
        if membership:
            quota = membership.quota
        else:
            q = Quota()
            q_kwargs = q.__dict__
            q_kwargs.pop('_state')
            q_kwargs.pop('id')
            q_kwargs.pop('uuid')
            db_quota = Quota.objects.filter(**q_kwargs).first()
            if db_quota:
                quota = db_quota
            else:
                new_quota = Quota.objects.create(**q_kwargs)
                quota = new_quota
        ident.quota = quota
        ident.save()

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0067_identity_quota'),
    ]

    operations = [
        migrations.RunPython(
            move_quota_around
        ),
    ]
