# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-07-05 13:48
from __future__ import unicode_literals

from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models
import django.db.models.deletion


def forwards_func(apps, schema_editor):
    ChangeSet = apps.get_model('editor', 'ChangeSet')
    for changeset in ChangeSet.objects.all():
        try:
            changeset.last_state_update = changeset.updates.filter(state__isnull=False).latest()
        except ObjectDoesNotExist:
            pass
        changeset.save()

def reverse_func(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('editor', '0014_last_update_foreign_key'),
    ]

    operations = [
        migrations.AddField(
            model_name='changeset',
            name='last_state_update',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='editor.ChangeSetUpdate', verbose_name='last state update'),
        ),
        migrations.RunPython(forwards_func, reverse_func),
    ]
