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

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