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.

81 rinda
3.5KB

  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. practice = Blueprint('practice', __name__)
  17. # route for displaying all practices in database
  18. @practice.route('/practices')
  19. def get_practices():
  20. view = request.args.get('view')
  21. practices = Resource.query.filter_by(type='practice').all()
  22. # get number of practices
  23. count = len(practices)
  24. if view != 'list':
  25. # append relationships to each practice
  26. append_relationships_multiple(practices)
  27. else:
  28. # reorder practices by practice name
  29. practices = sorted(practices, key=lambda d: d.__dict__['name'])
  30. return render_template('resources.html', resources=practices, type='practice', count=count, view=view)
  31. # route for displaying a single practice based on the ID in the database
  32. @practice.route('/practices/<int:practice_id>')
  33. def show_practice(practice_id):
  34. practice = get_full_resource(practice_id)
  35. # render Markdown as HTML
  36. practice.longDescription = markdown.markdown(practice.longDescription)
  37. practice.experimental = markdown.markdown(practice.experimental)
  38. practice.considerations = markdown.markdown(practice.considerations)
  39. practice.references = markdown.markdown(practice.references)
  40. return render_template('resource.html', resource=practice)
  41. # route for editing a single practice based on the ID in the database
  42. @practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))
  43. @login_required
  44. def edit_practice(practice_id):
  45. practice = get_resource(practice_id)
  46. resource_dropdown = Resource.query
  47. existing_relationships = get_relationships(practice_id)
  48. if request.method == 'POST':
  49. if not request.form['name']:
  50. flash('Name is required!')
  51. else:
  52. practice = Resource.query.get(practice_id)
  53. practice.name = request.form['name']
  54. practice.description = request.form['description']
  55. practice.longDescription = request.form['longDescription']
  56. practice.experimental = request.form['experimental']
  57. practice.considerations = request.form['considerations']
  58. practice.references = request.form['references']
  59. db.session.commit()
  60. linked_resources = request.form.getlist('linked_resources')
  61. remove_linked_resources = request.form.getlist('remove_linked_resources')
  62. edit_relationships(practice_id, linked_resources, remove_linked_resources, existing_relationships)
  63. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  64. return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, relationships=existing_relationships)
  65. # route for function to delete a single practice from the edit page
  66. @practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
  67. @login_required
  68. def delete_practice(practice_id):
  69. delete_resource(practice_id)
  70. return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))