# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2017-10-25 14:41
from __future__ import unicode_literals

from django.db import migrations

from django.db import migrations, models
import django.db.models.deletion


def copy_page_block_data_fwd(apps, schema_editor):
    PageBlockTranslation = apps.get_model('page_block', 'PageBlockTranslation')
    BasePageBlockTranslation = apps.get_model('page_block', 'BasePageBlockTranslation')

    for trans in BasePageBlockTranslation.objects.all():
        assert not PageBlockTranslation.objects.filter(pk=trans.pk).exists()
        PageBlockTranslation.objects.create(
            pk=trans.pk,
            base_ptr=trans,
            page_block=trans.page_block,
            cover_image=trans.cover_image_old,
            title=trans.title_old,
            contents_md=trans.contents_md_old,
            contents_html=trans.contents_html_old
        )


def copy_page_block_data_reverse(apps, schema_editor):
    PageBlockTranslation = apps.get_model('page_block', 'PageBlockTranslation')

    for trans in PageBlockTranslation.objects.all():
        base = trans.base_ptr
        base.cover_image_old = trans.cover_image
        base.title_old = trans.title
        base.contents_md_old = trans.contents_md
        base.contents_html_old = trans.contents_html
        base.save()


class Migration(migrations.Migration):

    dependencies = [
        ('page_block', '0004_auto_20171007_1749'),
    ]

    operations = [
        migrations.RenameModel(
            old_name='PageBlockTranslation',
            new_name='BasePageBlockTranslation',
        ),
        migrations.RenameField(
            model_name='basepageblocktranslation',
            old_name='contents_html',
            new_name='contents_html_old',
        ),
        migrations.RenameField(
            model_name='basepageblocktranslation',
            old_name='contents_md',
            new_name='contents_md_old',
        ),
        migrations.RenameField(
            model_name='basepageblocktranslation',
            old_name='cover_image',
            new_name='cover_image_old',
        ),
        migrations.RenameField(
            model_name='basepageblocktranslation',
            old_name='title',
            new_name='title_old',
        ),
        migrations.CreateModel(
            name='PageBlockTranslation',
            fields=[
                ('base_ptr', models.OneToOneField(                                                                      on_delete=django.db.models.deletion.CASCADE,
                  parent_link=True,
                  primary_key=True,
                  serialize=False,
                  to='page_block.BasePageBlockTranslation')
                 ),
                ('cover_image', models.FileField(blank=True, null=True, upload_to='')),
                ('title', models.CharField(blank=True, max_length=1024)),
                ('contents_md', models.TextField()),
                ('contents_html', models.TextField(editable=False)),
            ],
            bases=('page_block.basepageblocktranslation',),
        ),
        migrations.RunPython(
            copy_page_block_data_fwd, copy_page_block_data_reverse
        ),
        migrations.RemoveField(
            model_name='basepageblocktranslation',
            name='contents_html_old',
        ),
        migrations.RemoveField(
            model_name='basepageblocktranslation',
            name='contents_md_old',
        ),
        migrations.RemoveField(
            model_name='basepageblocktranslation',
            name='cover_image_old',
        ),
        migrations.RemoveField(
            model_name='basepageblocktranslation',
            name='title_old',
        )
    ]
