<reponame>MelanieFJNR/Blitz-API
# Generated by Django 2.2.2 on 2019-08-15 07:28

from django.db import migrations, models
import django.db.models.deletion


from utils.migrate_base_product import update_id_on_models



def link_retreat_to_base_product(apps, schema_editor):
    retreat_model = apps.get_model('retirement',
                                   'retreat')
    base_product_model = apps.get_model('store',
                                        'baseproduct')
    order_line_model = apps.get_model('store',
                                      'orderline')
    updated_ids = {}
    for retreat in retreat_model.objects.all():
        base_product = base_product_model.objects.create(
            name=retreat.name,
            name_fr=retreat.name_fr,
            name_en=retreat.name_en,
            available=retreat.available,
            price=retreat.price,
            details_fr=retreat.details_fr,
            details_en=retreat.details_en
        )
        updated_ids[retreat.old_id] = base_product.id
        retreat.baseproduct_ptr = base_product
        retreat.save()

    for order_line in order_line_model.objects.filter(
            content_type__model='retreat'):
        order_line.object_id = updated_ids[order_line.object_id]
        order_line.save()


def update_retreat_id_on_other_models(apps, schema_editor):
    retreat_model = apps.get_model('retirement',
                                   'retreat')

    list_model_to_migrate = [
        ('retirement', 'picture', 'id', 'retreat', True),
        ('retirement', 'reservation', 'id', 'retreat', True),
        ('store', 'membership', 'baseproduct_ptr_id', 'retreats', False),
        ('store', 'coupon', 'id', 'applicable_retreats', False),
        ('store', 'membershipcoupon', 'id', 'applicable_retreats', False),
        ('retirement', 'WaitQueueNotification', 'id', 'retreat', True),
    ]

    updated_ids = {}
    for retreat in retreat_model.objects.all():
        updated_ids[retreat.old_id] = retreat.baseproduct_ptr_id

    update_id_on_models(apps, list_model_to_migrate,
                        updated_ids, retreat_model)

    # Update wait queue
    # Copy them, delete and recreate
    waite_queue_model = apps.get_model('retirement', 'waitqueue')

    list_wait_queue = list(waite_queue_model.objects.all())
    waite_queue_model.objects.all().delete()

    for waite_queue in list_wait_queue:
        waite_queue.retreat_id = updated_ids[waite_queue.retreat_id]
        waite_queue.save()


def save_old_id(apps, schema_editor):
    retreat_model = apps.get_model('retirement',
                                   'retreat')
    for retreat in retreat_model.objects.all():
        retreat.old_id = retreat.id
        retreat.save()


class Migration(migrations.Migration):
    dependencies = [
        ('store', '0028_auto_20190815_0328'),
        ('retirement', '0015_auto_20190815_0305'),
    ]

    operations = [
        migrations.AddField(
            model_name='historicalretreat',
            name='old_id',
            field=models.IntegerField(default=1,
                                      null=True,
                                      verbose_name='Id before migrate to base product'),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='retreat',
            name='old_id',
            field=models.IntegerField(default=1,
                                      null=True,
                                      verbose_name='Id before migrate to base product'),
            preserve_default=False,
        ),
        migrations.RunPython(save_old_id),
        migrations.AddField(
            model_name='historicalretreat',
            name='baseproduct_ptr',
            field=models.ForeignKey(auto_created=True, blank=True,
                                    db_constraint=False,
                                    null=True,
                                    on_delete=django.db.models.deletion.DO_NOTHING,
                                    parent_link=True, related_name='+',
                                    to='store.BaseProduct'),
        ),
        migrations.AddField(
            model_name='retreat',
            name='baseproduct_ptr',
            field=models.OneToOneField(auto_created=True,
                                       on_delete=django.db.models.deletion.CASCADE,
                                       parent_link=True, null=True, blank=True,
                                       serialize=False,
                                       to='store.BaseProduct'),
            preserve_default=False,
        ),
        migrations.RunPython(link_retreat_to_base_product),
        migrations.RemoveField(
            model_name='retreat',
            name='id',
        ),
        migrations.AlterField(
            model_name='retreat',
            name='baseproduct_ptr',
            field=models.OneToOneField(auto_created=True,
                                       on_delete=django.db.models.deletion.CASCADE,
                                       parent_link=True, primary_key=True,
                                       serialize=False,
                                       to='store.BaseProduct'),
            preserve_default=False,
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='available',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='details',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='details_en',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='details_fr',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='name',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='name_en',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='name_fr',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='price',
        ),
        migrations.RunPython(update_retreat_id_on_other_models),
    ]

# Generated by Django 2.2.2 on 2019-08-15 07:28

