Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

practice.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # @name: practice.py
  2. # @creation_date: 2021-11-03
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: practice route for practice-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. import markdown
  16. from sqlalchemy.sql import func
  17. from sqlalchemy import or_
  18. practice = Blueprint('practice', __name__)
  19. # route for displaying all practices in database
  20. @practice.route('/practices')
  21. def get_practices():
  22. # GET PARAMETERS
  23. # get URL parameters for views and pages
  24. view = request.args.get('view')
  25. page = request.args.get('page', 1, type=int)
  26. # set resource type
  27. resource_type = 'practice'
  28. # get introductory paragraph Markdown
  29. with open('content/practices.md', 'r') as f:
  30. intro_text = f.read()
  31. intro_text = markdown.markdown(intro_text)
  32. # DATABASE QUERY
  33. practices_query = Resource.query.filter_by(type=resource_type)
  34. # temporarily removing incomplete practices from main list
  35. practices_query = Resource.query.filter(
  36. or_(
  37. Resource.id==53,
  38. Resource.id==56,
  39. Resource.id==57,
  40. Resource.id==59,
  41. Resource.id==61,
  42. Resource.id==62,
  43. Resource.id==63,
  44. Resource.id==64,
  45. Resource.id==65,
  46. Resource.id==66
  47. ))
  48. # finalise the query and add pagination
  49. practices = practices_query.order_by(Resource.name).paginate(page=page, per_page=25)
  50. # POST-FILTERING PROCESSING
  51. # if view is 'expanded' then append relationships
  52. if view != 'list':
  53. # append relationships to each book
  54. append_relationships_multiple_paginated(practices)
  55. return render_template('resources.html', resources=practices, type='practice', view=view, page=page, intro_text=intro_text)
  56. # route for displaying a single practice based on the ID in the database
  57. @practice.route('/practices/<int:practice_id>')
  58. def show_practice(practice_id):
  59. practice = get_full_resource(practice_id)
  60. # render Markdown as HTML
  61. practice.description = markdown.markdown(practice.description)
  62. practice.longDescription = markdown.markdown(practice.longDescription)
  63. practice.experimental = markdown.markdown(practice.experimental)
  64. practice.considerations = markdown.markdown(practice.considerations)
  65. practice.references = markdown.markdown(practice.references)
  66. return render_template('resource.html', resource=practice)
  67. # route for editing a single practice based on the ID in the database
  68. @practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))
  69. @login_required
  70. def edit_practice(practice_id):
  71. practice = get_resource(practice_id)
  72. resource_dropdown = Resource.query.order_by(Resource.name)
  73. existing_relationships = get_relationships(practice_id)
  74. if request.method == 'POST':
  75. if not request.form['name']:
  76. flash('Name is required!')
  77. else:
  78. practice = Resource.query.get(practice_id)
  79. practice.name = request.form['name']
  80. practice.description = request.form['description']
  81. practice.longDescription = request.form['longDescription']
  82. practice.experimental = request.form['experimental']
  83. practice.considerations = request.form['considerations']
  84. practice.references = request.form['references']
  85. db.session.commit()
  86. linked_resources = request.form.getlist('linked_tools') + request.form.getlist('linked_books')
  87. remove_linked_resources = request.form.getlist('remove_linked_resources')
  88. edit_relationships(practice_id, linked_resources, remove_linked_resources, existing_relationships)
  89. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  90. return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, relationships=existing_relationships)
  91. # route for function to delete a single practice from the edit page
  92. @practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
  93. @login_required
  94. def delete_practice(practice_id):
  95. delete_resource(practice_id)
  96. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))