選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

book.py 4.1KB

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