# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-09-05 20:44
from __future__ import unicode_literals

from django.db import migrations, models
# from django.core.exceptions import ObjectDoesNotExist
# from capdb.utils import generate_unique_slug


def fill_in_slug(apps, schema_editor):
    pass
    # Keeping for now for documentation -- we no longer handle slugs this way, and this migration has already been applied
    # to all deployments:

    # CaseMetadata = apps.get_model("capdb", "CaseMetadata")

    # for case in CaseMetadata.objects.all():
    #     try:
    #         citation = case.citations.get(type="official").cite
    #     except ObjectDoesNotExist:
    #         citation = case.citations.first().cite
    #
    #     case.slug = generate_unique_slug(case, citation)
    #     case.save(update_fields=['slug'])


def remove_slug(apps, schema_editor):
    for case in apps.get_model("capdb", "CaseMetadata").objects.all():
        case.slug = None
        case.save(update_fields=['slug'])


class Migration(migrations.Migration):

    dependencies = [
        ('capdb', '0008_casemetadata_slug'),
    ]

    operations = [
        migrations.RunPython(fill_in_slug, remove_slug),

        migrations.AlterField(
            model_name='casemetadata',
            name='slug',
            field=models.SlugField(max_length=255, unique=True),
        ),
    ]