from django.db import migrations, models
import django.db.models.deletion


from utils.migrate_base_product import update_id_on_models



def link_retreat_to_base_product(apps, schema_editor):
    retreat_model = apps.get_model('retirement',
                                   'retreat')
    base_product_model = apps.get_model('store',
                                        'baseproduct')
    order_line_model = apps.get_model('store',
                                      'orderline')
    updated_ids = {}
    for retreat in retreat_model.objects.all():
        base_product = base_product_model.objects.create(
            name=retreat.name,
            name_fr=retreat.name_fr,
            name_en=retreat.name_en,
            available=retreat.available,
            price=retreat.price,
            details_fr=retreat.details_fr,
            details_en=retreat.details_en
        )
        updated_ids[retreat.old_id] = base_product.id
        retreat.baseproduct_ptr = base_product
        retreat.save()

    for order_line in order_line_model.objects.filter(
            content_type__model='retreat'):
        order_line.object_id = updated_ids[order_line.object_id]
        order_line.save()


def update_retreat_id_on_other_models(apps, schema_editor):
    retreat_model = apps.get_model('retirement',
                                   'retreat')

    list_model_to_migrate = [
        ('retirement', 'picture', 'id', 'retreat', True),
        ('retirement', 'reservation', 'id', 'retreat', True),
        ('store', 'membership', 'baseproduct_ptr_id', 'retreats', False),
        ('store', 'coupon', 'id', 'applicable_retreats', False),
        ('store', 'membershipcoupon', 'id', 'applicable_retreats', False),
        ('retirement', 'WaitQueueNotification', 'id', 'retreat', True),
    ]

    updated_ids = {}
    for retreat in retreat_model.objects.all():
        updated_ids[retreat.old_id] = retreat.baseproduct_ptr_id

    update_id_on_models(apps, list_model_to_migrate,
                        updated_ids, retreat_model)

    # Update wait queue
    # Copy them, delete and recreate
    waite_queue_model = apps.get_model('retirement', 'waitqueue')

    list_wait_queue = list(waite_queue_model.objects.all())
    waite_queue_model.objects.all().delete()

    for waite_queue in list_wait_queue:
        waite_queue.retreat_id = updated_ids[waite_queue.retreat_id]
        waite_queue.save()


def save_old_id(apps, schema_editor):
    retreat_model = apps.get_model('retirement',
                                   'retreat')
    for retreat in retreat_model.objects.all():
        retreat.old_id = retreat.id
        retreat.save()


class Migration(migrations.Migration):
    dependencies = [
        ('store', '0028_auto_20190815_0328'),
        ('retirement', '0015_auto_20190815_0305'),
    ]

    operations = [
        migrations.AddField(
            model_name='historicalretreat',
            name='old_id',
            field=models.IntegerField(default=1,
                                      null=True,
                                      verbose_name='Id before migrate to base product'),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='retreat',
            name='old_id',
            field=models.IntegerField(default=1,
                                      null=True,
                                      verbose_name='Id before migrate to base product'),
            preserve_default=False,
        ),
        migrations.RunPython(save_old_id),
        migrations.AddField(
            model_name='historicalretreat',
            name='baseproduct_ptr',
            field=models.ForeignKey(auto_created=True, blank=True,
                                    db_constraint=False,
                                    null=True,
                                    on_delete=django.db.models.deletion.DO_NOTHING,
                                    parent_link=True, related_name='+',
                                    to='store.BaseProduct'),
        ),
        migrations.AddField(
            model_name='retreat',
            name='baseproduct_ptr',
            field=models.OneToOneField(auto_created=True,
                                       on_delete=django.db.models.deletion.CASCADE,
                                       parent_link=True, null=True, blank=True,
                                       serialize=False,
                                       to='store.BaseProduct'),
            preserve_default=False,
        ),
        migrations.RunPython(link_retreat_to_base_product),
        migrations.RemoveField(
            model_name='retreat',
            name='id',
        ),
        migrations.AlterField(
            model_name='retreat',
            name='baseproduct_ptr',
            field=models.OneToOneField(auto_created=True,
                                       on_delete=django.db.models.deletion.CASCADE,
                                       parent_link=True, primary_key=True,
                                       serialize=False,
                                       to='store.BaseProduct'),
            preserve_default=False,
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='available',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='details',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='details_en',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='details_fr',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='name',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='name_en',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='name_fr',
        ),
        migrations.RemoveField(
            model_name='retreat',
            name='price',
        ),
        migrations.RunPython(update_retreat_id_on_other_models),
    ]
# Generated by Django 2.2.2 on 2019-08-15 07:28 # Update wait queue # Copy them, delete and recreate