# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-04-21 01:11
from __future__ import unicode_literals

from django.db import migrations


def add_national_ministry(apps, *args):
    """ Add a new OrganisationType with name 'National Ministry'. """
    OrganisationType = apps.get_model('iati', 'OrganisationType')
    OrganisationType.objects.get_or_create(code=100, name='National Ministry')


def migrate_national_ministries_organisation_type(apps, *args):
    """ Migrate national ministries to the new org type. """
    Organisation = apps.get_model('aims', 'Organisation')

    ministries = Organisation.objects.filter(type__name='Government',
                                             activityparticipatingorganisation__role='Accountable').distinct()
    ministries.update(type_id=100)


class Migration(migrations.Migration):

    dependencies = [
        ('aims', '0017_add_total_commitment'),
    ]

    operations = [
        migrations.RunPython(add_national_ministry, reverse_code=migrations.RunPython.noop),
        migrations.RunPython(migrate_national_ministries_organisation_type, reverse_code=migrations.RunPython.noop),
    ]
