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.

99 linhas
4.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 . import db
  14. from sqlalchemy import text
  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).all()
  22. for key in request.args.keys():
  23. if key == 'practice':
  24. tools = Resource.query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.second_resource_id==request.args.get(key)).all()
  25. also_tools = Resource.query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.first_resource_id==request.args.get(key)).all()
  26. tools = tools + also_tools
  27. elif key == 'scriptingLanguage':
  28. regex = request.args.get(key) + "$|" + request.args.get(key) + "\s\/"
  29. tools = Resource.query.filter_by(type=type).filter(Resource.scriptingLanguage.regexp_match(regex)).all()
  30. else:
  31. kwargs = {'type': type, key: request.args.get(key)}
  32. tools = Resource.query.filter_by(**kwargs).all()
  33. # get number of tools
  34. count = len(tools)
  35. # append relationships to each tool
  36. append_relationships_multiple(tools)
  37. # get filters
  38. # practices
  39. practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name)
  40. #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;
  41. # license
  42. licenses_filter = get_filter_values('license', type)
  43. # language
  44. languages_filter = get_filter_values('scriptingLanguage', type)
  45. return render_template('resources.html', resources=tools, type=type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter, count=count)
  46. # route for displaying a single tool based on the ID in the database
  47. @tool.route('/tools/<int:tool_id>')
  48. def show_tool(tool_id):
  49. tool = get_full_resource(tool_id)
  50. return render_template('resource.html', resource=tool)
  51. # route for editing a single tool based on the ID in the database
  52. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  53. @login_required
  54. def edit_tool(tool_id):
  55. tool = get_resource(tool_id)
  56. resource_dropdown = Resource.query
  57. existing_relationships = get_relationships(tool_id)
  58. if request.method == 'POST':
  59. if not request.form['name']:
  60. flash('Name is required!')
  61. else:
  62. tool = Resource.query.get(tool_id)
  63. tool.name = request.form['name']
  64. tool.description = request.form['description']
  65. tool.developer = request.form['developer']
  66. tool.developerUrl = request.form['developerUrl']
  67. tool.projectUrl = request.form['projectUrl']
  68. tool.repositoryUrl = request.form['repositoryUrl']
  69. tool.license = request.form['license']
  70. tool.scriptingLanguage = request.form['scriptingLanguage']
  71. tool.expertiseToUse = request.form['expertiseToUse']
  72. tool.expertiseToHost = request.form['expertiseToHost']
  73. tool.dependencies = request.form['dependencies']
  74. tool.ingestFormats = request.form['ingestFormats']
  75. tool.outputFormats = request.form['outputFormats']
  76. tool.status = request.form['status']
  77. db.session.commit()
  78. linked_resources = request.form.getlist('linked_resources')
  79. remove_linked_resources = request.form.getlist('remove_linked_resources')
  80. edit_relationships(tool_id, linked_resources, remove_linked_resources, existing_relationships)
  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, relationships=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')))