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.

73 rindas
3.1KB

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