# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2018-02-22 13:19


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

def migrate_photos(apps, schema_editor):
    Content = apps.get_model('content', 'Content')
    Photo = apps.get_model('photologue', 'Photo')

    for c in Content.objects.exclude(image=None).exclude(image=""):
        if not c.photo:
            try:
                if '/photo' in c.image:
                    c.photo = Photo.objects.get(image__endswith=c.image.split('/')[-1])
                else:
                    c.photo = Photo.objects.get(title=c.image)
            except Photo.DoesNotExist:
                continue
            c.save()


class Migration(migrations.Migration):

    dependencies = [
        ('content', '0009_auto_20171219_1320'),
        ('photologue', '0010_auto_20160105_1307'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='contentcontent',
            options={'verbose_name': 'related content', 'verbose_name_plural': 'related content'},
        ),
        migrations.AlterModelOptions(
            name='file',
            options={'ordering': ['order', 'file']},
        ),
        migrations.AlterModelOptions(
            name='link',
            options={'ordering': ['order', 'url']},
        ),
        migrations.AddField(
            model_name='content',
            name='photo',
            field=models.ForeignKey(blank=True, null=True, on_delete=photologue.models.Photo, related_name='main_photo', to='photologue.Photo'),
        ),
        migrations.AlterField(
            model_name='content',
            name='image',
            field=models.CharField(blank=True, editable=False, max_length=150, null=True),
        ),
        migrations.RunPython(migrate_photos),

    ]