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

practice.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. relationships = get_relationships(practice_id)
  27. practice.references = replace_urls(practice.references)
  28. return render_template('resource.html', resource=practice, relationships=relationships)
  29. # route for editing a single practice based on the ID in the database
  30. @practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))
  31. @login_required
  32. def edit_practice(practice_id):
  33. practice = get_resource(practice_id)
  34. resource_dropdown = Resource.query
  35. existing_relationships = get_relationships(practice_id)
  36. if request.method == 'POST':
  37. if not request.form['name']:
  38. flash('Name is required!')
  39. else:
  40. practice = Resource.query.get(practice_id)
  41. practice.name = request.form['name']
  42. practice.description = request.form['description']
  43. practice.experimental = request.form['experimental']
  44. practice.lessonsLearned = request.form['lessonsLearned']
  45. practice.references = request.form['references']
  46. db.session.commit()
  47. linked_resources = request.form.getlist('linked_resources')
  48. remove_linked_resources = request.form.getlist('remove_linked_resources')
  49. edit_relationships(practice_id, linked_resources, remove_linked_resources, existing_relationships)
  50. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  51. return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, relationships=existing_relationships)
  52. # route for function to delete a single practice from the edit page
  53. @practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
  54. @login_required
  55. def delete_practice(practice_id):
  56. delete_resource(practice_id)
  57. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))