from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType
from django.db import migrations

from nodeconductor.quotas import models as quotas_models

from .. import models


def delete_backup_storage_quota_from_tenant(apps, schema_editor):
    tenant_content_type = ContentType.objects.get_for_model(models.Tenant)
    quota_name = 'backup_storage'
    quotas_models.Quota.objects.filter(name=quota_name, content_type=tenant_content_type).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(delete_backup_storage_quota_from_tenant),
    ]

from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType
from django.db import migrations

from nodeconductor.quotas import models as quotas_models

from .. import models


def cleanup_tenant_quotas(apps, schema_editor):
    for obj in models.Tenant.objects.all():
        quotas_names = models.Tenant.QUOTAS_NAMES + [f.name for f in models.Tenant.get_quotas_fields()]
        obj.quotas.exclude(name__in=quotas_names).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(cleanup_tenant_quotas),
    ]

Replace quota deletion with cleanup
Replace quota deletion with cleanup [WAL-433]

  from __future__ import unicode_literals
  
  from django.contrib.contenttypes.models import ContentType
  from django.db import migrations
  
  from nodeconductor.quotas import models as quotas_models
  
  from .. import models
  
  
- def delete_backup_storage_quota_from_tenant(apps, schema_editor):
-     tenant_content_type = ContentType.objects.get_for_model(models.Tenant)
-     quota_name = 'backup_storage'
-     quotas_models.Quota.objects.filter(name=quota_name, content_type=tenant_content_type).delete()
+ def cleanup_tenant_quotas(apps, schema_editor):
+     for obj in models.Tenant.objects.all():
+         quotas_names = models.Tenant.QUOTAS_NAMES + [f.name for f in models.Tenant.get_quotas_fields()]
+         obj.quotas.exclude(name__in=quotas_names).delete()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('openstack', '0030_subnet_dns_nameservers'),
      ]
  
      operations = [
-         migrations.RunPython(delete_backup_storage_quota_from_tenant),
+         migrations.RunPython(cleanup_tenant_quotas),
      ]
  
Replace quota deletion with cleanup
## Code Before:
from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType
from django.db import migrations

from nodeconductor.quotas import models as quotas_models

from .. import models


def delete_backup_storage_quota_from_tenant(apps, schema_editor):
    tenant_content_type = ContentType.objects.get_for_model(models.Tenant)
    quota_name = 'backup_storage'
    quotas_models.Quota.objects.filter(name=quota_name, content_type=tenant_content_type).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(delete_backup_storage_quota_from_tenant),
    ]

## Instruction:
Replace quota deletion with cleanup
## Code After:
from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType
from django.db import migrations

from nodeconductor.quotas import models as quotas_models

from .. import models


def cleanup_tenant_quotas(apps, schema_editor):
    for obj in models.Tenant.objects.all():
        quotas_names = models.Tenant.QUOTAS_NAMES + [f.name for f in models.Tenant.get_quotas_fields()]
        obj.quotas.exclude(name__in=quotas_names).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(cleanup_tenant_quotas),
    ]
