You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
4.2KB

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