# Generated by Django 2.1.3 on 2019-02-02 10:13
import os
import json

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


def insert_forms(apps,schema_editor):
    with open(os.path.abspath('./master_data/mapper/challan_mappings.json')) as json_file:
        data = json.load(json_file)
        user_data = apps.get_model('auth.User')
        user = user_data()
        user.first_name = 'setUpUser'
        user.last_name = 'setUpUser'
        user.username = 'setUpUser@gmail.com'
        user.email = 'setUpUser@gmail.com'
        user.save()
        for obj in data:
            challan_type = apps.get_model('master_data','ChallanTypes')
            ctype = challan_type()
            ctype.name = obj['type']
            ctype.created_by = user
            ctype.updated_by = user
            ctype.save()
            for columns_details in obj['columns']:
                column = columns_details.split('-')
                challan_column = apps.get_model('master_data', 'ChallanColumns')
                ccolumn = challan_column()
                ccolumn.column = column[0]
                ccolumn.type = column[1]
                ccolumn.challan_id = ctype
                ccolumn.created_by = user
                ccolumn.updated_by = user
                ccolumn.save()


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ChallanColumns',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_on', models.DateTimeField(auto_now_add=True)),
                ('updated_on', models.DateTimeField(auto_now=True)),
                ('deleted', models.BooleanField(default=False)),
                ('column', models.TextField()),
                ('type', models.TextField()),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ChallanTypes',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_on', models.DateTimeField(auto_now_add=True)),
                ('updated_on', models.DateTimeField(auto_now=True)),
                ('deleted', models.BooleanField(default=False)),
                ('name', models.TextField()),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='challantypes_created_by', to=settings.AUTH_USER_MODEL)),
                ('updated_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='challantypes_updated_by', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='UserProfile',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('external_id', models.TextField()),
                ('token', models.TextField(null=True)),
                ('middle_name', models.TextField(null=True)),
                ('user', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='Workspace',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_on', models.DateTimeField(auto_now_add=True)),
                ('updated_on', models.DateTimeField(auto_now=True)),
                ('deleted', models.BooleanField(default=False)),
                ('external_id', models.TextField(unique=True)),
                ('source_created_at', models.DateTimeField(null=True)),
                ('source_updated_at', models.DateTimeField(null=True)),
                ('source_created_by', models.TextField(null=True)),
                ('source_updated_by', models.TextField(null=True)),
                ('name', models.TextField()),
                ('description', models.TextField(null=True)),
                ('current_status', models.TextField(null=True)),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='workspace_created_by', to=settings.AUTH_USER_MODEL)),
                ('updated_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='workspace_updated_by', to=settings.AUTH_USER_MODEL)),
                ('users', models.ManyToManyField(to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='challancolumns',
            name='challan_id',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='master_data.ChallanTypes'),
        ),
        migrations.AddField(
            model_name='challancolumns',
            name='created_by',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='challancolumns_created_by', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='challancolumns',
            name='updated_by',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='challancolumns_updated_by', to=settings.AUTH_USER_MODEL),
        ),
        migrations.RunPython(insert_forms)
    ]
