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.

88 lines
3.6KB

  1. # @name: tool.py
  2. # @creation_date: 2021-10-20
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: tool route for tool-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. tool = Blueprint('tool', __name__)
  15. # route for displaying all tools in database
  16. @tool.route('/tools')
  17. def get_tools():
  18. tools = Resource.query.filter_by(type='tool')
  19. return render_template('resources.html', resources=tools, type='tool')
  20. # route for displaying a single tool based on the ID in the database
  21. @tool.route('/tools/<int:tool_id>')
  22. def show_tool(tool_id):
  23. tool = get_resource(tool_id)
  24. links = get_linked_resources(tool_id)
  25. return render_template('resource.html', resource=tool, links=links)
  26. # route for editing a single tool based on the ID in the database
  27. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  28. @login_required
  29. def edit_tool(tool_id):
  30. tool = get_resource(tool_id)
  31. resource_dropdown = Resource.query
  32. links = get_linked_resources(tool_id)
  33. if request.method == 'POST':
  34. name = request.form['name']
  35. description = request.form['description']
  36. projectUrl = request.form['projectUrl']
  37. repositoryUrl = request.form['repositoryUrl']
  38. expertiseToUse = request.form['expertiseToUse']
  39. expertiseToHost = request.form['expertiseToHost']
  40. dependencies = request.form['dependencies']
  41. ingestFormats = request.form['ingestFormats']
  42. outputFormats = request.form['outputFormats']
  43. status = request.form['status']
  44. linked_resources = request.form.getlist('linked_resources')
  45. remove_linked_resources = request.form.getlist('remove_linked_resources')
  46. if not name:
  47. flash('Name is required!')
  48. else:
  49. tool = Resource.query.get(tool_id)
  50. tool.name = name
  51. tool.description = description
  52. tool.projectUrl = projectUrl
  53. tool.repositoryUrl = repositoryUrl
  54. tool.dependencies = dependencies
  55. tool.expertiseToUse = expertiseToUse
  56. tool.expertiseToHost = expertiseToHost
  57. tool.ingestFormats = ingestFormats
  58. tool.outputFormats = outputFormats
  59. tool.status = status
  60. db.session.commit()
  61. if linked_resources:
  62. for linked_resource in linked_resources:
  63. link = Resource.query.get(linked_resource)
  64. if links and link not in links:
  65. add_linked_resource(tool_id, linked_resource)
  66. elif not links:
  67. add_linked_resource(tool_id, linked_resource)
  68. if remove_linked_resources:
  69. for remove_linked_resource in remove_linked_resources:
  70. delete_relationship(tool_id, remove_linked_resource)
  71. return redirect(url_for('tool.get_tools'))
  72. return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=links)
  73. # route for function to delete a single tool from the edit page
  74. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  75. @login_required
  76. def delete_tool(tool_id):
  77. delete_resource(tool_id)
  78. return redirect(url_for('tool.get_tools'))