Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2 лет назад
2 лет назад
2 лет назад
2 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 .relationships import *
  13. from werkzeug.exceptions import abort
  14. from . import db
  15. import os
  16. book = Blueprint('book', __name__)
  17. # route for displaying all books in database
  18. @book.route('/books')
  19. def get_books():
  20. books = Resource.query.filter_by(type='book')
  21. return render_template('resources.html', resources=books, type='book')
  22. # route for displaying a single book based on the ID in the database
  23. @book.route('/books/<int:book_id>')
  24. def show_book(book_id):
  25. book = get_resource(book_id)
  26. links = get_relationships(book_id)
  27. return render_template('resource.html', resource=book, links=links)
  28. # route for editing a single book based on the ID in the database
  29. @book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))
  30. @login_required
  31. def edit_book(book_id):
  32. book = get_resource(book_id)
  33. if request.method == 'POST':
  34. if not request.form['name']:
  35. flash('Name is required!')
  36. else:
  37. book = Resource.query.get(book_id)
  38. book.name = request.form['name']
  39. book.description = request.form['description']
  40. db.session.commit()
  41. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  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',_external=True,_scheme=os.environ.get('SSL_SCHEME')))