# Generated by Django 2.2.2 on 2019-06-27 19:59

from django.db import migrations
import os 
import csv
from django.conf import settings

def load_books(apps, schema_editor):
    Book = apps.get_model('core', 'Book')

    Book.objects.all().delete()

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

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


def delete_books(apps, schema_editor):
    Book = apps.get_model('core', 'Book')
    Book.objects.all().delete()



class Migration(migrations.Migration):

    dependencies = [
        ('core', '0006_book_url'),
    ]

    operations = [migrations.RunPython(load_books, delete_books)
    ]



