# Generated by Django 3.1 on 2021-05-23 11:14

import csv
from django.db import migrations
from django.apps.registry import apps

class Migration(migrations.Migration):

    dependencies = [
        ('mikoa', '0001_initial'),
    ]

    def load_data(self, schema_editor):
        Region = apps.get_model('mikoa', 'Region')
        District = apps.get_model('mikoa', 'District')

        mikoa = open('mikoa/mikoa.csv')
        wilaya = open('mikoa/wilaya.csv')

        mikoa_reader = csv.reader(mikoa)
        wilaya_reader = csv.reader(wilaya)

        Region.objects.all().delete()
        District.objects.all().delete()

        next(mikoa_reader)
        for row in mikoa_reader:

            r, created = Region.objects.get_or_create(
                id = row[0],
                name = row[1],
                capital = row[2],
                districts = row[3],
                area = row[4],
                population = row[5],
                postcode = row[6],
                zone = row[7]
            )

        next(wilaya_reader)
        for row in wilaya_reader:

            r, created = District.objects.get_or_create(
                id = row[0],
                region_id = row[1],
                name = row[2],
                population = row[3],
            )

    operations = [
        migrations.RunPython(
            load_data,
            migrations.RunPython.noop
        ),
    ]
