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

from django.db import migrations, models
import mtp.apps.workshops.managers


def delete_duplicated_user_answers(apps, schema_editor):
    """Delete duplicated ``WorkshopStepQuestionUserAnswer`` instances."""

    WorkshopStepQuestionUserAnswer = apps.get_model(
        "workshops",
        "WorkshopStepQuestionUserAnswer"
    )
    WorkshopStepQuestionUserAnswer.objects.clean_duplicates(
        (('user', 'training_session', 'answer', 'question'),)
    )


def undelete_duplicated_user_answers(apps, schema_editor):
    """Undelete duplicated ``WorkshopStepQuestionUserAnswer`` instances.

    But it's impossible to undelete duplicated ``WorkshopStepQuestionUserAnswer`` instances
    so we pass the method.
    """

    pass


class Migration(migrations.Migration):

    dependencies = [
        ('workshops', '0025_remove_workshop_needs_approval'),
    ]

    operations = [
        migrations.AlterModelManagers(
            name='workshopstepquestionuseranswer',
            managers=[
                ('objects', mtp.apps.workshops.managers.WorkshopStepQuestionUserAnswerManager()),
            ],
        ),
        migrations.RunPython(
            delete_duplicated_user_answers,
            undelete_duplicated_user_answers
        ),
    ]
