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.3KB

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