Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

95 linhas
4.5KB

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