# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-07-22 08:33
from __future__ import unicode_literals

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


def copy_iati_to_aims(apps, *args):
    """
    Copy the field values from the iati models to the aims models
    """
    models = ['ActivityDateType', 'PolicyMarker', 'Currency']
    for model in models:
        old_model = apps.get_model('iati', model)
        new_model = apps.get_model('aims', model)
        for old in old_model.objects.all():
            serialized = old.__dict__
            del serialized['_state']
            new_model.objects.create(**serialized)


def load_fixture(apps, schema_editor):
    call_command('loaddata_iati_required', revision='0039')


class Migration(migrations.Migration):

    dependencies = [
        ('aims', '0038_remove_transaction_openly_status'),
    ]

    operations = [
        migrations.CreateModel(
            name='ActivityDateType',
            fields=[
                ('code', models.CharField(max_length=20, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=200)),
            ],
        ),
        migrations.CreateModel(
            name='PolicyMarker',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=200)),
            ],
        ),
        migrations.CreateModel(
            name='Currency',
            fields=[
                ('code', models.CharField(max_length=3, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
                ('language', models.CharField(max_length=2)),
            ],
        ),
        migrations.RunPython(copy_iati_to_aims),
        migrations.AlterField(
            model_name='activity',
            name='total_commitment_currency',
            field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Currency'),
        ),
        migrations.AlterField(
            model_name='currencyexchangerate',
            name='base_currency',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='base_currencies', to='aims.Currency', verbose_name='Base Currency'),
        ),
        migrations.AlterField(
            model_name='currencyexchangerate',
            name='currency',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='exchange_currencies', to='aims.Currency', verbose_name='Exchange Currency'),
        ),
        migrations.AlterField(
            model_name='transactionvaluelocation',
            name='currency',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Currency'),
        ),
        migrations.RunPython(load_fixture)
    ]
