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..ab40883 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,6 @@ 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')) 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_author_detail.html b/app/templates/show_author_detail.html new file mode 100644 index 0000000..2129270 --- /dev/null +++ b/app/templates/show_author_detail.html @@ -0,0 +1,16 @@ +{% extends 'base.html' %} + +{% block main %} +
+ +

{{ author.author_name }}

+ + +

Books: {% for book in author.books %} + +

  • {{ book.title }}
  • + + {% endfor %}

    + +
    +{% endblock %} diff --git a/app/templates/show_book_detail.html b/app/templates/show_book_detail.html old mode 100644 new mode 100755 index 3716923..044deee --- a/app/templates/show_book_detail.html +++ b/app/templates/show_book_detail.html @@ -7,7 +7,7 @@ -

    Author(s): {% for author in book.author %} +

    Author(s): {% for author in book.authors %}

  • {{ author.author_name }}
  • diff --git a/app/templates/show_books.html b/app/templates/show_books.html index ca0acb1..3eef110 100755 --- a/app/templates/show_books.html +++ b/app/templates/show_books.html @@ -28,9 +28,9 @@ {{ book.title }} - {% for author in book.author %} + {% for author in book.authors %} -
  • {{ author.author_name }}
  • +
  • {{ author.author_name }}
  • {% endfor %} {{ book.fileformat }} diff --git a/app/views.py b/app/views.py index 4ac30f8..1f9f125 100755 --- a/app/views.py +++ b/app/views.py @@ -78,7 +78,7 @@ def remove_book_by_id(id): @app.route('/books//edit', methods=['POST', 'GET']) def edit_book_by_id(id): book_to_edit = Book.query.filter_by(id=id).first() - user_form = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.author) + user_form = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.authors) if request.method == 'POST': if user_form.validate_on_submit(): @@ -91,10 +91,10 @@ def edit_book_by_id(id): db.session.commit() book = Book.query.filter_by(title=title).first() - author_table = Author.query.filter_by(user_id=book.id).delete() + author_table = Author.query.filter_by(book_id=book.id).delete() for this_author in author: this_author = Author(this_author.get('author_name')) - book.author.append(this_author) + book.authors.append(this_author) db.session.commit() flash("%s updated" % (title)) @@ -127,18 +127,21 @@ def add_book(): file.save(fullpath) cover = get_cover(fullpath, name) title = user_form.title.data # You could also have used request.form['name'] - author = user_form.author.data # You could also have used + authors = user_form.author.data # You could also have used tag = user_form.tag.data - print(author) - print(len(author)) + #print(author) + #print(len(author)) book = Book(title, filename, cover, file_extension, tag) db.session.add(book) + for author in authors: + author_name = author.get("author_name") + if author_name: + a = db.session.query(Author).filter_by(author_name=author_name).first() + if a == None: + a = Author(author_name=author_name) + db.session.add(a) + book.authors.append(a) db.session.commit() - book = Book.query.filter_by(title=title).first() - for this_author in author: - this_author = Author(this_author.get('author_name')) - book.author.append(this_author) - db.session.commit() # save user to database @@ -159,6 +162,13 @@ def flash_errors(form): error )) +@app.route('/authors/') +def show_author_by_id(id): + author = Author.query.get(id) + if not author: + abort(404) + else: + return render_template('show_author_detail.html', author=author) ### # The API diff --git a/import_csv.py b/import_csv.py index 6a7e89d..c6eb773 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,20 +9,21 @@ 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): - #print(row['Title']) - book = Book (row['Title'], "", "", row['Format'], row['Shelf']) + 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) db.session.commit() -#app = Flask(__name__) -#books = db.session.query(Book).all() -#print(books) -# @app.cli.command() -# @click.argument('name') -# def import_csv(name): -# print("hello") +#books = db.session.query(Book).all() diff --git a/init.py b/init.py old mode 100644 new mode 100755