text: # Generated by Django 2.2.2 on 2019-08-15 07:28
  
  from django.conf import settings
  from django.db import migrations, models
  import django.db.models.deletion
  import simple_history.models
  
  from utils.migrate_base_product import update_id_on_models
  
  
  def save_old_id_membership(apps, schema_editor):
      membership_model = apps.get_model('store',
                                     'membership')
      for membership in membership_model.objects.all():
          membership.old_id = membership.id
          membership.save()
  
  
  def save_old_id_package(apps, schema_editor):
      package_model = apps.get_model('store',
                                     'package')
      for package in package_model.objects.all():
          package.old_id = package.id
          package.save()
  
  
  def link_membership_to_base_product(apps, schema_editor):
      membership_model = apps.get_model('store',
                                          'membership')
      base_product_model = apps.get_model('store',
                                          'baseproduct')
      order_line_model = apps.get_model('store',
                                        'orderline')
      updated_ids = {}
      for membership in membership_model.objects.all():
          base_product = base_product_model.objects.create(
              name=membership.name,
              name_fr=membership.name_fr,
              name_en=membership.name_en,
              available=membership.available,
              price=membership.price,
              details_fr=membership.details_fr,
              details_en=membership.details_en
          )
          updated_ids[membership.old_id] = base_product.id
          membership.baseproduct_ptr = base_product
          membership.save()
  
      for order_line in order_line_model.objects.filter(
              content_type__model='membership'):
          order_line.object_id = updated_ids[order_line.object_id]
          order_line.save()
  
  
  def link_package_to_base_product(apps, schema_editor):
      package_model = apps.get_model('store',
                                     'package')
      base_product_model = apps.get_model('store',
                                          'baseproduct')
      order_line_model = apps.get_model('store',
                                        'orderline')
      updated_ids = {}
  
      for package in package_model.objects.all():
          base_product = base_product_model.objects.create(
              name=package.name,
              name_fr=package.name_fr,
              name_en=package.name_en,
              available=package.available,
              price=package.price,
              details_fr=package.details_fr,
              details_en=package.details_en
          )
          updated_ids[package.old_id] = base_product.id
          package.baseproduct_ptr = base_product
          package.save()
  
      for order_line in order_line_model.objects.filter(
              content_type__model='package'):
          order_line.object_id = updated_ids[order_line.object_id]
          order_line.save()
  
  
  def update_membership_id_on_other_models(apps, schema_editor):
      membership_model = apps.get_model('store',
                                        'membership')
  
      list_model_to_migrate = [
          ('store', 'package', 'baseproduct_ptr_id', 'exclusive_memberships', False),
          ('store', 'coupon', 'id', 'applicable_memberships', False),
          ('store', 'membershipcoupon', 'id', 'applicable_memberships', False),
          ('store', 'membershipcoupon', 'id', 'membership', True),
          ('blitz_api', 'user', 'id', 'membership', True),
      ]
  
      updated_ids = {}
      for membership in membership_model.objects.all():
          updated_ids[membership.old_id] = membership.baseproduct_ptr_id
  
      update_id_on_models(apps, list_model_to_migrate, updated_ids, membership_model)
  
  
  def update_package_id_on_other_models(apps, schema_editor):
      package_model = apps.get_model('store',
                                     'package')
  
      list_model_to_migrate = [
          ('store', 'coupon', 'id', 'applicable_packages', False),
          ('store', 'membershipcoupon', 'id', 'applicable_packages',  False)
      ]
  
      updated_ids = {}
      for package in package_model.objects.all():
          updated_ids[package.old_id] = package.baseproduct_ptr_id
      update_id_on_models(apps, list_model_to_migrate, updated_ids, package_model)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          migrations.swappable_dependency(settings.AUTH_USER_MODEL),
          ('store', '0027_auto_20190815_0305'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='historicalmembership',
              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='membership',
              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_membership),
          migrations.AddField(
              model_name='historicalpackage',
              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='package',
              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_package),
          migrations.CreateModel(
              name='BaseProduct',
              fields=[
                  ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                  ('name', models.CharField(max_length=253, verbose_name='Name')),
                  ('name_fr', models.CharField(max_length=253, null=True, verbose_name='Name')),
                  ('name_en', models.CharField(max_length=253, null=True, verbose_name='Name')),
                  ('available', models.BooleanField(default=False, verbose_name='Available')),
                  ('price', models.DecimalField(decimal_places=2, max_digits=6, verbose_name='Price')),
                  ('details', models.CharField(blank=True, max_length=1000, null=True, verbose_name='Details')),
                  ('details_fr', models.CharField(blank=True, max_length=1000, null=True, verbose_name='Details')),
                  ('details_en', models.CharField(blank=True, max_length=1000, null=True, verbose_name='Details')),
              ],
          ),
          migrations.CreateModel(
              name='HistoricalBaseProduct',
              fields=[
                  ('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
                  ('name', models.CharField(max_length=253, verbose_name='Name')),
                  ('name_fr', models.CharField(max_length=253, null=True, verbose_name='Name')),
                  ('name_en', models.CharField(max_length=253, null=True, verbose_name='Name')),
                  ('available', models.BooleanField(default=False, verbose_name='Available')),
                  ('price', models.DecimalField(decimal_places=2, max_digits=6, verbose_name='Price')),
                  ('details', models.CharField(blank=True, max_length=1000, null=True, verbose_name='Details')),
                  ('details_fr', models.CharField(blank=True, max_length=1000, null=True, verbose_name='Details')),
                  ('details_en', models.CharField(blank=True, max_length=1000, null=True, verbose_name='Details')),
                  ('history_id', models.AutoField(primary_key=True, serialize=False)),
                  ('history_date', models.DateTimeField()),
                  ('history_change_reason', models.CharField(max_length=100, null=True)),
                  ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
                  ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
              ],
              options={
                  'verbose_name': 'historical base product',
                  'ordering': ('-history_date', '-history_id'),
                  'get_latest_by': 'history_date',
              },
              bases=(simple_history.models.HistoricalChanges, models.Model),
          ),
          migrations.AddField(
              model_name='historicalmembership',
              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='historicalpackage',
              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='membership',
              name='baseproduct_ptr',
              field=models.ForeignKey(auto_created=True, null=True, blank=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, serialize=False, to='store.BaseProduct'),
              preserve_default=False,
          ),
          migrations.RunPython(link_membership_to_base_product),
          migrations.RemoveField(
              model_name='membership',
              name='id',
          ),
          migrations.AlterField(
              model_name='membership',
              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='optionproduct',
              name='id',
          ),
          migrations.AddField(
              model_name='optionproduct',
              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.AddField(
              model_name='package',
              name='baseproduct_ptr',
              field=models.ForeignKey(auto_created=True, null=True, blank=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, serialize=False, to='store.BaseProduct'),
              preserve_default=False,
          ),
          migrations.RunPython(link_package_to_base_product),
          migrations.RemoveField(
              model_name='package',
              name='id',
          ),
          migrations.AlterField(
              model_name='package',
              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='membership',
              name='available',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='details',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='details_en',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='details_fr',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='name',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='name_en',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='name_fr',
          ),
          migrations.RemoveField(
              model_name='membership',
              name='price',
          ),
          migrations.RemoveField(
              model_name='optionproduct',
              name='available',
          ),
          migrations.RemoveField(
              model_name='optionproduct',
              name='details',
          ),
          migrations.RemoveField(
              model_name='optionproduct',
              name='name',
          ),
          migrations.RemoveField(
              model_name='optionproduct',
              name='price',
          ),
          migrations.RemoveField(
              model_name='package',
              name='available',
          ),
          migrations.RemoveField(
              model_name='package',
              name='details',
          ),
          migrations.RemoveField(
              model_name='package',
              name='details_en',
          ),
          migrations.RemoveField(
              model_name='package',
              name='details_fr',
          ),
          migrations.RemoveField(
              model_name='package',
              name='name',
          ),
          migrations.RemoveField(
              model_name='package',
              name='name_en',
          ),
          migrations.RemoveField(
              model_name='package',
              name='name_fr',
          ),
          migrations.RemoveField(
              model_name='package',
              name='price',
          ),
          migrations.RunPython(update_package_id_on_other_models),
          migrations.RunPython(update_membership_id_on_other_models),
      ]
  