您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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