# -*- coding: utf-8 -*-
# Generated by Django 1.11.27 on 2020-03-30 10:18
from __future__ import unicode_literals

from django.db import migrations

updates = [
    {"from": "review_unassigned_article", "to": "review_home"},
    {"from": "publish_article", "to": "publish"},
    {"from": "article_copyediting", "to": "copyediting"},
]


def update_handshake_urls(apps, schema_editor):
    WorkflowElement = apps.get_model("core", "WorkflowElement")

    for update in updates:
        elements_to_update = WorkflowElement.objects.filter(
            handshake_url=update.get("from")
        )
        for element_to_update in elements_to_update:
            element_to_update.handshake_url = update.get("to")
            element_to_update.save()


def reverse_handshake_urls(apps, schema_editor):
    WorkflowElement = apps.get_model("core", "WorkflowElement")

    for update in updates:
        elements_to_update = WorkflowElement.objects.filter(
            handshake_url=update.get("to")
        )
        for element_to_update in elements_to_update:
            element_to_update.handshake_url = update.get("from")
            element_to_update.save()


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0033_set_default_xml_galley_xsl"),
    ]

    operations = [
        migrations.RunPython(
            update_handshake_urls,
            reverse_code=reverse_handshake_urls,
        ),
    ]
