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.

68 lines
2.6KB

  1. # @name: sensitivity.py
  2. # @version: 0.1
  3. # @creation_date: 2022-02-08
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  6. # @purpose: sensitivity route for sensitivity-related functions and pages
  7. # @acknowledgements:
  8. # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
  9. from flask import Blueprint, render_template, request, flash, redirect, url_for
  10. from flask_login import login_required, current_user
  11. from .models import Resource
  12. from werkzeug.exceptions import abort
  13. from . import db
  14. sensitivity = Blueprint('sensitivity', __name__)
  15. # function to retrieve data about a single sensitivity from the database
  16. def get_sensitivity(sensitivity_id):
  17. sensitivity = Sensitivity.query.filter_by(id=sensitivity_id).first()
  18. if sensitivity is None:
  19. abort(404)
  20. return sensitivity
  21. # route for displaying all sensitivities in database
  22. @sensitivity.route('/sensitivities')
  23. def get_sensitivities():
  24. sensitivities = Sensitivity.query
  25. return render_template('sensitivities.html', sensitivities=sensitivities)
  26. # route for displaying a single sensitivity based on the ID in the database
  27. @sensitivity.route('/sensitivities/<int:sensitivity_id>')
  28. def show_sensitivity(sensitivity_id):
  29. sensitivity = get_sensitivity(sensitivity_id)
  30. return render_template('sensitivity.html', sensitivity=sensitivity)
  31. # route for editing a single sensitivity based on the ID in the database
  32. @sensitivity.route('/sensitivities/<int:sensitivity_id>/edit', methods=('GET', 'POST'))
  33. @login_required
  34. def edit_sensitivity(sensitivity_id):
  35. sensitivity = get_sensitivity(sensitivity_id)
  36. if request.method == 'POST':
  37. name = request.form['name']
  38. description = request.form['description']
  39. if not name:
  40. flash('Name is required!')
  41. else:
  42. sensitivity = Sensitivity.query.get(sensitivity_id)
  43. sensitivity.name = name
  44. sensitivity.description = description
  45. db.session.commit()
  46. return redirect(url_for('sensitivity.get_sensitivities'))
  47. return render_template('edit.html', sensitivity=sensitivity)
  48. # route for function to delete a single sensitivity from the edit page
  49. @sensitivity.route('/sensitivities/<int:sensitivity_id>/delete', methods=('POST',))
  50. @login_required
  51. def delete_sensitivity(sensitivity_id):
  52. sensitivity = get_sensitivity(sensitivity_id)
  53. deletion = Sensitivity.query.get(sensitivity_id)
  54. db.session.delete(deletion)
  55. db.session.commit()
  56. flash('Successfully deleted!')
  57. return redirect(url_for('sensitivity.get_sensitivities'))