Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import markdown
  17. book = Blueprint('book', __name__)
  18. # route for displaying all books in database
  19. @book.route('/books')
  20. def get_books():
  21. # GET PARAMETERS
  22. # get URL parameters for views and pages
  23. view = request.args.get('view')
  24. page = request.args.get('page', 1, type=int)
  25. # set resource type
  26. resource_type = 'book'
  27. # get introductory paragraph Markdown
  28. with open('content/books.md', 'r') as f:
  29. intro_text = f.read()
  30. intro_text = markdown.markdown(intro_text)
  31. # DATABASE QUERY
  32. books_query = Resource.query.filter_by(type=resource_type).filter_by(published=True)
  33. # FILTERING
  34. for key in request.args.keys():
  35. if key != 'view' and key != 'page':
  36. if (key == 'practice' and request.args.get(key) != ''):
  37. books_1 = books_query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Relationship.second_resource_id==request.args.get(key))
  38. books_2 = books_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key))
  39. books_query = books_1.union(books_2)
  40. if (key != 'practice' and request.args.get(key) != ''):
  41. kwargs = {key: request.args.get(key)}
  42. books_query = books_query.filter_by(**kwargs)
  43. # finalise the query and add pagination
  44. books = books_query.order_by(Resource.name).paginate(page=page, per_page=25)
  45. # FILTERS MENU
  46. # get values for filter menu dropdowns
  47. # practices
  48. practices_filter = Resource.query.filter_by(type='practice').filter_by(published=True).with_entities(Resource.id, Resource.name).all()
  49. # year
  50. year_filter = get_filter_values('year', resource_type)
  51. # typology
  52. typology_filter = get_filter_values('typology', resource_type)
  53. # POST-FILTERING PROCESSING
  54. # if view is 'expanded' then append relationships
  55. if view != 'list':
  56. # append relationships to each book
  57. append_relationships_multiple_paginated(books)
  58. # render Markdown as HTML
  59. for book in books:
  60. book.description = markdown.markdown(book.description)
  61. return render_template('resources.html', resources=books, type=resource_type, practices_filter=practices_filter, year_filter=year_filter, typology_filter=typology_filter, view=view, page=page, intro_text=intro_text)
  62. # route for displaying a single book based on the ID in the database
  63. @book.route('/books/<int:book_id>')
  64. def show_book(book_id):
  65. book = get_full_resource(book_id)
  66. return render_template('book.html', resource=book)
  67. # route for editing a single book based on the ID in the database
  68. @book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))
  69. @login_required
  70. def edit_book(book_id):
  71. book = get_resource(book_id)
  72. resource_dropdown = Resource.query.order_by(Resource.name)
  73. existing_relationships = get_relationships(book_id)
  74. if request.method == 'POST':
  75. if not request.form['name']:
  76. flash('Name is required!')
  77. else:
  78. book = Resource.query.get(book_id)
  79. book.name = request.form['name']
  80. book.description = request.form['description']
  81. book.author = request.form['author']
  82. book.year = request.form['year']
  83. book.bookUrl = request.form['bookUrl']
  84. book.isbn = request.form['isbn']
  85. book.typology = request.form['typology']
  86. db.session.commit()
  87. linked_resources = request.form.getlist('linked_tools') + request.form.getlist('linked_practices')
  88. remove_linked_resources = request.form.getlist('remove_linked_resources')
  89. edit_relationships(book_id, linked_resources, remove_linked_resources, existing_relationships)
  90. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  91. return render_template('edit.html', resource=book, resource_dropdown=resource_dropdown, relationships=existing_relationships)
  92. # route for function to delete a single book from the edit page
  93. @book.route('/books/<int:book_id>/delete', methods=('POST',))
  94. @login_required
  95. def delete_book(book_id):
  96. delete_resource(book_id)
  97. return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))