You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sensitivity.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # @name: sensitivity.py
  2. # @creation_date: 2022-02-08
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: sensitivity route for sensitivity-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. sensitivity = Blueprint('sensitivity', __name__)
  15. # route for displaying all sensitivities in database
  16. @sensitivity.route('/sensitivities')
  17. def get_sensitivities():
  18. sensitivities = Resource.query.filter_by(type='sensitivity')
  19. return render_template('resources.html', resources=sensitivities, type='sensitivity')
  20. # route for displaying a single sensitivity based on the ID in the database
  21. @sensitivity.route('/sensitivities/<int:sensitivity_id>')
  22. def show_sensitivity(sensitivity_id):
  23. sensitivity = get_resource(sensitivity_id)
  24. links = get_linked_resources(sensitivity_id)
  25. return render_template('resource.html', resource=sensitivity, links=links)
  26. # route for editing a single sensitivity based on the ID in the database
  27. @sensitivity.route('/sensitivities/<int:sensitivity_id>/edit', methods=('GET', 'POST'))
  28. @login_required
  29. def edit_sensitivity(sensitivity_id):
  30. sensitivity = get_resource(sensitivity_id)
  31. if request.method == 'POST':
  32. name = request.form['name']
  33. description = request.form['description']
  34. if not name:
  35. flash('Name is required!')
  36. else:
  37. sensitivity = Resource.query.get(sensitivity_id)
  38. sensitivity.name = name
  39. sensitivity.description = description
  40. db.session.commit()
  41. return redirect(url_for('sensitivity.get_sensitivities'))
  42. return render_template('edit.html', sensitivity=sensitivity)
  43. # route for function to delete a single sensitivity from the edit page
  44. @sensitivity.route('/sensitivities/<int:sensitivity_id>/delete', methods=('POST',))
  45. @login_required
  46. def delete_sensitivity(sensitivity_id):
  47. delete_resource(sensitivity_id)
  48. return redirect(url_for('sensitivity.get_sensitivities'))