<filename>profiles/migrations/0026_profile_image_medium.py
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-28 17:04
from __future__ import unicode_literals
from uuid import uuid4

from django.db import migrations, models
from profiles.util import (
    IMAGE_MEDIUM_MAX_DIMENSION,
    make_thumbnail,
    profile_image_upload_uri_medium,
)


def populate_image_medium(apps, schema_editor):
    """
    Populate image_medium with thumbnail of image if it exists
    """
    Profile = apps.get_model('profiles.Profile')
    for profile in Profile.objects.all():
        if profile.image and not profile.image_medium:
            try:
                thumbnail = make_thumbnail(profile.image.file, IMAGE_MEDIUM_MAX_DIMENSION)
                profile.image_medium.save("{}.jpg".format(uuid4().hex), thumbnail)
            except OSError:
                pass


class Migration(migrations.Migration):

    dependencies = [
        ('profiles', '0025_populate_image_small'),
    ]

    operations = [
        migrations.AddField(
            model_name='profile',
            name='image_medium',
            field=models.ImageField(null=True, upload_to=profile_image_upload_uri_medium),
        ),
        migrations.RunPython(populate_image_medium, reverse_code=migrations.RunPython.noop),
    ]
