|
|
@ -9,7 +9,7 @@ from app import app, db |
|
|
|
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify, abort |
|
|
|
|
|
|
|
from app.forms import UserForm, UserForm_Edit |
|
|
|
from app.models import Book, BookSchema, Author |
|
|
|
from app.models import Book, BookSchema, Author, AuthorSchema |
|
|
|
from app.cover import get_cover |
|
|
|
|
|
|
|
import os |
|
|
@ -18,6 +18,8 @@ from werkzeug.utils import secure_filename |
|
|
|
# import sqlite3 |
|
|
|
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) |
|
|
|
|
|
|
|
author_schema = AuthorSchema() |
|
|
|
authors_schema = AuthorSchema(many=True) |
|
|
|
book_schema = BookSchema() |
|
|
|
books_schema = BookSchema(many=True) |
|
|
|
|
|
|
@ -79,19 +81,21 @@ 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.authors) |
|
|
|
user_form = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category ) |
|
|
|
|
|
|
|
if request.method == 'POST': |
|
|
|
if user_form.validate_on_submit(): |
|
|
|
# check if the post request has the file part |
|
|
|
title = user_form.title.data # You could also have used request.form['name'] |
|
|
|
authors = user_form.author.data # You could also have used request.form['email'] |
|
|
|
category = user_form.category.data |
|
|
|
# save user to database |
|
|
|
#book = Book(title, author, filename, cover, file_extension) |
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
book = Book.query.filter_by(id=id).first() |
|
|
|
book.title = title |
|
|
|
book.category = category |
|
|
|
book.authors= [] |
|
|
|
db.session.commit() |
|
|
|
for author in authors: |
|
|
@ -134,10 +138,10 @@ def add_book(): |
|
|
|
cover = get_cover(fullpath, name) |
|
|
|
title = user_form.title.data # You could also have used request.form['name'] |
|
|
|
authors = user_form.author.data # You could also have used |
|
|
|
tag = user_form.tag.data |
|
|
|
category = user_form.category.data |
|
|
|
#print(author) |
|
|
|
#print(len(author)) |
|
|
|
book = Book(title, filename, cover, file_extension, tag) |
|
|
|
book = Book(title, filename, cover, file_extension, category) |
|
|
|
db.session.add(book) |
|
|
|
for author in authors: |
|
|
|
author_name = author.get("author_name") |
|
|
@ -181,9 +185,7 @@ def show_author_by_id(id): |
|
|
|
|
|
|
|
@app.route('/authors/<int:id>/edit', methods=['POST', 'GET']) |
|
|
|
def edit_author_by_id(id): |
|
|
|
#EDIT AUTHOR TO DO |
|
|
|
return None |
|
|
|
|
|
|
|
return "Ask the programmer." |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -195,16 +197,17 @@ def edit_author_by_id(id): |
|
|
|
def get_books(): |
|
|
|
books = Book.query.all() |
|
|
|
data, errors = books_schema.dump(books) |
|
|
|
print(errors) |
|
|
|
return jsonify({'books': data}) |
|
|
|
|
|
|
|
@app.route('/api/books/<int:id>', methods=['GET']) |
|
|
|
def get_book_by_id(id): |
|
|
|
book = Book.query.get(id) |
|
|
|
book_result, error = book_schema.dump(book) |
|
|
|
if not book_result: |
|
|
|
data, error = book_schema.dump(book) |
|
|
|
if not data: |
|
|
|
return jsonify({"message": "Book could not be found."}), 400 |
|
|
|
else: |
|
|
|
return jsonify({'book': book_result}) |
|
|
|
return jsonify({'book': data }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|