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

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


def set_classicalwork_slug(apps, schema_editor):
    """
    Create a slug for each ClassicalWork already in the DB.
    """
    ClassicalWork = apps.get_model('spectator_events', 'ClassicalWork')

    asfield = AutoSlugField(populate_from='title', separator='-')
    asfield.attname = 'slug'

    for e in ClassicalWork.objects.all():
        e.slug = asfield.pre_save(e, True)
        e.save()

def set_dancepiece_slug(apps, schema_editor):
    """
    Create a slug for each DancePiece already in the DB.
    """
    DancePiece = apps.get_model('spectator_events', 'DancePiece')

    asfield = AutoSlugField(populate_from='title', separator='-')
    asfield.attname = 'slug'

    for e in DancePiece.objects.all():
        e.slug = asfield.pre_save(e, True)
        e.save()

def set_movie_slug(apps, schema_editor):
    """
    Create a slug for each Movie already in the DB.
    """
    Movie = apps.get_model('spectator_events', 'Movie')

    asfield = AutoSlugField(populate_from='title', separator='-')
    asfield.attname = 'slug'

    for e in Movie.objects.all():
        e.slug = asfield.pre_save(e, True)
        e.save()

def set_play_slug(apps, schema_editor):
    """
    Create a slug for each Play already in the DB.
    """
    Play = apps.get_model('spectator_events', 'Play')

    asfield = AutoSlugField(populate_from='title', separator='-')
    asfield.attname = 'slug'

    for e in Play.objects.all():
        e.slug = asfield.pre_save(e, True)
        e.save()

class Migration(migrations.Migration):

    dependencies = [
        ('spectator_events', '0002_event_slug'),
    ]

    operations = [
        migrations.AddField(
            model_name='classicalwork',
            name='slug',
            field=AutoSlugField(blank=True, editable=False, null=True, populate_from='title'),
        ),
        migrations.AddField(
            model_name='dancepiece',
            name='slug',
            field=AutoSlugField(blank=True, editable=False, null=True, populate_from='title'),
        ),
        migrations.AddField(
            model_name='movie',
            name='slug',
            field=AutoSlugField(blank=True, editable=False, null=True, populate_from='title'),
        ),
        migrations.AddField(
            model_name='play',
            name='slug',
            field=AutoSlugField(blank=True, editable=False, null=True, populate_from='title'),
        ),
        migrations.RunPython(set_classicalwork_slug),
        migrations.RunPython(set_dancepiece_slug),
        migrations.RunPython(set_movie_slug),
        migrations.RunPython(set_play_slug),
    ]
