# Generated by Django 2.2.2 on 2019-06-26 20:09

from django.db import migrations
import os
import sys
import csv

from django.db import migrations
from django.conf import settings
from django.db import transaction

def load_data(apps, schema_editor):
    Book = apps.get_model('book', 'Book')
    Author = apps.get_model('book', 'Author')
    

    filename = os.path.join(settings.BASE_DIR, 'sample_books.csv')

    with open(filename) as file:
        
        reader = csv.DictReader(file) 
        for row in reader:
            author = Author(name=row["author"])
            author.save()
            book = Book(title=row['title'], author=author, url=row['url'], 
            description=row['description'])
            book.save()


class Migration(migrations.Migration):

    dependencies = [
        ('book', '0003_author_name'),
    ]

    operations = [
        migrations.RunPython(load_data)
    ]
