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.

91 lines
4.2KB

  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. for key in request.args.keys():
  22. if key == 'practice':
  23. query = 'SELECT Resource.* FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id WHERE Relationship.second_resource_id=' + request.args.get(key) + ';'
  24. tools = db.engine.execute(query)
  25. else:
  26. kwargs = {'type': 'tool', key: request.args.get(key)}
  27. tools = Resource.query.filter_by(**kwargs)
  28. # get filters
  29. # practices
  30. practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name)
  31. #FOR LATER: SELECT Resource.name, second.name FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id LEFT JOIN Resource second ON Relationship.second_resource_id=second.id;
  32. # license
  33. licenses_filter = get_filter_values('license')
  34. # language
  35. languages_filter = get_filter_values('scriptingLanguage')
  36. return render_template('resources.html', resources=tools, type='tool', practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter)
  37. # route for displaying a single tool based on the ID in the database
  38. @tool.route('/tools/<int:tool_id>')
  39. def show_tool(tool_id):
  40. tool = get_resource(tool_id)
  41. relationships = get_relationships(tool_id)
  42. return render_template('resource.html', resource=tool, relationships=relationships)
  43. # route for editing a single tool based on the ID in the database
  44. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  45. @login_required
  46. def edit_tool(tool_id):
  47. tool = get_resource(tool_id)
  48. resource_dropdown = Resource.query
  49. existing_relationships = get_relationships(tool_id)
  50. if request.method == 'POST':
  51. if not request.form['name']:
  52. flash('Name is required!')
  53. else:
  54. tool = Resource.query.get(tool_id)
  55. tool.name = request.form['name']
  56. tool.description = request.form['description']
  57. tool.developer = request.form['developer']
  58. tool.developerUrl = request.form['developerUrl']
  59. tool.projectUrl = request.form['projectUrl']
  60. tool.repositoryUrl = request.form['repositoryUrl']
  61. tool.license = request.form['license']
  62. tool.scriptingLanguage = request.form['scriptingLanguage']
  63. tool.expertiseToUse = request.form['expertiseToUse']
  64. tool.expertiseToHost = request.form['expertiseToHost']
  65. tool.dependencies = request.form['dependencies']
  66. tool.ingestFormats = request.form['ingestFormats']
  67. tool.outputFormats = request.form['outputFormats']
  68. tool.status = request.form['status']
  69. db.session.commit()
  70. linked_resources = request.form.getlist('linked_resources')
  71. remove_linked_resources = request.form.getlist('remove_linked_resources')
  72. edit_relationships(tool_id, linked_resources, remove_linked_resources, existing_relationships)
  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')))