# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2020-08-13 13:47
from __future__ import unicode_literals

from django.db import migrations

REST_BASE = (
    "{{ request.press_base_url }}{% url 'core_reset_password' reset_token.token %}"
)
RESET_NEW = "{% site_url 'core_reset_password' reset_token.token %}"

REG_BASE = "{{ request.press_base_url }}{% url 'core_confirm_account' user.confirmation_code %}"
REG_NEW = "{% site_url 'core_confirm_account' user.confirmation_code %}"


def update_press_templates(apps, schema_editor):
    Press = apps.get_model("press", "Press")
    try:
        press_obj = Press.objects.all()[0]

        press_obj.password_reset_text = press_obj.password_reset_text.replace(
            REST_BASE,
            RESET_NEW,
        )
        press_obj.registration_text = press_obj.registration_text.replace(
            REG_BASE,
            REG_NEW,
        )

        press_obj.save()
    except IndexError:
        pass


class Migration(migrations.Migration):
    dependencies = [
        ("press", "0023_auto_20200718_1117"),
    ]

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