# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-04-25 09:25
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import wooey.models.mixins


def createParsers(apps, schema_editor):
    ScriptParameters = apps.get_model('wooey', 'ScriptParameter')
    ScriptParser = apps.get_model('wooey', 'ScriptParser')

    for param in ScriptParameters.objects.all():
        script_version = param.script_version
        parser, created = ScriptParser.objects.get_or_create(script_version=script_version)
        param.parser = parser
        param.save()


class Migration(migrations.Migration):

    dependencies = [
        ('wooey', '0027_parameter_order'),
    ]

    operations = [
        migrations.CreateModel(
            name='ScriptParser',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(blank=True, max_length=255, null=True)),
                ('script_version', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='wooey.ScriptVersion')),
            ],
            bases=(wooey.models.mixins.WooeyPy2Mixin, models.Model),
        ),
        migrations.RunPython(createParsers),
        migrations.AddField(
            model_name='scriptparameter',
            name='parser',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='wooey.ScriptParser'),
            preserve_default=False,
        ),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-04-25 09:25
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import wooey.models.mixins


def createParsers(apps, schema_editor):
    ScriptParameter = apps.get_model('wooey', 'ScriptParameter')
    ScriptParser = apps.get_model('wooey', 'ScriptParser')

    for param in ScriptParameter.objects.all():
        script_version = param.script_version.last()
        parser, created = ScriptParser.objects.get_or_create(script_version=script_version)
        param.parser = parser
        param.save()


class Migration(migrations.Migration):

    dependencies = [
        ('wooey', '0027_parameter_order'),
    ]

    operations = [
        migrations.CreateModel(
            name='ScriptParser',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(blank=True, max_length=255, null=True)),
                ('script_version', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='wooey.ScriptVersion')),
            ],
            bases=(wooey.models.mixins.WooeyPy2Mixin, models.Model),
        ),
        migrations.RunPython(createParsers),
        migrations.AddField(
            model_name='scriptparameter',
            name='parser',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='wooey.ScriptParser'),
            preserve_default=False,
        ),
    ]

Fix scriptparser creation to fetch the last script version in migration