From d4f23ab5074057dbf68d9bf782c5fd5227287a25 Mon Sep 17 00:00:00 2001 From: Alice Date: Wed, 23 May 2018 17:02:06 +0200 Subject: [PATCH] Import csv works! Many to many books and authors is also nice --- app/cover.py | 0 app/models.py | 10 ++++++++-- app/static/css/style.css | 0 app/templates/edit_book_detail.html | 0 app/templates/red_link.html | 0 app/templates/show_book_detail.html | 0 import_csv.py | 31 ++++++++++++++++++++--------- init.py | 0 8 files changed, 30 insertions(+), 11 deletions(-) mode change 100644 => 100755 app/cover.py mode change 100644 => 100755 app/static/css/style.css mode change 100644 => 100755 app/templates/edit_book_detail.html mode change 100644 => 100755 app/templates/red_link.html mode change 100644 => 100755 app/templates/show_book_detail.html mode change 100644 => 100755 init.py diff --git a/app/cover.py b/app/cover.py old mode 100644 new mode 100755 diff --git a/app/models.py b/app/models.py index 3b654f2..66a1970 100755 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,11 @@ from app import db from marshmallow import Schema, fields, ValidationError, pre_load +authors = db.Table('books_authors', + db.Column('book_id', db.Integer, db.ForeignKey('books.id'), primary_key=True), + db.Column('author_id', db.Integer, db.ForeignKey('authors.id'), primary_key=True) +) + class Book(db.Model): __tablename__ = 'books' id = db.Column(db.Integer, primary_key = True) @@ -8,9 +13,10 @@ class Book(db.Model): file = db.Column(db.String(255)) cover = db.Column(db.String(255)) fileformat = db.Column(db.String(255)) - author = db.relationship('Author') tag = db.Column(db.String(255)) + authors = db.relationship('Author', secondary=authors, lazy='subquery', + backref=db.backref('books', lazy=True)) def __init__(self, title, file, cover, fileformat, tag): self.title = title @@ -30,7 +36,7 @@ class Book(db.Model): class Author(db.Model): __tablename__ = 'authors' id = db.Column(db.Integer(), primary_key=True) - user_id = db.Column(db.Integer(), db.ForeignKey('books.id')) + book_id = db.Column(db.Integer(), db.ForeignKey('books.id')) author_name = db.Column(db.String(50)) diff --git a/app/static/css/style.css b/app/static/css/style.css old mode 100644 new mode 100755 diff --git a/app/templates/edit_book_detail.html b/app/templates/edit_book_detail.html old mode 100644 new mode 100755 diff --git a/app/templates/red_link.html b/app/templates/red_link.html old mode 100644 new mode 100755 diff --git a/app/templates/show_book_detail.html b/app/templates/show_book_detail.html old mode 100644 new mode 100755 diff --git a/import_csv.py b/import_csv.py index f40102b..62833f6 100644 --- a/import_csv.py +++ b/import_csv.py @@ -1,7 +1,7 @@ #import click #from flask import Flask from app import app, db -from app.models import Book +from app.models import Book, Author from csv import DictReader import argparse @@ -9,14 +9,27 @@ 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() +with open(args.csv) as f: + for row in DictReader(f): + book = Book(row['Title'], '', '', row['Format'], row['Shelf']) + db.session.add(book) + 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) -print(args) -#app = Flask(__name__) -books = db.session.query(Book).all() -#print(books) + #print(row['Title'], authors) + print(a) + -# @app.cli.command() -# @click.argument('name') -# def import_csv(name): -# print("hello") + print(book) +db.session.commit() +#print(args) + +books = db.session.query(Book).all() diff --git a/init.py b/init.py old mode 100644 new mode 100755