from django.db import migrations, models


def migrate_scope_forward(apps, schema_editor):
    Mlog = apps.get_model('wpm', 'Mlog')
    Mlog.objects.filter(stored_mgroup=True).update(stored_scope=10)


def migrate_scope_backward(apps, schema_editor):
    Mlog = apps.get_model('wpm', 'Mlog')
    Mlog.objects.filter(stored_scope=10).update(stored_mgroup=True)
    Mlog.objects.exclude(stored_scope=10).update(stored_mgroup=False)


class Migration(migrations.Migration):

    dependencies = [
        ('mtm', '0068_process_into_wm_scope'),
        ('wpm', '0134_alter_mlog_clear_defect'),
    ]

    operations = [
        migrations.AddField(
            model_name='mlog',
            name='stored_scope',
            field=models.PositiveSmallIntegerField(
                choices=[(10, '工段'), (20, '部门'), (30, '全局')],
                default=20,
                verbose_name='产出库存归属',
            ),
        ),
        migrations.RunPython(migrate_scope_forward, migrate_scope_backward),
        migrations.RemoveField(
            model_name='mlog',
            name='stored_mgroup',
        ),
    ]
