# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-05-30 11:38
from __future__ import unicode_literals
import os

from django.db import migrations
from django.core.files.base import ContentFile


def create_images(apps, schema_editor):
    ModelBase = apps.get_model("jmbo", "ModelBase")
    Image = apps.get_model("jmbo", "Image")
    ModelBaseImage = apps.get_model("jmbo", "ModelBaseImage")

    for obj in ModelBase.objects.all():
        if obj.image:
            image = Image.objects.create(
                title="Canonical image", subtitle="For #%s" % obj.pk
            )
            image.image.save(
                os.path.basename(obj.image.path),
                ContentFile(open(obj.image.path, "rb").read())
            )
            ModelBaseImage.objects.create(modelbase=obj, image=image)


class Migration(migrations.Migration):

    dependencies = [
        ("jmbo", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(create_images),
    ]
