2018-05-23 15:58:16 +02:00
|
|
|
#import click
|
|
|
|
#from flask import Flask
|
|
|
|
from app import app, db
|
2018-05-23 17:02:06 +02:00
|
|
|
from app.models import Book, Author
|
2018-05-23 15:58:16 +02:00
|
|
|
from csv import DictReader
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
ap = argparse.ArgumentParser("import csv into flask")
|
|
|
|
ap.add_argument("csv", help = "csv file to import")
|
|
|
|
ap.add_argument("--limit", type=int, default = None, help = "limit to x number of x")
|
|
|
|
args = ap.parse_args()
|
2018-05-23 17:03:47 +02:00
|
|
|
with open(args.csv) as f:
|
|
|
|
for row in DictReader(f):
|
2018-05-23 17:02:06 +02:00
|
|
|
book = Book(row['Title'], '', '', row['Format'], row['Shelf'])
|
2018-05-23 17:03:47 +02:00
|
|
|
db.session.add(book)
|
2018-05-23 17:02:06 +02:00
|
|
|
authors = row['Author'].split(',')
|
|
|
|
authors = [x.strip() for x in authors]
|
|
|
|
for author in authors:
|
|
|
|
if author:
|
|
|
|
a = db.session.query(Author).filter_by(author_name=author).first()
|
|
|
|
if a == None:
|
|
|
|
a = Author(author_name=author)
|
|
|
|
db.session.add(a)
|
|
|
|
book.authors.append(a)
|
2018-05-23 17:03:47 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2018-05-23 15:58:16 +02:00
|
|
|
|
|
|
|
|
2018-05-23 17:53:30 +02:00
|
|
|
#books = db.session.query(Book).all()
|