# Generated by Django 2.2.12 on 2020-06-19 15:03

from django.db import migrations


def undo(apps, schema_editor):
    ScenarioLanguage = apps.get_model('stats', 'ScenarioLanguage')
    for sl in ScenarioLanguage.objects.all():
        sl.other_labels = ','.join([x.title for x in sl.include_labels.all()])
        sl.include_labels.clear()
        sl.save()


def migrate_scenario_language_labels(apps, schema_editor):
    ScenarioLanguage = apps.get_model('stats', 'ScenarioLanguage')
    Label = apps.get_model('annotations', 'Label')

    for sl in ScenarioLanguage.objects.all():
        for old_label in sl.other_labels.split(','):
            new_label = Label.objects.filter(key__corpora=sl.scenario.corpus,
                                             title=old_label).first()
            if new_label is not None:
                sl.include_labels.add(new_label)

        sl.save()


class Migration(migrations.Migration):

    dependencies = [
        ('stats', '0016_scenariolanguage_include_labels'),
    ]

    operations = [
        migrations.RunPython(migrate_scenario_language_labels, undo)
    ]
