<reponame>code-for-canada/django-nginx-reactjs-docker<filename>backend/custom_models/migrations/0016_upload_org_structure_tree_view_markdown.py
# Generated by Django 2.1.7 on 2019-05-30 14:07
# Edited by <NAME> to upload the organizatioanl structure tree view markdown content for the sample test

from django.db import migrations


def upload_org_structure_tree_view_markdown(apps, schema_editor):
    # get models
    language = apps.get_model("custom_models", "Language")
    item_type = apps.get_model("custom_models", "ItemType")
    item = apps.get_model("custom_models", "Item")
    item_text = apps.get_model("custom_models", "ItemText")
    test = apps.get_model("custom_models", "Test")

    # get db alias
    db_alias = schema_editor.connection.alias

    # lookup languages; do not use bulk_create since we need these objects later on
    l_english = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="en", ISO_Code_2="en-ca")
        .last()
    )
    l_french = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="fr", ISO_Code_2="fr-ca")
        .last()
    )
    emib_sample_item_id = (
        test.objects.using(db_alias).filter(test_name="emibSampleTest").last().item_id
    )
    it_background = (
        item_type.objects.using(db_alias).filter(type_desc="background").last()
    )
    background_id = (
        item.objects.using(db_alias)
        .filter(parent_id=emib_sample_item_id, item_type_id=it_background, order=0)
        .last()
    )
    it_markdown = item_type.objects.using(db_alias).filter(type_desc="markdown").last()

    # create items; do not use bulk_create since we need these objects later on
    # organizational structure items
    i_org_structure_tree_view_title = item(
        parent_id=background_id, item_type_id=it_markdown, order=6
    )
    i_org_structure_tree_view_title.save()

    i_org_structure_tree_view_description = item(
        parent_id=background_id, item_type_id=it_markdown, order=7
    )
    i_org_structure_tree_view_description.save()

    # bulk create item_text
    item_text.objects.using(db_alias).bulk_create(
        [
            item_text(
                item_id=i_org_structure_tree_view_title,
                text_detail="The Organizational Chart of the ODC",
                language=l_english,
            ),
            item_text(
                item_id=i_org_structure_tree_view_title,
                text_detail="Organigramme (CDO)",
                language=l_french,
            ),
            item_text(
                item_id=i_org_structure_tree_view_description,
                text_detail="Below is a tree view of the organization chart. Once selected, you can use the arrow keys to navigation, expand, and collapse information.",
                language=l_english,
            ),
            item_text(
                item_id=i_org_structure_tree_view_description,
                text_detail="FR Below is a tree view of the organization chart. Once selected, you can use the arrow keys to navigation, expand, and collapse information.",
                language=l_french,
            ),
        ]
    )


def destroy_org_structure_tree_view_markdown(apps, schema_editor):
    # get models
    language = apps.get_model("custom_models", "Language")
    item_type = apps.get_model("custom_models", "ItemType")
    item = apps.get_model("custom_models", "Item")
    item_text = apps.get_model("custom_models", "ItemText")
    test = apps.get_model("custom_models", "Test")
    # get db alias
    db_alias = schema_editor.connection.alias
    # get language objects
    l_english = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="en", ISO_Code_2="en-ca")
        .last()
    )
    l_french = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="fr", ISO_Code_2="fr-ca")
        .last()
    )
    # get item_type objects
    emib_sample_item_id = (
        test.objects.using(db_alias).filter(test_name="emibSampleTest").last().item_id
    )
    it_background = (
        item_type.objects.using(db_alias).filter(type_desc="background").last()
    )
    background_id = (
        item.objects.using(db_alias)
        .filter(parent_id=emib_sample_item_id, item_type_id=it_background, order=0)
        .last()
    )
    it_markdown = item_type.objects.using(db_alias).filter(type_desc="markdown").last()

    # get item objects
    i_org_structure_tree_view_title = (
        item.objects.using(db_alias)
        .filter(parent_id=background_id, item_type_id=it_markdown, order=6)
        .last()
    )

    i_org_structure_tree_view_description = (
        item.objects.using(db_alias)
        .filter(parent_id=background_id, item_type_id=it_markdown, order=7)
        .last()
    )

    # destroy item_text
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_title, language=l_english
    ).delete()
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_title, language=l_french
    ).delete()
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_description, language=l_english
    ).delete()
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_description, language=l_french
    ).delete()

    # destroy items; inverted order as children must be deleted first
    i_org_structure_tree_view_title.delete()
    i_org_structure_tree_view_description.delete()


class Migration(migrations.Migration):

    dependencies = [("custom_models", "0015_update_based_on_migration_13")]

    operations = [
        migrations.RunPython(
            upload_org_structure_tree_view_markdown,
            destroy_org_structure_tree_view_markdown,
        )
    ]

# Generated by Django 2.1.7 on 2019-05-30 14:07
# Edited by <NAME> to upload the organizatioanl structure tree view markdown content for the sample test

from django.db import migrations


