# Generated by Django 2.2.14 on 2020-12-18 21:36

from django.db import migrations

# constant to avoid typos
KPI_INDICATOR_NAME = "Key Performance Indicator (KPI)"

def make_kpi_indicator_type(apps, schema_editor):
    IndicatorType = apps.get_model("indicators", "IndicatorType")
    db_alias = schema_editor.connection.alias
    if not IndicatorType.objects.using(db_alias).filter(indicator_type=KPI_INDICATOR_NAME).exists():
        kpi_it = IndicatorType(indicator_type=KPI_INDICATOR_NAME)
        kpi_it.save()

def reverse_make_kpi_indicator_type(apps, schema_editor):
    IndicatorType = apps.get_model("indicators", "IndicatorType")
    db_alias = schema_editor.connection.alias
    IndicatorType.objects.using(db_alias).filter(indicator_type=KPI_INDICATOR_NAME).delete()

def set_kpi_indicator_type_for_kpis(apps, schema_editor):
    Indicator = apps.get_model("indicators", "Indicator")
    db_alias = schema_editor.connection.alias
    kpi_it = apps.get_model("indicators", "IndicatorType").objects.using(db_alias).filter(
        indicator_type=KPI_INDICATOR_NAME).first()
    for kpi_indicator in Indicator.objects.using(db_alias).filter(key_performance_indicator=True):
        kpi_indicator.indicator_type.add(kpi_it)

def reverse_set_kpi_indicator_type_for_kpis(apps, schema_editor):
    Indicator = apps.get_model("indicators", "Indicator")
    db_alias = schema_editor.connection.alias
    kpi_it = apps.get_model("indicators", "IndicatorType").objects.using(db_alias).filter(
        indicator_type=KPI_INDICATOR_NAME).first()
    for kpi_indicator in Indicator.objects.using(db_alias).filter(indicator_type=kpi_it):
        kpi_indicator.key_performance_indicator = True
        kpi_indicator.save()

class Migration(migrations.Migration):

    dependencies = [
        ('indicators', '0092_auto_20201214_1357'),
    ]

    operations = [
        migrations.RunPython(make_kpi_indicator_type, reverse_make_kpi_indicator_type),
        migrations.RunPython(set_kpi_indicator_type_for_kpis, reverse_set_kpi_indicator_type_for_kpis),
        migrations.RemoveField(
            model_name='indicator',
            name='key_performance_indicator',
        ),
    ]

# Generated by Django 2.2.14 on 2020-12-18 21:36

from django.db import migrations

# constant to avoid typos
KPI_INDICATOR_NAME = "Key Performance Indicator (KPI)"

def make_kpi_indicator_type(apps, schema_editor):
    IndicatorType = apps.get_model("indicators", "IndicatorType")
    db_alias = schema_editor.connection.alias
    if not IndicatorType.objects.using(db_alias).filter(indicator_type=KPI_INDICATOR_NAME).exists():
        kpi_it = IndicatorType(indicator_type=KPI_INDICATOR_NAME)
        kpi_it.save()

def reverse_make_kpi_indicator_type(apps, schema_editor):
    IndicatorType = apps.get_model("indicators", "IndicatorType")
    db_alias = schema_editor.connection.alias
    IndicatorType.objects.using(db_alias).filter(indicator_type=KPI_INDICATOR_NAME).delete()

def set_kpi_indicator_type_for_kpis(apps, schema_editor):
    Indicator = apps.get_model("indicators", "Indicator")
    db_alias = schema_editor.connection.alias
    kpi_it = apps.get_model("indicators", "IndicatorType").objects.using(db_alias).filter(
        indicator_type=KPI_INDICATOR_NAME).first()
    for kpi_indicator in Indicator.objects.using(db_alias).filter(key_performance_indicator=True):
        kpi_indicator.indicator_type.add(kpi_it)

def reverse_set_kpi_indicator_type_for_kpis(apps, schema_editor):
    Indicator = apps.get_model("indicators", "Indicator")
    db_alias = schema_editor.connection.alias
    kpi_it = apps.get_model("indicators", "IndicatorType").objects.using(db_alias).filter(
        indicator_type=KPI_INDICATOR_NAME).first()
    for kpi_indicator in Indicator.objects.using(db_alias).filter(indicator_type=kpi_it):
        kpi_indicator.key_performance_indicator = True
        kpi_indicator.save()

class Migration(migrations.Migration):

    dependencies = [
        ('indicators', '0092_auto_20201214_1357'),
    ]

    operations = [
        migrations.RunPython(make_kpi_indicator_type, reverse_make_kpi_indicator_type),
        migrations.RunPython(set_kpi_indicator_type_for_kpis, reverse_set_kpi_indicator_type_for_kpis),
        migrations.RemoveField(
            model_name='indicator',
            name='key_performance_indicator',
        ),
    ]

# Generated by Django 2.2.14 on 2020-12-18 21:36 # constant to avoid typos