Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

83 Zeilen
3.5KB

  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. type = 'book'
  21. books = Resource.query.filter_by(type=type)
  22. for key in request.args.keys():
  23. if key == 'practice':
  24. query = 'SELECT Resource.* FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id WHERE Relationship.second_resource_id=' + request.args.get(key) + ' AND Resource.type="' + type + '";'
  25. books = db.engine.execute(query)
  26. else:
  27. kwargs = {'type': type, key: request.args.get(key)}
  28. books = Resource.query.filter_by(**kwargs)
  29. # get filters
  30. # practices
  31. practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name)
  32. # year
  33. year_filter = get_filter_values('year', type)
  34. return render_template('resources.html', resources=books, type=type, practices_filter=practices_filter, year_filter=year_filter)
  35. # route for displaying a single book based on the ID in the database
  36. @book.route('/books/<int:book_id>')
  37. def show_book(book_id):
  38. book = get_resource(book_id)
  39. relationships = get_relationships(book_id)
  40. book_data = get_book_data(book.isbn)
  41. return render_template('book.html', resource=book, relationships=relationships, book=book_data)
  42. # route for editing a single book based on the ID in the database
  43. @book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))
  44. @login_required
  45. def edit_book(book_id):
  46. book = get_resource(book_id)
  47. resource_dropdown = Resource.query
  48. existing_relationships = get_relationships(book_id)
  49. if request.method == 'POST':
  50. if not request.form['name']:
  51. flash('Name is required!')
  52. else:
  53. book = Resource.query.get(book_id)
  54. book.name = request.form['name']
  55. book.description = request.form['description']
  56. book.author = request.form['author']
  57. book.year = request.form['year']
  58. book.bookUrl = request.form['bookUrl']
  59. book.isbn = request.form['isbn']
  60. db.session.commit()
  61. linked_resources = request.form.getlist('linked_resources')
  62. remove_linked_resources = request.form.getlist('remove_linked_resources')
  63. edit_relationships(book_id, linked_resources, remove_linked_resources, existing_relationships)
  64. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  65. return render_template('edit.html', resource=book, resource_dropdown=resource_dropdown, relationships=existing_relationships)
  66. # route for function to delete a single book from the edit page
  67. @book.route('/books/<int:book_id>/delete', methods=('POST',))
  68. @login_required
  69. def delete_book(book_id):
  70. delete_resource(book_id)
  71. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))