Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

89 lines
3.8KB

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