old_contents: # Generated by Django 2.2.9 on 2020-01-29 06:50
  
  from django.db import migrations, models
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ("product", "0111_auto_20191209_0437"),
      ]
  
      operations = [
          migrations.AddField(
              model_name="producttype",
              name="slug",
              field=models.SlugField(default="", max_length=128, unique=True),
              preserve_default=False,
          ),
          migrations.AlterField(
              model_name="category",
              name="slug",
              field=models.SlugField(max_length=128, unique=True),
          ),
          migrations.AlterField(
              model_name="collection",
              name="slug",
              field=models.SlugField(max_length=128, unique=True),
          ),
      ]
  
new_contents: # Generated by Django 2.2.9 on 2020-01-29 06:50
  
  from django.db import migrations, models
  from django.utils.text import slugify
  
  
  def create_unique_slugs_for_producttypes(apps, schema_editor):
      ProductType = apps.get_model("product", "ProductType")
  
      for product_type in ProductType.objects.all():
          if not product_type.slug:
              product_type.slug = generate_unique_slug(product_type)
              product_type.save()
  
  
  def update_non_unique_slugs_for_models(apps, schema_editor):
      models = ["Category", "Collection"]
  
      for model in models:
          Model = apps.get_model("product", model)
  
          for instance in Model.objects.all():
              instance.slug = generate_unique_slug(instance, instance.slug)
              instance.save()
  
  
  def generate_unique_slug(instance, slug_value=None):
      if slug_value:
          slug = slug_value
      else:
          slug = slugify(instance.name)
      unique_slug = slug
  
      ModelClass = instance.__class__
      extension = 1
  
      search_field = f"name__iregex"
      pattern = rf"{slug}-\d+$|{slug}$"
      slug_values = (
          ModelClass._default_manager.filter(**{search_field: pattern})
          .exclude(pk=instance.pk)
          .values_list("slug", flat=True)
      )
  
      while unique_slug in slug_values:
          extension += 1
          unique_slug = f"{slug}-{extension}"
  
      return unique_slug
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ("product", "0111_auto_20191209_0437"),
      ]
  
      operations = [
          migrations.AddField(
              model_name="producttype",
              name="slug",
              field=models.SlugField(default="", max_length=128, unique=True),
              preserve_default=False,
          ),
          migrations.AlterField(
              model_name="category",
              name="slug",
              field=models.SlugField(max_length=128, unique=True),
          ),
          migrations.AlterField(
              model_name="collection",
              name="slug",
              field=models.SlugField(max_length=128, unique=True),
          ),
          migrations.RunPython(
              create_unique_slugs_for_producttypes, migrations.RunPython.noop
          ),
          migrations.RunPython(
              update_non_unique_slugs_for_models, migrations.RunPython.noop
          ),
      ]
  
subject: Add data migration for ProductType, Collection and Category