# Generated by Django 3.0.5 on 2020-04-29 06:42

from django.conf import settings
from django.db import migrations, models
from django.db.models import Q
from django.utils.module_loading import import_string


def get_plugin(plugins, plugin_identifier):
    for plugin in plugins:
        if plugin.PLUGIN_ID == plugin_identifier:
            return plugin
    return None


def fill_plugins_name(apps, schema):
    plugins = []
    for plugin_path in settings.PLUGINS:
        plugins.append(import_string(plugin_path))

    plugins_configuration = apps.get_model(
        "plugins", "PluginConfiguration"
    ).objects.filter(Q(name="") | Q(name__isnull=True))

    for plugin_configuration in plugins_configuration:
        plugin = get_plugin(plugins, plugin_configuration.identifier)
        plugin_configuration.name = plugin.PLUGIN_NAME
        plugin_configuration.save()


class Migration(migrations.Migration):

    dependencies = [
        ("plugins", "0002_auto_20200417_0335"),
    ]

    operations = [
        migrations.RunPython(fill_plugins_name),
        migrations.AlterField(
            model_name="pluginconfiguration",
            name="name",
            field=models.CharField(max_length=128),
            preserve_default=False,
        ),
    ]
