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.

98 lines
4.1KB

  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 .relationships import *
  13. from werkzeug.exceptions import abort
  14. from . import db
  15. import os
  16. tool = Blueprint('tool', __name__)
  17. # route for displaying all tools in database
  18. @tool.route('/tools')
  19. def get_tools():
  20. tools = Resource.query.filter_by(type='tool')
  21. return render_template('resources.html', resources=tools, type='tool')
  22. # route for displaying a single tool based on the ID in the database
  23. @tool.route('/tools/<int:tool_id>')
  24. def show_tool(tool_id):
  25. tool = get_resource(tool_id)
  26. links = get_relationships(tool_id)
  27. return render_template('resource.html', resource=tool, links=links)
  28. # route for editing a single tool based on the ID in the database
  29. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  30. @login_required
  31. def edit_tool(tool_id):
  32. tool = get_resource(tool_id)
  33. resource_dropdown = Resource.query
  34. existing_relationships = get_relationships(tool_id)
  35. if request.method == 'POST':
  36. name = request.form['name']
  37. description = request.form['description']
  38. developer = request.form['developer']
  39. developerUrl = request.form['developerUrl']
  40. projectUrl = request.form['projectUrl']
  41. repositoryUrl = request.form['repositoryUrl']
  42. license = request.form['license']
  43. scriptingLanguage = request.form['scriptingLanguage']
  44. expertiseToUse = request.form['expertiseToUse']
  45. expertiseToHost = request.form['expertiseToHost']
  46. dependencies = request.form['dependencies']
  47. ingestFormats = request.form['ingestFormats']
  48. outputFormats = request.form['outputFormats']
  49. status = request.form['status']
  50. linked_resources = request.form.getlist('linked_resources')
  51. remove_linked_resources = request.form.getlist('remove_linked_resources')
  52. if not name:
  53. flash('Name is required!')
  54. else:
  55. tool = Resource.query.get(tool_id)
  56. tool.name = name
  57. tool.description = description
  58. tool.developer = developer
  59. tool.developerUrl = developerUrl
  60. tool.projectUrl = projectUrl
  61. tool.repositoryUrl = repositoryUrl
  62. tool.license = license
  63. tool.scriptingLanguage = scriptingLanguage
  64. tool.dependencies = dependencies
  65. tool.expertiseToUse = expertiseToUse
  66. tool.expertiseToHost = expertiseToHost
  67. tool.ingestFormats = ingestFormats
  68. tool.outputFormats = outputFormats
  69. tool.status = status
  70. db.session.commit()
  71. if linked_resources:
  72. for linked_resource in linked_resources:
  73. link = Resource.query.get(linked_resource)
  74. if existing_relationships and link not in existing_relationships:
  75. add_relationship(tool_id, linked_resource)
  76. elif not existing_relationships:
  77. add_relationship(tool_id, linked_resource)
  78. if remove_linked_resources:
  79. for remove_linked_resource in remove_linked_resources:
  80. delete_relationship(tool_id, remove_linked_resource)
  81. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  82. return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=existing_relationships)
  83. # route for function to delete a single tool from the edit page
  84. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  85. @login_required
  86. def delete_tool(tool_id):
  87. delete_resource(tool_id)
  88. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))