# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-11-01 15:53
from __future__ import unicode_literals

from django.db import migrations
from spectator.core.fields import AutoSlugField


def set_slug(apps, schema_editor):
    """
    Create a slug for each PublicationSeries already in the DB.
    """
    PublicationSeries = apps.get_model('spectator_reading', 'PublicationSeries')

    # We need to use AutoSlugField's methods to generate a slug for each
    # PublicationSeries.
    asfield = AutoSlugField(populate_from='title', separator='-')
    asfield.attname = 'slug'

    for ps in PublicationSeries.objects.all():
        ps.slug = asfield.pre_save(ps, True)
        ps.save()


class Migration(migrations.Migration):

    dependencies = [
        ('spectator_reading', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='publicationseries',
            name='slug',
            field=AutoSlugField(blank=True, editable=False, null=True, populate_from='title'),
        ),
        migrations.RunPython(set_slug),
    ]
