No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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