Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

102 lines
4.9KB

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