def upload_org_structure_tree_view_markdown(apps, schema_editor):
    # get models
    language = apps.get_model("custom_models", "Language")
    item_type = apps.get_model("custom_models", "ItemType")
    item = apps.get_model("custom_models", "Item")
    item_text = apps.get_model("custom_models", "ItemText")
    test = apps.get_model("custom_models", "Test")

    # get db alias
    db_alias = schema_editor.connection.alias

    # lookup languages; do not use bulk_create since we need these objects later on
    l_english = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="en", ISO_Code_2="en-ca")
        .last()
    )
    l_french = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="fr", ISO_Code_2="fr-ca")
        .last()
    )
    emib_sample_item_id = (
        test.objects.using(db_alias).filter(test_name="emibSampleTest").last().item_id
    )
    it_background = (
        item_type.objects.using(db_alias).filter(type_desc="background").last()
    )
    background_id = (
        item.objects.using(db_alias)
        .filter(parent_id=emib_sample_item_id, item_type_id=it_background, order=0)
        .last()
    )
    it_markdown = item_type.objects.using(db_alias).filter(type_desc="markdown").last()

    # create items; do not use bulk_create since we need these objects later on
    # organizational structure items
    i_org_structure_tree_view_title = item(
        parent_id=background_id, item_type_id=it_markdown, order=6
    )
    i_org_structure_tree_view_title.save()

    i_org_structure_tree_view_description = item(
        parent_id=background_id, item_type_id=it_markdown, order=7
    )
    i_org_structure_tree_view_description.save()

    # bulk create item_text
    item_text.objects.using(db_alias).bulk_create(
        [
            item_text(
                item_id=i_org_structure_tree_view_title,
                text_detail="The Organizational Chart of the ODC",
                language=l_english,
            ),
            item_text(
                item_id=i_org_structure_tree_view_title,
                text_detail="Organigramme (CDO)",
                language=l_french,
            ),
            item_text(
                item_id=i_org_structure_tree_view_description,
                text_detail="Below is a tree view of the organization chart. Once selected, you can use the arrow keys to navigation, expand, and collapse information.",
                language=l_english,
            ),
            item_text(
                item_id=i_org_structure_tree_view_description,
                text_detail="FR Below is a tree view of the organization chart. Once selected, you can use the arrow keys to navigation, expand, and collapse information.",
                language=l_french,
            ),
        ]
    )


def destroy_org_structure_tree_view_markdown(apps, schema_editor):
    # get models
    language = apps.get_model("custom_models", "Language")
    item_type = apps.get_model("custom_models", "ItemType")
    item = apps.get_model("custom_models", "Item")
    item_text = apps.get_model("custom_models", "ItemText")
    test = apps.get_model("custom_models", "Test")
    # get db alias
    db_alias = schema_editor.connection.alias
    # get language objects
    l_english = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="en", ISO_Code_2="en-ca")
        .last()
    )
    l_french = (
        language.objects.using(db_alias)
        .filter(ISO_Code_1="fr", ISO_Code_2="fr-ca")
        .last()
    )
    # get item_type objects
    emib_sample_item_id = (
        test.objects.using(db_alias).filter(test_name="emibSampleTest").last().item_id
    )
    it_background = (
        item_type.objects.using(db_alias).filter(type_desc="background").last()
    )
    background_id = (
        item.objects.using(db_alias)
        .filter(parent_id=emib_sample_item_id, item_type_id=it_background, order=0)
        .last()
    )
    it_markdown = item_type.objects.using(db_alias).filter(type_desc="markdown").last()

    # get item objects
    i_org_structure_tree_view_title = (
        item.objects.using(db_alias)
        .filter(parent_id=background_id, item_type_id=it_markdown, order=6)
        .last()
    )

    i_org_structure_tree_view_description = (
        item.objects.using(db_alias)
        .filter(parent_id=background_id, item_type_id=it_markdown, order=7)
        .last()
    )

    # destroy item_text
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_title, language=l_english
    ).delete()
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_title, language=l_french
    ).delete()
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_description, language=l_english
    ).delete()
    item_text.objects.using(db_alias).filter(
        item_id=i_org_structure_tree_view_description, language=l_french
    ).delete()

    # destroy items; inverted order as children must be deleted first
    i_org_structure_tree_view_title.delete()
    i_org_structure_tree_view_description.delete()


class Migration(migrations.Migration):

    dependencies = [("custom_models", "0015_update_based_on_migration_13")]

    operations = [
        migrations.RunPython(
            upload_org_structure_tree_view_markdown,
            destroy_org_structure_tree_view_markdown,
        )
    ]
# Generated by Django 2.1.7 on 2019-05-30 14:07 # Edited by <NAME> to upload the organizatioanl structure tree view markdown content for the sample test # get models # get db alias # lookup languages; do not use bulk_create since we need these objects later on # create items; do not use bulk_create since we need these objects later on # organizational structure items # bulk create item_text # get models # get db alias # get language objects # get item_type objects # get item objects # destroy item_text # destroy items; inverted order as children must be deleted first