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.

86 lines
3.0KB

  1. # @name: tool.py
  2. # @version: 0.1
  3. # @creation_date: 2021-10-20
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  6. # @purpose: tool route for tool-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 .models import Relationships
  13. from werkzeug.exceptions import abort
  14. from . import db
  15. tool = Blueprint('tool', __name__)
  16. # function to retrieve data about a single tool from the database
  17. def get_tool(tool_id):
  18. tool = Resource.query.filter_by(id=tool_id).first()
  19. if tool is None:
  20. abort(404)
  21. return tool
  22. # function to retrieve linked resources
  23. def get_linked_resources(tool_id):
  24. relationships = Relationships.query.filter_by(first_resource_id=tool_id).first()
  25. if relationships:
  26. resource_id = relationships.second_resource_id
  27. resources = Resource.query.filter_by(id=resource_id).all()
  28. return resources
  29. # route for displaying all tools in database
  30. @tool.route('/tools')
  31. def get_tools():
  32. tools = Resource.query.filter_by(type='tool')
  33. return render_template('tools.html', tools=tools)
  34. # route for displaying a single tool based on the ID in the database
  35. @tool.route('/tools/<int:tool_id>')
  36. def show_tool(tool_id):
  37. tool = get_tool(tool_id)
  38. resources = get_linked_resources(tool_id)
  39. return render_template('tool.html', tool=tool, resources=resources)
  40. # route for editing a single tool based on the ID in the database
  41. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  42. @login_required
  43. def edit_tool(tool_id):
  44. tool = get_tool(tool_id)
  45. if request.method == 'POST':
  46. name = request.form['name']
  47. description = request.form['description']
  48. if not name:
  49. flash('Name is required!')
  50. else:
  51. tool = Resource.query.get(tool_id)
  52. tool.name = name
  53. tool.description = description
  54. tool.projectUrl = project_url
  55. tool.repositoryUrl = repository_url
  56. tool.dependencies = dependencies
  57. tool.expertiseToUse = expertise
  58. tool.expertiseToHost = self_host_expertise
  59. tool.ingestFormats = ingest
  60. tool.outputFormats = output
  61. tool.status = status
  62. db.session.commit()
  63. return redirect(url_for('tool.get_tools'))
  64. return render_template('edit.html', resource=tool)
  65. # route for function to delete a single tool from the edit page
  66. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  67. @login_required
  68. def delete_tool(tool_id):
  69. tool = get_tool(tool_id)
  70. deletion = Resource.query.get(tool_id)
  71. db.session.delete(deletion)
  72. db.session.commit()
  73. flash('Successfully deleted!')
  74. return redirect(url_for('tool.get_tools'))