Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

89 lines
3.7KB

  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 werkzeug.exceptions import abort
  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. tools = Resource.query.filter_by(type='tool')
  20. return render_template('resources.html', resources=tools, type='tool')
  21. # route for displaying a single tool based on the ID in the database
  22. @tool.route('/tools/<int:tool_id>/')
  23. def show_tool(tool_id):
  24. tool = get_resource(tool_id)
  25. links = get_linked_resources(tool_id)
  26. return render_template('resource.html', resource=tool, links=links)
  27. # route for editing a single tool based on the ID in the database
  28. @tool.route('/tools/<int:tool_id>/edit/', methods=('GET', 'POST'))
  29. @login_required
  30. def edit_tool(tool_id):
  31. tool = get_resource(tool_id)
  32. resource_dropdown = Resource.query
  33. links = get_linked_resources(tool_id)
  34. if request.method == 'POST':
  35. name = request.form['name']
  36. description = request.form['description']
  37. projectUrl = request.form['projectUrl']
  38. repositoryUrl = request.form['repositoryUrl']
  39. expertiseToUse = request.form['expertiseToUse']
  40. expertiseToHost = request.form['expertiseToHost']
  41. dependencies = request.form['dependencies']
  42. ingestFormats = request.form['ingestFormats']
  43. outputFormats = request.form['outputFormats']
  44. status = request.form['status']
  45. linked_resources = request.form.getlist('linked_resources')
  46. remove_linked_resources = request.form.getlist('remove_linked_resources')
  47. if not name:
  48. flash('Name is required!')
  49. else:
  50. tool = Resource.query.get(tool_id)
  51. tool.name = name
  52. tool.description = description
  53. tool.projectUrl = projectUrl
  54. tool.repositoryUrl = repositoryUrl
  55. tool.dependencies = dependencies
  56. tool.expertiseToUse = expertiseToUse
  57. tool.expertiseToHost = expertiseToHost
  58. tool.ingestFormats = ingestFormats
  59. tool.outputFormats = outputFormats
  60. tool.status = status
  61. db.session.commit()
  62. if linked_resources:
  63. for linked_resource in linked_resources:
  64. link = Resource.query.get(linked_resource)
  65. if links and link not in links:
  66. add_linked_resource(tool_id, linked_resource)
  67. elif not links:
  68. add_linked_resource(tool_id, linked_resource)
  69. if remove_linked_resources:
  70. for remove_linked_resource in remove_linked_resources:
  71. delete_relationship(tool_id, remove_linked_resource)
  72. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  73. return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=links)
  74. # route for function to delete a single tool from the edit page
  75. @tool.route('/tools/<int:tool_id>/delete/', methods=('POST',))
  76. @login_required
  77. def delete_tool(tool_id):
  78. delete_resource(tool_id)
  79. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))