您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

105 行
5.1KB

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