Browse Source

made authorspage

ansible-setup-and-deploy
Alex 6 years ago
parent
commit
8c3fdea707
  1. 0
      app/cover.py
  2. 9
      app/models.py
  3. 0
      app/static/css/style.css
  4. 0
      app/templates/edit_book_detail.html
  5. 0
      app/templates/red_link.html
  6. 16
      app/templates/show_author_detail.html
  7. 2
      app/templates/show_book_detail.html
  8. 4
      app/templates/show_books.html
  9. 32
      app/views.py
  10. 23
      import_csv.py
  11. 0
      init.py

0
app/cover.py

9
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))

0
app/static/css/style.css

0
app/templates/edit_book_detail.html

0
app/templates/red_link.html

16
app/templates/show_author_detail.html

@ -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 %}

2
app/templates/show_book_detail.html

@ -7,7 +7,7 @@
<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>

4
app/templates/show_books.html

@ -28,9 +28,9 @@
<td><img src="../uploads/cover/{{ book.cover }}" width="80"></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>
<td>{{ book.fileformat }}</td>

32
app/views.py

@ -78,7 +78,7 @@ def remove_book_by_id(id):
@app.route('/books/<int:id>/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/<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

23
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()

0
init.py

Loading…
Cancel
Save