Import csv works! Many to many books and authors is also nice

This commit is contained in:
Alice 2018-05-23 17:02:06 +02:00
parent e6bc9a7ff9
commit d4f23ab507
8 changed files with 30 additions and 11 deletions

0
app/cover.py Normal file → Executable file
View File

View File

@ -1,6 +1,11 @@
from app import db from app import db
from marshmallow import Schema, fields, ValidationError, pre_load 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): class Book(db.Model):
__tablename__ = 'books' __tablename__ = 'books'
id = db.Column(db.Integer, primary_key = True) id = db.Column(db.Integer, primary_key = True)
@ -8,9 +13,10 @@ class Book(db.Model):
file = db.Column(db.String(255)) file = db.Column(db.String(255))
cover = db.Column(db.String(255)) cover = db.Column(db.String(255))
fileformat = db.Column(db.String(255)) fileformat = db.Column(db.String(255))
author = db.relationship('Author')
tag = db.Column(db.String(255)) 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): def __init__(self, title, file, cover, fileformat, tag):
self.title = title self.title = title
@ -30,7 +36,7 @@ class Book(db.Model):
class Author(db.Model): class Author(db.Model):
__tablename__ = 'authors' __tablename__ = 'authors'
id = db.Column(db.Integer(), primary_key=True) 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)) author_name = db.Column(db.String(50))

0
app/static/css/style.css Normal file → Executable file
View File

0
app/templates/edit_book_detail.html Normal file → Executable file
View File

0
app/templates/red_link.html Normal file → Executable file
View File

0
app/templates/show_book_detail.html Normal file → Executable file
View File

View File

@ -1,7 +1,7 @@
#import click #import click
#from flask import Flask #from flask import Flask
from app import app, db from app import app, db
from app.models import Book from app.models import Book, Author
from csv import DictReader from csv import DictReader
import argparse 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("csv", help = "csv file to import")
ap.add_argument("--limit", type=int, default = None, help = "limit to x number of x") ap.add_argument("--limit", type=int, default = None, help = "limit to x number of x")
args = ap.parse_args() 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__) #print(row['Title'], authors)
print(a)
print(book)
db.session.commit()
#print(args)
books = db.session.query(Book).all() books = db.session.query(Book).all()
#print(books)
# @app.cli.command()
# @click.argument('name')
# def import_csv(name):
# print("hello")

0
init.py Normal file → Executable file
View File