
from django.db import migrations

from ..utils import get_countries_without_shipping_zone


def assign_countries_in_default_shipping_zone(apps, schema_editor):
    ShippingZone = apps.get_model("shipping", "ShippingZone")
    qs = ShippingZone.objects.filter(default=True)
    if qs.exists():
        default_zone = qs[0]
        if not default_zone.countries:
            default_zone.countries = get_countries_without_shipping_zone()
            default_zone.save(update_fields=["countries"])


class Migration(migrations.Migration):

    dependencies = [
        ("shipping", "0017_django_price_2"),
    ]

    operations = [
        migrations.RunPython(
            assign_countries_in_default_shipping_zone, migrations.RunPython.noop
        )
    ]


from django.db import migrations
from django_countries import countries


def get_countries_without_shipping_zone(ShippingZone):
    """Return countries that are not assigned to any shipping zone."""
    covered_countries = set()
    for zone in ShippingZone.objects.all():
        covered_countries.update({c.code for c in zone.countries})
    return (country[0] for country in countries if country[0] not in covered_countries)


def assign_countries_in_default_shipping_zone(apps, schema_editor):
    ShippingZone = apps.get_model("shipping", "ShippingZone")
    qs = ShippingZone.objects.filter(default=True)
    if qs.exists():
        default_zone = qs[0]
        if not default_zone.countries:
            default_zone.countries = get_countries_without_shipping_zone(ShippingZone)
            default_zone.save(update_fields=["countries"])


class Migration(migrations.Migration):

    dependencies = [
        ("shipping", "0017_django_price_2"),
    ]

    operations = [
        migrations.RunPython(
            assign_countries_in_default_shipping_zone, migrations.RunPython.noop
        )
    ]

Move utility function to migration
Move utility function to migration

  
  from django.db import migrations
+ from django_countries import countries
  
- from ..utils import get_countries_without_shipping_zone
+ 
+ def get_countries_without_shipping_zone(ShippingZone):
+     """Return countries that are not assigned to any shipping zone."""
+     covered_countries = set()
+     for zone in ShippingZone.objects.all():
+         covered_countries.update({c.code for c in zone.countries})
+     return (country[0] for country in countries if country[0] not in covered_countries)
  
  
  def assign_countries_in_default_shipping_zone(apps, schema_editor):
      ShippingZone = apps.get_model("shipping", "ShippingZone")
      qs = ShippingZone.objects.filter(default=True)
      if qs.exists():
          default_zone = qs[0]
          if not default_zone.countries:
-             default_zone.countries = get_countries_without_shipping_zone()
+             default_zone.countries = get_countries_without_shipping_zone(ShippingZone)
              default_zone.save(update_fields=["countries"])
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ("shipping", "0017_django_price_2"),
      ]
  
      operations = [
          migrations.RunPython(
              assign_countries_in_default_shipping_zone, migrations.RunPython.noop
          )
      ]
  
Move utility function to migration
## Code Before:

from django.db import migrations

from ..utils import get_countries_without_shipping_zone


def assign_countries_in_default_shipping_zone(apps, schema_editor):
    ShippingZone = apps.get_model("shipping", "ShippingZone")
    qs = ShippingZone.objects.filter(default=True)
    if qs.exists():
        default_zone = qs[0]
        if not default_zone.countries:
            default_zone.countries = get_countries_without_shipping_zone()
            default_zone.save(update_fields=["countries"])


class Migration(migrations.Migration):

    dependencies = [
        ("shipping", "0017_django_price_2"),
    ]

    operations = [
        migrations.RunPython(
            assign_countries_in_default_shipping_zone, migrations.RunPython.noop
        )
    ]

## Instruction:
Move utility function to migration
## Code After:

from django.db import migrations
from django_countries import countries


def get_countries_without_shipping_zone(ShippingZone):
    """Return countries that are not assigned to any shipping zone."""
    covered_countries = set()
    for zone in ShippingZone.objects.all():
        covered_countries.update({c.code for c in zone.countries})
    return (country[0] for country in countries if country[0] not in covered_countries)


def assign_countries_in_default_shipping_zone(apps, schema_editor):
    ShippingZone = apps.get_model("shipping", "ShippingZone")
    qs = ShippingZone.objects.filter(default=True)
    if qs.exists():
        default_zone = qs[0]
        if not default_zone.countries:
            default_zone.countries = get_countries_without_shipping_zone(ShippingZone)
            default_zone.save(update_fields=["countries"])


class Migration(migrations.Migration):

    dependencies = [
        ("shipping", "0017_django_price_2"),
    ]

    operations = [
        migrations.RunPython(
            assign_countries_in_default_shipping_zone, migrations.RunPython.noop
        )
    ]
