選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

96 行
4.6KB

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