# Generated by Django 2.1.5 on 2019-01-10 22:40

from django.db import migrations
import os
import csv

#Locate csv file to import
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
WORK_FILE = os.path.join(BASE_DIR, 'art_data.csv')

def insert_artworks(apps, schema_editor):
    Work = apps.get_model('core', 'Work')
    Category = apps.get_model('core', 'Category')
    with open(WORK_FILE, mode='r') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            work = Work.objects.create(
                title=row['title'],
                artist=row['artist_first_name'] + " " + row['artist_last_name'],
                about=row['description'],
                media=row['media'],
                measurements=row['measurements'],
                date=row['date'],
                location=row['location'],
                address=row['address'],
                lat=row['latitude'],
                long=row['longitude']
            )
            category_id = Category.objects.filter(name=row['classification']).values_list('pk', flat=True)
            work.category.set(category_id)

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0013_auto_20190110_insert_categories'),
    ]

    operations = [
        migrations.RunPython(insert_artworks)
    ]
