# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-11-22 14:20
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import mptt.fields


def migrate_business_module_data(apps, schema_editor):
    old_model = apps.get_model('issue', 'BusinessModule')
    new_model = apps.get_model('issue', 'BusinessModuleTree')
    tree_id = 1
    for obj in old_model.objects.all():
        print(obj.name)
        new_model.objects.create(name=obj.name, description=obj.description, tree_id=tree_id,
                                 lft=1, rght=2, level=0, parent=None)
        tree_id += 1


class Migration(migrations.Migration):

    dependencies = [
        ('issue', '0020_auto_20171117_1705'),
    ]

    operations = [
        migrations.CreateModel(
            name='BusinessModuleTree',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, unique=True, verbose_name='业务模块名称')),
                ('description', models.CharField(blank=True, max_length=256, null=True, verbose_name='业务模块描述')),
                ('lft', models.PositiveIntegerField(db_index=True, editable=False)),
                ('rght', models.PositiveIntegerField(db_index=True, editable=False)),
                ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
                ('level', models.PositiveIntegerField(db_index=True, editable=False)),
                ('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='issue.BusinessModuleTree')),
            ],
        ),
        migrations.RunPython(migrate_business_module_data, reverse_code=migrations.RunPython.noop),
        migrations.DeleteModel(
            name='BusinessModule',
        ),
    ]
