Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

82 lines
3.2KB

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