Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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