您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # @name: book.py
  2. # @creation_date: 2022-04-05
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: book route for book-related functions and pages
  6. # @acknowledgements:
  7. # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
  8. from flask import Blueprint, render_template, request, flash, redirect, url_for
  9. from flask_login import login_required, current_user
  10. from .models import Resource
  11. from .resources import *
  12. from werkzeug.exceptions import abort
  13. from . import db
  14. book = Blueprint('book', __name__)
  15. # route for displaying all books in database
  16. @book.route('/books')
  17. def get_books():
  18. books = Resource.query.filter_by(type='book')
  19. return render_template('resources.html', resources=books, type='book')
  20. # route for displaying a single book based on the ID in the database
  21. @book.route('/books/<int:book_id>')
  22. def show_book(book_id):
  23. book = get_resource(book_id)
  24. links = get_linked_resources(book_id)
  25. return render_template('resource.html', resource=book, links=links)
  26. # route for editing a single book based on the ID in the database
  27. @book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))
  28. @login_required
  29. def edit_book(book_id):
  30. book = get_resource(book_id)
  31. if request.method == 'POST':
  32. name = request.form['name']
  33. description = request.form['description']
  34. if not name:
  35. flash('Name is required!')
  36. else:
  37. book = Resource.query.get(book_id)
  38. book.name = name
  39. book.description = description
  40. db.session.commit()
  41. return redirect(url_for('book.get_books'))
  42. return render_template('edit.html', resource=book)
  43. # route for function to delete a single book from the edit page
  44. @book.route('/books/<int:book_id>/delete', methods=('POST',))
  45. @login_required
  46. def delete_book(book_id):
  47. delete_resource(book_id)
  48. return redirect(url_for('book.get_books'))