# @name: book.py # @creation_date: 2022-04-05 # @license: The MIT License # @author: Simon Bowie # @purpose: book route for book-related functions and pages # @acknowledgements: # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3 from flask import Blueprint, render_template, request, flash, redirect, url_for from flask_login import login_required, current_user from .models import Resource from .resources import * from .relationships import * from werkzeug.exceptions import abort from . import db import os book = Blueprint('book', __name__) # route for displaying all books in database @book.route('/books') def get_books(): books = Resource.query.filter_by(type='book') return render_template('resources.html', resources=books, type='book') # route for displaying a single book based on the ID in the database @book.route('/books/') def show_book(book_id): book = get_resource(book_id) links = get_relationships(book_id) return render_template('resource.html', resource=book, links=links) # route for editing a single book based on the ID in the database @book.route('/books//edit', methods=('GET', 'POST')) @login_required def edit_book(book_id): book = get_resource(book_id) if request.method == 'POST': if not request.form['name']: flash('Name is required!') else: book = Resource.query.get(book_id) book.name = request.form['name'] book.description = request.form['description'] db.session.commit() return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME'))) return render_template('edit.html', resource=book) # route for function to delete a single book from the edit page @book.route('/books//delete', methods=('POST',)) @login_required def delete_book(book_id): delete_resource(book_id) return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))