# Generated by Django 2.2.5 on 2019-12-30 11:13

from django.db import migrations


def mutation(original):
    # It only works because no-one used apostrophe (') in any existing data.
    return original.replace("['", "").replace("']", "").replace("', '", "")


def update_additional_contact(apps, schema_editor):
    """Apply `mutation` for all workshop requests, inquiries or self-organised
    submissions (only to additional_contact field, though)."""
    WorkshopRequest = apps.get_model('workshops', 'WorkshopRequest')
    WorkshopInquiryRequest = apps.get_model('extrequests',
                                            'WorkshopInquiryRequest')
    SelfOrganisedSubmission = apps.get_model('extrequests',
                                             'SelfOrganisedSubmission')

    for object in WorkshopRequest.objects.exclude(additional_contact=''):
        old = object.additional_contact
        object.additional_contact = mutation(object.additional_contact)
        if old != object.additional_contact:
            object.save()

    for object in WorkshopInquiryRequest.objects.exclude(additional_contact=''):
        old = object.additional_contact
        object.additional_contact = mutation(object.additional_contact)
        if old != object.additional_contact:
            object.save()

    for object in SelfOrganisedSubmission.objects.exclude(additional_contact=''):
        old = object.additional_contact
        object.additional_contact = mutation(object.additional_contact)
        if old != object.additional_contact:
            object.save()


class Migration(migrations.Migration):

    dependencies = [
        ('extrequests', '0017_auto_20190913_0835'),
    ]

    operations = [
        migrations.RunPython(update_additional_contact),
    ]
