# Generated by Django 2.0.9 on 2019-03-14 19:51

from django.db import migrations, models

from ..models import TodoRecurrenceLog

class Migration(migrations.Migration):

    dependencies = [
        ('todo', '0009_todoitem_streak'),
    ]

    def forwards_func(apps, schema_editor):
        TodoItem = apps.get_model("todo", "TodoItem")

        db_alias = schema_editor.connection.alias

        items = TodoItem.objects.using(db_alias).filter(recurrence__isnull=False)

        for i in items:
            i.streak = 0

            for log in i.todorecurrencelog_set.order_by('-start'):
                if log.status == TodoRecurrenceLog.STATUS.complete:
                    i.streak = i.streak + 1
                elif log.status == TodoRecurrenceLog.STATUS.cancelled:
                    pass
                elif log.status == TodoRecurrenceLog.STATUS.failed:
                    break

            i.save()

    def reverse_func(apps, schema_editor):
        pass

    operations = [
        migrations.RunPython(forwards_func, reverse_func),
    ]
