No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

59 líneas
2.1KB

  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. import os
  15. book = Blueprint('book', __name__)
  16. # route for displaying all books in database
  17. @book.route('/books/')
  18. def get_books():
  19. books = Resource.query.filter_by(type='book')
  20. return render_template('resources.html', resources=books, type='book')
  21. # route for displaying a single book based on the ID in the database
  22. @book.route('/books/<int:book_id>/')
  23. def show_book(book_id):
  24. book = get_resource(book_id)
  25. links = get_linked_resources(book_id)
  26. return render_template('resource.html', resource=book, links=links)
  27. # route for editing a single book based on the ID in the database
  28. @book.route('/books/<int:book_id>/edit/', methods=('GET', 'POST'))
  29. @login_required
  30. def edit_book(book_id):
  31. book = get_resource(book_id)
  32. if request.method == 'POST':
  33. name = request.form['name']
  34. description = request.form['description']
  35. if not name:
  36. flash('Name is required!')
  37. else:
  38. book = Resource.query.get(book_id)
  39. book.name = name
  40. book.description = description
  41. db.session.commit()
  42. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  43. return render_template('edit.html', resource=book)
  44. # route for function to delete a single book from the edit page
  45. @book.route('/books/<int:book_id>/delete/', methods=('POST',))
  46. @login_required
  47. def delete_book(book_id):
  48. delete_resource(book_id)
  49. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))