# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-08-10 01:41
from __future__ import unicode_literals

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


def load_fixture(apps, schema_editor):
    call_command('loaddata', 'iati_objects_required', app='aims', exclude=['aims.ActivityStatus'])


def load_iati_required(apps, schema_editor):
    """
    Moved the 'ActivityStatus' out of the fixture create
    in order to sync data loading with the current state
    """
    ActivityStatus = apps.get_model("aims", "ActivityStatus")
    ActivityStatus.objects.bulk_create(
        [
            ActivityStatus(pk=1, language="en", name="Pipeline/identification",),
            ActivityStatus(pk=2, language="en", name="Implementation",),
            ActivityStatus(pk=3, language="en", name="Completion",),
            ActivityStatus(pk=4, language="en", name="Post-completion",),
            ActivityStatus(pk=5, language="en", name="Cancelled",),
            ActivityStatus(pk=6, language="en", name="Suspended",),
        ]
    )


def unload_iati_required(apps, schema_editor):
    ActivityStatus = apps.get_model("aims", "ActivityStatus")
    ActivityStatus.objects.all().delete()
    pass


# These are tables which previously existed in both IATI and in AIMS.
# They were moved in 0042 and can be safely deleted now.
TABLES = [
    'vocabulary',
    'activityparticipatingorganisation',
    'activitypolicymarker',
    'activityrecipientcountry',
    'activityrecipientregion',
    'otheridentifier',
    'activitywebsite',
    'contactinfo',
    'relatedactivity',
    'planneddisbursement',
    'documentlink',
    'result',
    'resultindicator',
    'resultindicatorperiod',
    'description',
    'budget',
    'condition',
]


def drop_tables_code():
    '''
    Generate the SQL to drop the tables which were temporarily renamed in previous migration
    '''
    return ';'.join(['DROP TABLE oldaims_%s' % (t.lower()) for t in TABLES])


