# -*- coding: utf-8 -*-
# Generated by Django 1.11.17 on 2018-12-19 11:31
from __future__ import unicode_literals

import csv
from django.db import migrations

def ImportSpeciesElements(apps, schema_editor):
    Element = apps.get_model('elements', 'Element')
    Species = apps.get_model('species',  'Species')

    with open('veekun/pokedex/data/csv/pokemon_types.csv', 'r') as se_f:
        data = csv.DictReader(se_f.read().splitlines())
    for row in data:
        try:
            p = Species.objects.get(pk=row['pokemon_id'])
            t = Element.objects.get(pk=row['type_id'])
            p.types.add(t)
            p.save()
        except (Species.DoesNotExist,):
            pass

class Migration(migrations.Migration):

    dependencies = [
        ('elements', '0002_importelements'),
        ('species',  '0002_importspecies' ),
    ]

    operations = [
        migrations.RunPython(ImportSpeciesElements),
    ]
