# Generated by Django 3.2.14 on 2022-07-13 11:03


from django.apps import apps as registry
from django.db import migrations
from django.db.models.signals import post_migrate


def assign_permissions(apps, schema_editor):
    def on_migrations_complete(sender=None, **kwargs):
        Permission = apps.get_model("auth", "Permission")
        App = apps.get_model("app", "App")
        Group = apps.get_model("auth", "Group")

        handle_taxes = Permission.objects.filter(
            codename="handle_taxes", content_type__app_label="checkout"
        ).first()
        manage_checkouts = Permission.objects.filter(
            codename="manage_checkouts", content_type__app_label="checkout"
        ).first()

        apps_qs = App.objects.filter(
            permissions=manage_checkouts,
        )
        for app in apps_qs.iterator():
            app.permissions.add(handle_taxes)

        groups = Group.objects.filter(
            permissions=manage_checkouts,
        )
        for group in groups.iterator():
            group.permissions.add(handle_taxes)

    sender = registry.get_app_config("checkout")
    post_migrate.connect(on_migrations_complete, weak=False, sender=sender)


def set_default_checkout_line_currency(apps, schema_editor):
    Checkout = apps.get_model("checkout", "Checkout")
    CheckoutLine = apps.get_model("checkout", "CheckoutLine")

    for currency in Checkout.objects.values_list("currency", flat=True):
        CheckoutLine.objects.filter(currency=None, checkout__currency=currency).update(
            currency=currency
        )


class Migration(migrations.Migration):

    dependencies = [
        ("checkout", "0050_auto_20220713_1057"),
    ]

    operations = [
        migrations.RunPython(assign_permissions, migrations.RunPython.noop),
        migrations.RunPython(
            set_default_checkout_line_currency, migrations.RunPython.noop
        ),
    ]

# Generated by Django 3.2.14 on 2022-07-13 11:03


from django.apps import apps as registry
from django.db import migrations
from django.db.models.signals import post_migrate


def assign_permissions(apps, schema_editor):
    def on_migrations_complete(sender=None, **kwargs):
        Permission = apps.get_model("auth", "Permission")
        App = apps.get_model("app", "App")
        Group = apps.get_model("auth", "Group")

        handle_taxes = Permission.objects.filter(
            codename="handle_taxes", content_type__app_label="checkout"
        ).first()
        manage_checkouts = Permission.objects.filter(
            codename="manage_checkouts", content_type__app_label="checkout"
        ).first()

        apps_qs = App.objects.filter(
            permissions=manage_checkouts,
        )
        for app in apps_qs.iterator():
            app.permissions.add(handle_taxes)

        groups = Group.objects.filter(
            permissions=manage_checkouts,
        )
        for group in groups.iterator():
            group.permissions.add(handle_taxes)

    sender = registry.get_app_config("checkout")
    post_migrate.connect(on_migrations_complete, weak=False, sender=sender)


def set_default_checkout_line_currency(apps, schema_editor):
    Checkout = apps.get_model("checkout", "Checkout")
    CheckoutLine = apps.get_model("checkout", "CheckoutLine")

    for currency in (
        Checkout.objects.values_list("currency", flat=True).distinct().order_by()
    ):
        CheckoutLine.objects.filter(currency=None, checkout__currency=currency).update(
            currency=currency
        )


class Migration(migrations.Migration):

    dependencies = [
        ("checkout", "0050_auto_20220713_1057"),
    ]

    operations = [
        migrations.RunPython(assign_permissions, migrations.RunPython.noop),
        migrations.RunPython(
            set_default_checkout_line_currency, migrations.RunPython.noop
        ),
    ]

Fix checkout line currency migration (#10542)