made authorspage

master
Alex 7 years ago
commit 8c3fdea707

@ -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,6 @@ 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'))
author_name = db.Column(db.String(50)) author_name = db.Column(db.String(50))

@ -0,0 +1,16 @@
{% extends 'base.html' %}
{% block main %}
<div class="container">
<h1 class="header">{{ author.author_name }}</h1>
<p>Books: {% for book in author.books %}
<li> {{ book.title }}</li>
{% endfor %}</p>
</div>
{% endblock %}

@ -7,7 +7,7 @@
<img src="../uploads/cover/{{ book.cover }}" width="200"> <img src="../uploads/cover/{{ book.cover }}" width="200">
<p>Author(s): {% for author in book.author %} <p>Author(s): {% for author in book.authors %}
<li> {{ author.author_name }}</li> <li> {{ author.author_name }}</li>

@ -28,9 +28,9 @@
<td><img src="../uploads/cover/{{ book.cover }}" width="80"></td> <td><img src="../uploads/cover/{{ book.cover }}" width="80"></td>
<td><a href="books/{{ book.id }}">{{ book.title }}</a></td> <td><a href="books/{{ book.id }}">{{ book.title }}</a></td>
<td> {% for author in book.author %} <td> {% for author in book.authors %}
<li> {{ author.author_name }}</li> <li> <a href ="authors/{{ author.id }}">{{ author.author_name }}</a></li>
{% endfor %}</td> {% endfor %}</td>
<td>{{ book.fileformat }}</td> <td>{{ book.fileformat }}</td>

@ -78,7 +78,7 @@ def remove_book_by_id(id):
@app.route('/books/<int:id>/edit', methods=['POST', 'GET']) @app.route('/books/<int:id>/edit', methods=['POST', 'GET'])
def edit_book_by_id(id): def edit_book_by_id(id):
book_to_edit = Book.query.filter_by(id=id).first() 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 request.method == 'POST':
if user_form.validate_on_submit(): if user_form.validate_on_submit():
@ -91,10 +91,10 @@ def edit_book_by_id(id):
db.session.commit() db.session.commit()
book = Book.query.filter_by(title=title).first() 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: for this_author in author:
this_author = Author(this_author.get('author_name')) this_author = Author(this_author.get('author_name'))
book.author.append(this_author) book.authors.append(this_author)
db.session.commit() db.session.commit()
flash("%s updated" % (title)) flash("%s updated" % (title))
@ -127,17 +127,20 @@ def add_book():
file.save(fullpath) file.save(fullpath)
cover = get_cover(fullpath, name) cover = get_cover(fullpath, name)
title = user_form.title.data # You could also have used request.form['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 tag = user_form.tag.data
print(author) #print(author)
print(len(author)) #print(len(author))
book = Book(title, filename, cover, file_extension, tag) book = Book(title, filename, cover, file_extension, tag)
db.session.add(book) db.session.add(book)
db.session.commit() for author in authors:
book = Book.query.filter_by(title=title).first() author_name = author.get("author_name")
for this_author in author: if author_name:
this_author = Author(this_author.get('author_name')) a = db.session.query(Author).filter_by(author_name=author_name).first()
book.author.append(this_author) if a == None:
a = Author(author_name=author_name)
db.session.add(a)
book.authors.append(a)
db.session.commit() db.session.commit()
# save user to database # save user to database
@ -159,6 +162,13 @@ def flash_errors(form):
error error
)) ))
@app.route('/authors/<int:id>')
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 # The API

@ -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,20 +9,21 @@ 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: with open(args.csv) as f:
for row in DictReader(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) 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() db.session.commit()
#app = Flask(__name__)
#books = db.session.query(Book).all()
#print(books)
# @app.cli.command() #books = db.session.query(Book).all()
# @click.argument('name')
# def import_csv(name):
# print("hello")

Loading…
Cancel
Save