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.

67 lines
2.4KB

  1. # @name: typology.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: typology route for typology-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 werkzeug.exceptions import abort
  12. from . import db
  13. typology = Blueprint('typology', __name__)
  14. # function to retrieve data about a single typology from the database
  15. def get_typology(typology_id):
  16. typology = Typology.query.filter_by(id=typology_id).first()
  17. if typology is None:
  18. abort(404)
  19. return typology
  20. # route for displaying all typologies in database
  21. @typology.route('/typologies')
  22. def get_typologies():
  23. typologies = Typology.query
  24. return render_template('typologies.html', typologies=typologies)
  25. # route for displaying a single typology based on the ID in the database
  26. @typology.route('/typologies/<int:typology_id>')
  27. def show_typology(typology_id):
  28. typology = get_typology(typology_id)
  29. return render_template('typology.html', typology=typology)
  30. # route for editing a single typology based on the ID in the database
  31. @typology.route('/typologies/<int:typology_id>/edit', methods=('GET', 'POST'))
  32. @login_required
  33. def edit_typology(typology_id):
  34. typology = get_typology(typology_id)
  35. if request.method == 'POST':
  36. name = request.form['name']
  37. description = request.form['description']
  38. if not name:
  39. flash('Name is required!')
  40. else:
  41. typology = Typology.query.get(typology_id)
  42. typology.name = name
  43. typology.description = description
  44. db.session.commit()
  45. return redirect(url_for('typology.get_typologies'))
  46. return render_template('edit.html', typology=typology)
  47. # route for function to delete a single typology from the edit page
  48. @typology.route('/typologies/<int:typology_id>/delete', methods=('POST',))
  49. @login_required
  50. def delete_typology(typology_id):
  51. typology = get_typology(typology_id)
  52. deletion = Typology.query.get(typology_id)
  53. db.session.delete(deletion)
  54. db.session.commit()
  55. flash('Successfully deleted!')
  56. return redirect(url_for('typology.get_typologies'))