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

74 行
3.2KB

  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 werkzeug.exceptions import abort
  14. from . import db
  15. import os
  16. practice = Blueprint('practice', __name__)
  17. # route for displaying all practices in database
  18. @practice.route('/practices')
  19. def get_practices():
  20. practices = Resource.query.filter_by(type='practice')
  21. return render_template('resources.html', resources=practices, type='practice')
  22. # route for displaying a single practice based on the ID in the database
  23. @practice.route('/practices/<int:practice_id>')
  24. def show_practice(practice_id):
  25. practice = get_resource(practice_id)
  26. links = get_relationships(practice_id)
  27. return render_template('resource.html', resource=practice, links=links)
  28. # route for editing a single practice based on the ID in the database
  29. @practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))
  30. @login_required
  31. def edit_practice(practice_id):
  32. practice = get_resource(practice_id)
  33. resource_dropdown = Resource.query
  34. existing_relationships = get_relationships(practice_id)
  35. if request.method == 'POST':
  36. name = request.form['name']
  37. description = request.form['description']
  38. linked_resources = request.form.getlist('linked_resources')
  39. remove_linked_resources = request.form.getlist('remove_linked_resources')
  40. if not name:
  41. flash('Name is required!')
  42. else:
  43. practice = Resource.query.get(practice_id)
  44. practice.name = name
  45. practice.description = description
  46. db.session.commit()
  47. if linked_resources:
  48. for linked_resource in linked_resources:
  49. link = Resource.query.get(linked_resource)
  50. if existing_relationships and link not in existing_relationships:
  51. add_relationship(practice_id, linked_resource)
  52. elif not existing_relationships:
  53. add_relationship(practice_id, linked_resource)
  54. if remove_linked_resources:
  55. for remove_linked_resource in remove_linked_resources:
  56. delete_relationship(practice_id, remove_linked_resource)
  57. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  58. return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, links=existing_relationships)
  59. # route for function to delete a single practice from the edit page
  60. @practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
  61. @login_required
  62. def delete_practice(practice_id):
  63. delete_resource(practice_id)
  64. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))