|
- # @name: book.py
- # @creation_date: 2022-04-05
- # @license: The MIT License <https://opensource.org/licenses/MIT>
- # @author: Simon Bowie <ad7588@coventry.ac.uk>
- # @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 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/<int:book_id>/')
- def show_book(book_id):
- book = get_resource(book_id)
- links = get_linked_resources(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/<int:book_id>/edit/', methods=('GET', 'POST'))
- @login_required
- def edit_book(book_id):
- book = get_resource(book_id)
-
- if request.method == 'POST':
- name = request.form['name']
- description = request.form['description']
-
- if not name:
- flash('Name is required!')
- else:
- book = Resource.query.get(book_id)
- book.name = name
- book.description = 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/<int:book_id>/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')))
|