You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool.py 5.1KB

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