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

from django.db import migrations

import mtp.apps.assessments.managers.activities


def delete_duplicated_scores(apps, schema_editor):
    """Delete duplicated ``AssessableActivityPartUserScore`` instances
    according the new unique_together.

    """

    AssessableActivityPartUserScore = apps.get_model(
        "assessments",
        "AssessableActivityPartUserScore"
    )
    AssessableActivityPartUserScore.objects.clean_duplicates(
        [('user', 'session', 'activity_part_id', 'activity_part_type')]
    )


def undelete_duplicated_scores(apps, schema_editor):
    """It's impossible to undelete duplicated ``AssessableActivityPartUserScore`` instances
    so we pass the method.

    """

    pass


class Migration(migrations.Migration):

    dependencies = [
        ('assessments', '0025_force_certification_status'),
    ]

    operations = [
        migrations.AlterModelManagers(
            name='assessableactivitypartuserscore',
            managers=[
                ('objects', mtp.apps.assessments.managers.activities.AssessableActivityPartUserScoreManager()),
            ],
        ),
        migrations.RunPython(
            delete_duplicated_scores,
            undelete_duplicated_scores
        ),
    ]
