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.

90 lines
3.8KB

  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. projectUrl = request.form['projectUrl']
  39. repositoryUrl = request.form['repositoryUrl']
  40. expertiseToUse = request.form['expertiseToUse']
  41. expertiseToHost = request.form['expertiseToHost']
  42. dependencies = request.form['dependencies']
  43. ingestFormats = request.form['ingestFormats']
  44. outputFormats = request.form['outputFormats']
  45. status = request.form['status']
  46. linked_resources = request.form.getlist('linked_resources')
  47. remove_linked_resources = request.form.getlist('remove_linked_resources')
  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 = projectUrl
  55. tool.repositoryUrl = repositoryUrl
  56. tool.dependencies = dependencies
  57. tool.expertiseToUse = expertiseToUse
  58. tool.expertiseToHost = expertiseToHost
  59. tool.ingestFormats = ingestFormats
  60. tool.outputFormats = outputFormats
  61. tool.status = status
  62. db.session.commit()
  63. if linked_resources:
  64. for linked_resource in linked_resources:
  65. link = Resource.query.get(linked_resource)
  66. if existing_relationships and link not in existing_relationships:
  67. add_relationship(tool_id, linked_resource)
  68. elif not existing_relationships:
  69. add_relationship(tool_id, linked_resource)
  70. if remove_linked_resources:
  71. for remove_linked_resource in remove_linked_resources:
  72. delete_relationship(tool_id, remove_linked_resource)
  73. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  74. return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=existing_relationships)
  75. # route for function to delete a single tool from the edit page
  76. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  77. @login_required
  78. def delete_tool(tool_id):
  79. delete_resource(tool_id)
  80. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))