# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-11-19 17:11
from __future__ import unicode_literals

from django.db import migrations

from cmsplugin_zinnia.choices_helpers import get_default_template


def fill_empty_template_value(apps, schema_editor):
    """
    Get every plugin entries to fill "template" field value if empty with
    default template
    """
    LatestEntriesPlugin = apps.get_model("cmsplugin_zinnia", "LatestEntriesPlugin")
    for entry in LatestEntriesPlugin.objects.all():
        if not entry.template_to_render:
            entry.template_to_render = get_default_template()
        entry.save()

    SelectedEntriesPlugin = apps.get_model("cmsplugin_zinnia", "SelectedEntriesPlugin")
    for entry in SelectedEntriesPlugin.objects.all():
        if not entry.template_to_render:
            entry.template_to_render = get_default_template()
        entry.save()

    RandomEntriesPlugin = apps.get_model("cmsplugin_zinnia", "RandomEntriesPlugin")
    for entry in RandomEntriesPlugin.objects.all():
        if not entry.template_to_render:
            entry.template_to_render = get_default_template()
        entry.save()

    QueryEntriesPlugin = apps.get_model("cmsplugin_zinnia", "QueryEntriesPlugin")
    for entry in QueryEntriesPlugin.objects.all():
        if not entry.template_to_render:
            entry.template_to_render = get_default_template()
        entry.save()


class Migration(migrations.Migration):

    dependencies = [
        ('cmsplugin_zinnia', '0003_add_default_template_value'),
    ]

    operations = [
        migrations.RunPython(fill_empty_template_value),
    ]
