# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-08-18 17:28
from __future__ import unicode_literals

from django.db import migrations
from intake.constants import ORG_NAMES


def get_name_map():
    return {
        name: slug
        for slug, name in ORG_NAMES.items()
        }


def get_models(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    return apps.get_model(
            'user_accounts', 'Organization').objects.using(db_alias)


def forward(*args):
    Organization = get_models(*args)
    name_map = get_name_map()
    for org in Organization.all():
        org.slug = name_map[org.name]
        org.save()


def backward(*args):
    Organization = get_models(*args)
    for org in Organization.all():
        org.slug = None
        org.save()


class Migration(migrations.Migration):

    dependencies = [
        ('user_accounts', '0009_organization_slug'),
    ]

    operations = [
        migrations.RunPython(forward, backward),
    ]
