# Generated by Django 2.2.4 on 2019-08-07 11:02

from django.db import migrations, models
from django.core.management import call_command


def hvad_to_modeltranslations(apps, schema_editor):
    ''' copy translations for all nutrition contributions to model translation fields '''
    OP_translation = apps.get_model('profiles_v2', 'OrganisationProfileTranslation')
    for opt in OP_translation.objects.all():
        setattr(opt.master, 'background_{}'.format(opt.language_code), opt.background)
        opt.master.save()


def call_sync_translation_fields(*arga, **kwargs):
    call_command("sync_translation_fields", "--noinput")


class Migration(migrations.Migration):

    dependencies = [
        ('profiles_v2', '0016_contact'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='organisationprofile',
            options={},
        ),
        migrations.AlterModelManagers(
            name='organisationprofile',
            managers=[
            ],
        ),
        # Add the new model translation fields
        migrations.AddField(
            model_name='organisationprofile',
            name='background',
            field=models.TextField(blank=True, max_length=100000, null=True),
        ),

        # Modeltranslation update
        # In Openly these were actually added as "AddField" commands
        # which confused everything
        migrations.RunPython(call_sync_translation_fields),

        # These WERE run, but screw up new installs
        # and test runs because modeltranslations gets horribly confused

        # migrations.AddField(
        #     model_name='organisationprofile',
        #     name='background_en',
        #     field=models.TextField(blank=True, max_length=100000, null=True),
        # ),
        # migrations.AddField(
        #     model_name='organisationprofile',
        #     name='background_tm',
        #     field=models.TextField(blank=True, max_length=100000, null=True),
        # ),
        # migrations.AddField(
        #     model_name='organisationprofile',
        #     name='background_my',
        #     field=models.TextField(blank=True, max_length=100000, null=True),
        # ),
        # migrations.AddField(
        #     model_name='organisationprofile',
        #     name='background_uz',
        #     field=models.TextField(blank=True, max_length=100000, null=True),
        # ),
        # migrations.AddField(
        #     model_name='organisationprofile',
        #     name='background_ru',
        #     field=models.TextField(blank=True, max_length=100000, null=True),
        # ),

        # copy data from hvad to modeltranslations
        migrations.RunPython(hvad_to_modeltranslations),
        # delete data in hvad
        migrations.DeleteModel(
            name='OrganisationProfileTranslation',
        ),
    ]
