# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-05-23 17:58
from __future__ import unicode_literals

from django.db import migrations


def replace_author_review_setting(apps, schema_editor):
    try:
        Setting = apps.get_model("core", "Setting")
        SettingValue = apps.get_model("core", "SettingValue")
        SettingValueTranslation = apps.get_model("core", "SettingValueTranslation")
        Journal = apps.get_model("journal", "Journal")

        setting = Setting.objects.filter(
            name="copyeditor_notify_author",
            group__name="email",
        )

        journals = Journal.objects.all()

        values_to_replace = [
            "{{journal.site_url}}{{ url }}",
            "{{request.journal.site_url}}{{ url }}",
        ]

        for journal in journals:
            values = SettingValue.objects.filter(journal=journal, setting=setting)

            for value in values:
                translations = SettingValueTranslation.objects.filter(
                    master_id=value.pk
                )

                for translation in translations:
                    value = translation.value
                    for value_to_replace in values_to_replace:
                        translation.value = value.replace(
                            value_to_replace,
                            "{% journal_url 'author_copyedit' article.pk author_review.pk %}",
                        )
                    translation.save()
    except LookupError:
        pass


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0002_auto_20170711_1203"),
        ("copyediting", "0004_auto_20180412_1544"),
    ]

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