Files @ 9a21d2c7818e
Branch filter:

Location: symposion_app/cms_pages/migrations/0014_auto_20160918_0358.py

Christopher Neugebauer
Schedule tweaks etc (#80)
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-09-18 03:58
from __future__ import unicode_literals

from django.db import migrations


def image_to_custom_image(apps, schema_editor):
    Image = apps.get_model("wagtailimages","Image")
    CustomImage = apps.get_model("cms_pages", "CustomImage")
    HomePage = apps.get_model("cms_pages", "HomePage")
    #tags = TaggableManager(help_text=None, blank=True, verbose_name=_('tags'))

    keys = (
        "title", "file", "width", "height", "created_at", "uploaded_by_user",
        "focal_point_y", "focal_point_x", "focal_point_width",
        "focal_point_height", "file_size",
    )

    customs = {}

    for image in Image.objects.all():
        kwargs = dict((key, getattr(image, key)) for key in keys)
        custom_image = CustomImage(**kwargs)

        # Does this actually work?!
        custom_image.tags = image.tags

        custom_image.save()

        customs[image.id] = custom_image

    def swap(customs, block, key):
        im = (block.value[key])
        if im is not None:
            block.value[key] = customs[im.id]

    # Go through the links.
    for page in HomePage.objects.all():
        for block in page.body:
            if block.block_type == "basic_content":
                swap(customs, block, "background_image")
            elif block.block_type == "keynotes":
                for keynote in block.value:
                    swap(customs, keynote, "profile_image")
        page.save()

    abstract_content_page_models = ["ContentPage", "NewsIndexPage", "NewsPage"]

    for model in abstract_content_page_models:
        Model = apps.get_model("cms_pages", model)

        for page in Model.objects.all():
            for block in page.body:
                if block.block_type == "floating_image" and block.value:
                    block.value = customs[block.value.id]

            if page.background_image:
                page.background_image_CUSTOM = customs[page.background_image.id]

            if model == "NewsPage" and page.portrait_image:
                page.portrait_image_CUSTOM = customs[page.portrait_image.id]

            page.save()

    Image.objects.all().delete()

class Migration(migrations.Migration):

    dependencies = [
        ('cms_pages', '0013_auto_20160918_0358'),
    ]

    operations = [
        migrations.RunPython(image_to_custom_image),
    ]