# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


def reorder_steps(apps, shema_editor):
    Workshop = apps.get_model('workshops', 'Workshop')

    for workshop in Workshop.objects.all():
        for index_step, step in enumerate(workshop.steps.order_by('sort_order')):
            step.sort_order = index_step
            step.save()


def reorder_answers(apps, shema_editor):
    WorkshopStepQuestion = apps.get_model('workshops', 'WorkshopStepQuestion')

    for question in WorkshopStepQuestion.objects.all():
        for index_answer, answer in enumerate(question.answers.order_by('sort_order')):
            answer.sort_order = index_answer
            answer.save()


class Migration(migrations.Migration):

    dependencies = [
        ('workshops', '0018_auto_20150831_2206'),
    ]

    operations = [
        migrations.RunPython(reorder_steps),
        migrations.AlterUniqueTogether(
            name='workshopstep',
            unique_together=set([('sort_order', 'workshop')]),
        ),
        migrations.RunPython(reorder_answers),
        migrations.AlterUniqueTogether(
            name='workshopstepquestionanswer',
            unique_together=set([('sort_order', 'question')]),
        ),
    ]
