Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

tool.py 4.5KB

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