Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

85 linhas
3.8KB

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