# -*- 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
from django.conf import settings
from django.core import serializers

import os


def load_data_country(apps, schema_editor):
    country_code_model = apps.get_model('core', 'CountryNumberphone')
    with open(os.path.join(settings.BASE_DIR, 'core', 'fixtures', 'country_codes.json'), 'rb') as data:
        country_codes = serializers.deserialize('json', data)
        country_code_model.objects.bulk_create((country_code.object for country_code in country_codes))


def unload_data_country(apps, schema_editor):
    apps.get_model('core', 'CountryNumberphone').objects.all().delete()


def load_data_language(apps, schema_editor):
    obj_model = apps.get_model('core', 'Language')
    with open(os.path.join(settings.BASE_DIR, 'core', 'fixtures', 'languages.json'), 'rb') as data:
        objs = serializers.deserialize('json', data)
        obj_model.objects.bulk_create((obj.object for obj in objs))


def unload_data_language(apps, schema_editor):
    apps.get_model('core', 'Language').objects.all().delete()



class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('core', '0001_initial'),
        ('geodata', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='countrynumberphone',
            name='country',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='geodata.Country'),
        ),
        migrations.RunPython(
            load_data_country,
            reverse_code=unload_data_country
        ),
        migrations.RunPython(
            load_data_language,
            reverse_code=unload_data_language
        ),

    ]
