Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

82 lines
3.6KB

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