# Generated by Django 3.0.7 on 2020-07-09 09:35

from django.db import migrations, models
import re
def slugify(title):
    slug = title.lower()
    slug = re.sub("[^0-9a-zA-Z-]+", " ", slug)
    slug = slug.strip()
    slug = re.sub("\s+", "-", slug)
    return slug

def update_post_slug(apps, schema_editor):
    Post = apps.get_model("discussions", "Post")
    post_objs = []
    for post in Post.objects.raw("select id, title from posts where parent_post_id is null and title is not null and title != ''"):
        post.slug = slugify(post.title)
        post_objs.append(post)
    Post.objects.bulk_update(post_objs, ['slug'], batch_size=100)


class Migration(migrations.Migration):

    dependencies = [
        ('discussions', '0020_auto_20200709_0551'),
    ]

    operations = [
        migrations.AddField(
            model_name='post',
            name='slug',
            field=models.CharField(null=True, max_length=120),
            preserve_default=False,
        ),
        migrations.RunPython(update_post_slug),
        migrations.AlterField(
            model_name='post',
            name='slug',
            field=models.CharField(max_length=120),
        ),
    ]
