# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-14 11:01
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import geodata.models
from django.conf import settings
from django.core import serializers

import os


def load_data(apps, schema_editor):
    country_model = apps.get_model('geodata', 'Country')
    with open(os.path.join(settings.BASE_DIR, 'geodata', 'fixtures', 'countries.json'), 'rb') as data:
        countries = serializers.deserialize('json', data)
        country_model.objects.bulk_create((country.object for country in countries))

    region_model = apps.get_model('geodata', 'Region')
    with open(os.path.join(settings.BASE_DIR, 'geodata', 'fixtures', 'regions.json'), 'rb') as data:
        regiones = serializers.deserialize('json', data)
        region_model.objects.bulk_create((region.object for region in regiones))

    city_model = apps.get_model('geodata', 'City')
    with open(os.path.join(settings.BASE_DIR, 'geodata', 'fixtures', 'cities.json'), 'rb') as data:
        cities = serializers.deserialize('json', data)
        city_model.objects.bulk_create((city.object for city in cities))


def unload_data(apps, schema_editor):
    apps.get_model('geodata', 'Country').objects.all().delete()
    apps.get_model('geodata', 'Region').objects.all().delete()
    apps.get_model('geodata', 'City').objects.all().delete()


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
    ]

    operations = [
        migrations.CreateModel(
            name='City',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code', models.CharField(default=0, max_length=2, verbose_name='City code')),
                ('name_ru', models.CharField(max_length=255, verbose_name='City name (russian)')),
                ('name_en', models.CharField(max_length=255, verbose_name='City name (english)')),
            ],
        ),
        migrations.CreateModel(
            name='Country',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('group', models.PositiveSmallIntegerField(choices=[(0, 'Does not consist'), (1, 'European Union'), (2, 'Customs Union'), (3, 'Common market of South America'), (4, 'Andean Community'), (5, 'South Asian Union'), (6, 'The North American Union'), (7, 'Central American Common Market'), (8, 'South African Customs Union'), (9, 'Caribbean Community')], default=0)),
                ('code', models.CharField(default=0, max_length=2, verbose_name='Country code')),
                ('name_ru', models.CharField(max_length=255, verbose_name='Country name (russian)')),
                ('name_en', models.CharField(max_length=255, verbose_name='Country name (english)')),
                ('show', models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            name='NotExistCity',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('object_id', models.PositiveIntegerField(default=0)),
                ('code', models.CharField(default=0, max_length=2, verbose_name='City code')),
                ('name_en', models.CharField(max_length=255, verbose_name='City name (english)')),
                ('content_type', models.ForeignKey(default=geodata.models.get_region_default_id, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
            ],
        ),
        migrations.CreateModel(
            name='NotExistRegion',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code', models.CharField(default=0, max_length=2, verbose_name='Region code')),
                ('name_en', models.CharField(max_length=255, verbose_name='Region name (english)')),
                ('country', models.ForeignKey(default=geodata.models.get_country_default_id, on_delete=django.db.models.deletion.CASCADE, to='geodata.Country', verbose_name='Country')),
            ],
        ),
        migrations.CreateModel(
            name='Region',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code', models.CharField(default=0, max_length=2, verbose_name='Region code')),
                ('name_ru', models.CharField(max_length=255, verbose_name='Region name (russian)')),
                ('name_en', models.CharField(max_length=255, verbose_name='Region name (english)')),
                ('country', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='geodata.Country', verbose_name='Country')),
            ],
        ),
        migrations.AddField(
            model_name='city',
            name='region',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='geodata.Region', verbose_name='Region'),
        ),
        migrations.RunPython(
            load_data,
            reverse_code=unload_data
        ),

    ]