class Migration(migrations.Migration):

    dependencies = [
        ('aims', '0042_a_pre_move_vocabulary'), ('iati',
                                                 '0021_iati_to_aims_altertables')
    ]

    create_model_operations = [
        migrations.CreateModel(
            name='ActivityScope',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='ActivityStatus',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=50)),
                ('name_en', models.CharField(max_length=50, null=True)),
                ('name_my', models.CharField(max_length=50, null=True)),
                ('name_ru', models.CharField(max_length=50, null=True)),
                ('name_tm', models.CharField(max_length=50, null=True)),
                ('name_uz', models.CharField(max_length=50, null=True)),
                ('language', models.CharField(max_length=2)),
            ],
        ),
        migrations.CreateModel(
            name='CollaborationType',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
                ('description', models.TextField()),
                ('language', models.CharField(max_length=2)),
            ],
        ),
        migrations.CreateModel(
            name='FinanceTypeCategory',
            fields=[
                ('code', models.IntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=50)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='FinanceType',
            fields=[
                ('code', models.IntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=220)),
                ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.FinanceTypeCategory')),
            ],
        ),
        migrations.CreateModel(
            name='FlowType',
            fields=[
                ('code', models.IntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=150)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='RegionVocabulary',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=20)),
            ],
        ),
        migrations.CreateModel(
            name='TiedStatus',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=40)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='Vocabulary',
            fields=[
                ('code', models.CharField(max_length=10, serialize=False, primary_key=True)),
                ('name', models.CharField(max_length=140)),
            ],
        ),
        migrations.CreateModel(
            name='DescriptionType',
            fields=[
                ('code', models.SmallIntegerField(serialize=False, primary_key=True)),
                ('name', models.CharField(max_length=50)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='LocationType',
            fields=[
                ('code', models.CharField(max_length=10, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
                ('description', models.TextField(default='')),
            ],
        ),
        migrations.CreateModel(
            name='LocationTypeCategory',
            fields=[
                ('code', models.CharField(max_length=10, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
            ],
        ),
        migrations.AddField(
            model_name='locationtype',
            name='category',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='aims.LocationTypeCategory'),
            preserve_default=False,
        ),
        migrations.CreateModel(
            name='GeographicLocationClass',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=200)),
            ],
        ),
        migrations.CreateModel(
            name='DisbursementChannel',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='TransactionType',
            fields=[
                ('code', models.CharField(max_length=2, serialize=False, primary_key=True)),
                ('name', models.CharField(max_length=40)),
                ('description', models.TextField()),
            ]
        ),
        migrations.CreateModel(
            name='ActivityParticipatingOrganisation',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.TextField(default='', blank=True, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='participating_organisations', to='iati.Activity')),
                ('role', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.OrganisationRole')),
                ('organisation', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Organisation')),
            ],
        ),
        migrations.CreateModel(
            name='ActivityPolicyMarker',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('alt_policy_marker', models.CharField(blank=True, default='', max_length=200, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
                ('policy_marker', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.PolicyMarker')),
                ('policy_significance', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.PolicySignificance')),
                ('vocabulary', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Vocabulary')),
            ],
        ),
        migrations.CreateModel(
            name='PolicySignificance',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='ActivityRecipientCountry',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('percentage', models.DecimalField(decimal_places=2, default=None, max_digits=5, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
                ('country', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='geodata.Country')),
            ],
        ),
        migrations.CreateModel(
            name='ActivityRecipientRegion',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('percentage', models.DecimalField(decimal_places=2, default=None, max_digits=5, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
                ('region', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='geodata.Region')),
                ('region_vocabulary',
                 models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='aims.RegionVocabulary')),
            ],
        ),
        migrations.CreateModel(
            name='ActivityWebsite',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('url', models.CharField(max_length=150)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
            ],
        ),
        migrations.CreateModel(
            name='ContactInfo',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('person_name', models.CharField(default='', max_length=100)),
                ('organisation', models.CharField(default='', max_length=100)),
                ('telephone', models.CharField(default='', max_length=100)),
                ('email', models.TextField(default='')),
                ('mailing_address', models.TextField(default='')),
                ('website', models.CharField(default='', max_length=255)),
                ('job_title', models.CharField(default='', max_length=150)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
                ('contact_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='iati.ContactType')),
            ],
        ),
        migrations.CreateModel(
            name='OtherIdentifier',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('owner_ref', models.CharField(default='', max_length=100)),
                ('owner_name', models.CharField(default='', max_length=100)),
                ('identifier', models.CharField(max_length=100)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
            ],
        ),
        migrations.CreateModel(
            name='RelatedActivityType',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=20)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='RelatedActivity',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('ref', models.CharField(default='', max_length=200)),
                ('text', models.TextField(default='')),
                ('current_activity',
                 models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='related_activities',
                                   to='aims.Activity')),
                ('type',
                 models.ForeignKey(blank=True, max_length=200, null=True, on_delete=django.db.models.deletion.CASCADE,
                                   to='aims.RelatedActivityType')),
            ],
        ),
        migrations.CreateModel(
            name='PlannedDisbursement',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('period_start', models.CharField(default='', max_length=100)),
                ('period_end', models.CharField(default='', max_length=100)),
                ('value_date', models.DateField(null=True)),
                ('value', models.DecimalField(decimal_places=2, max_digits=15)),
                ('updated', models.DateField(default=None, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.Activity')),
                ('currency', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                               to='aims.Currency')),
            ],
        ),
        migrations.CreateModel(
            name='DocumentCategoryCategory',
            fields=[
                ('code', models.CharField(max_length=3, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='DocumentCategory',
            fields=[
                ('code', models.CharField(max_length=3, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
                ('description', models.TextField()),
                ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.DocumentCategoryCategory')),
            ],
        ),
        migrations.CreateModel(
            name='DocumentLink',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('url', models.TextField(max_length=500)),
                ('title', models.CharField(default='', max_length=255)),
                ('iso_date', models.DateField(blank=True, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.Activity')),
                ('categories', models.ManyToManyField(to='aims.DocumentCategory')),
                ('file_format', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.FileFormat')),
                ('language', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Language'))
            ],
        ),
        migrations.CreateModel(
            name='FileFormat',
            fields=[
                ('code', models.CharField(max_length=100, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='Result',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(default='', max_length=255)),
                ('description', models.TextField(default='')),
                ('aggregation_status', models.BooleanField(default=False)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='results',
                                               to='aims.Activity')),
                ('result_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                                  to='aims.ResultType')),
            ],
        ),
        migrations.CreateModel(
            name='ResultType',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=30)),
            ],
        ),
        migrations.CreateModel(
            name='ResultIndicator',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(default='', max_length=200)),
                ('description', models.TextField(default='')),
                ('baseline_year', models.IntegerField()),
                ('baseline_value', models.CharField(max_length=100)),
                ('comment', models.TextField(default='')),
                ('measure', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.ResultIndicatorMeasure')),
                ('result', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.Result')),
            ],
        ),
        migrations.CreateModel(
            name='ResultIndicatorMeasure',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=40)),
            ]
        ),
        migrations.CreateModel(
            name='ResultIndicatorPeriod',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('period_start', models.CharField(default='', max_length=50)),
                ('period_end', models.CharField(default='', max_length=50)),
                ('planned_disbursement_period_start', models.CharField(default='', max_length=50)),
                ('planned_disbursement_period_end', models.CharField(default='', max_length=50)),
                ('target', models.CharField(default='', max_length=50)),
                ('actual', models.CharField(default='', max_length=50)),
                ('result_indicator', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.ResultIndicator')),
            ],
        ),
        migrations.CreateModel(
            name='Description',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.TextField(default='', max_length=40000)),
                ('rsr_description_type_id', models.IntegerField(default=None, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
                ('language', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Language')),
                ('type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='description_type', to='aims.DescriptionType')),
            ],
        ),
        migrations.CreateModel(
            name='BudgetStatus',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=20)),
            ],
        ),
        migrations.CreateModel(
            name='BudgetType',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=20)),
                ('language', models.CharField(max_length=2)),
            ],
        ),
        migrations.CreateModel(
            name='Budget',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('period_start', models.DateField(blank=True, default=None, null=True)),
                ('period_end', models.DateField(blank=True, default=None, null=True)),
                ('value', models.DecimalField(decimal_places=2, max_digits=15)),
                ('value_date', models.DateField(default=None, null=True)),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='iati.Activity')),
                ('currency', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Currency')),
                ('status', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='aims.BudgetStatus')),
                ('type', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='aims.BudgetType')),
            ],
        ),
        migrations.CreateModel(
            name='ConditionType',
            fields=[
                ('code', models.SmallIntegerField(primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=40)),
                ('language', models.CharField(max_length=2)),
            ],
        ),
        migrations.CreateModel(
            name='Condition',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('text', models.TextField(default='')),
                ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='aims.Activity')),
                ('type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                           to='aims.ConditionType')),
            ],
        ),
    ]

    operations = [
        SeparateDatabaseAndState([], create_model_operations),
        migrations.AlterField(
            model_name='activitysector',
            name='vocabulary',
            field=models.ForeignKey(
                blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.Vocabulary'),
        ),
        migrations.AlterField(
            model_name='activitypolicymarker',
            name='policy_significance',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    to='aims.PolicySignificance'),
        ),
        migrations.AlterUniqueTogether(
            name='activityparticipatingorganisation',
            unique_together=set([('activity', 'organisation', 'role')]),
        ),
        migrations.AlterField(
            model_name='documentnarrative',
            name='document',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='narrative',
                                    to='aims.DocumentLink'),
        ),
        migrations.AlterField(
            model_name='documentupload',
            name='documentlink',
            field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True,
                                       related_name='upload', serialize=False, to='aims.DocumentLink'),
        ),
        migrations.AlterField(
            model_name='result',
            name='result_type',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='aims.ResultType'),
        ),
        migrations.AlterUniqueTogether(
            name='description',
            unique_together=set([('activity', 'type', 'language')]),
        ),
        migrations.RunPython(load_iati_required, unload_iati_required),
        migrations.RunPython(load_fixture),
        migrations.RunSQL(drop_tables_code())
    ]
