# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2022-08-11 08:32
from __future__ import unicode_literals
import os
import core.file_system
import core.model_utils
from core import files
from django.db import migrations
import journal.models
from django.core.files.base import ContentFile
from django.conf import settings
from submission import models


def copy_press_image_override_temp(apps, schema_editor):
    Journal = apps.get_model("journal", "Journal")
    for journal in Journal.objects.all():
        if journal.press_image_override:
            file_path = os.path.join(
                settings.BASE_DIR,
                "files",
                "journals",
                str(journal.pk),
                str(journal.press_image_override.uuid_filename),
            )
            if os.path.isfile(file_path):
                with open(file_path, "rb") as file:
                    image_file = ContentFile(file.read())
                    image_file.name = journal.press_image_override.original_filename
                    journal.press_image_override_temp.save(
                        journal.press_image_override.original_filename,
                        image_file,
                    )


class Migration(migrations.Migration):
    dependencies = [
        ("journal", "0053_display_article_images_settings"),
    ]

    operations = [
        migrations.AddField(
            model_name="journal",
            name="press_image_override_temp",
            field=core.model_utils.SVGImageField(
                blank=True,
                help_text="Replaces the press logo in the footer.",
                null=True,
                storage=core.file_system.JanewayFileSystemStorage(),
                upload_to=journal.models.cover_images_upload_path,
            ),
        ),
        migrations.RunPython(
            copy_press_image_override_temp, reverse_code=migrations.RunPython.noop
        ),
    ]
