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.

72 lines
3.0KB

  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. remove_linked_resources = request.form.getlist('remove_linked_resources')
  38. if not name:
  39. flash('Name is required!')
  40. else:
  41. practice = Resource.query.get(practice_id)
  42. practice.name = name
  43. practice.description = description
  44. db.session.commit()
  45. if linked_resources:
  46. for linked_resource in linked_resources:
  47. link = Resource.query.get(linked_resource)
  48. if links and link not in links:
  49. add_linked_resource(practice_id, linked_resource)
  50. elif not links:
  51. add_linked_resource(practice_id, linked_resource)
  52. if remove_linked_resources:
  53. for remove_linked_resource in remove_linked_resources:
  54. delete_relationship(practice_id, remove_linked_resource)
  55. return redirect(url_for('practice.get_practices'))
  56. return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, links=links)
  57. # route for function to delete a single practice from the edit page
  58. @practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
  59. @login_required
  60. def delete_practice(practice_id):
  61. delete_resource(practice_id)
  62. return redirect(url_for('practice.get_practices'))