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

67 lines
2.3KB

  1. # @name: tool.py
  2. # @version: 0.1
  3. # @creation_date: 2021-10-20
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  6. # @purpose: tool route for tool-related functions and pages
  7. # @acknowledgements:
  8. # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
  9. from flask import Blueprint, render_template, request, flash, redirect, url_for
  10. from flask_login import login_required, current_user
  11. from .models import Resource
  12. from .resources import *
  13. from werkzeug.exceptions import abort
  14. from . import db
  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('tools.html', tools=tools)
  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. resources = get_linked_resources(tool_id)
  26. return render_template('tool.html', tool=tool, resources=resources)
  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. if request.method == 'POST':
  33. name = request.form['name']
  34. description = request.form['description']
  35. if not name:
  36. flash('Name is required!')
  37. else:
  38. tool = Resource.query.get(tool_id)
  39. tool.name = name
  40. tool.description = description
  41. # tool.projectUrl = project_url
  42. # tool.repositoryUrl = repository_url
  43. # tool.dependencies = dependencies
  44. # tool.expertiseToUse = expertise
  45. # tool.expertiseToHost = self_host_expertise
  46. # tool.ingestFormats = ingest
  47. # tool.outputFormats = output
  48. # tool.status = status
  49. db.session.commit()
  50. return redirect(url_for('tool.get_tools'))
  51. return render_template('edit.html', resource=tool)
  52. # route for function to delete a single tool from the edit page
  53. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  54. @login_required
  55. def delete_tool(tool_id):
  56. delete_resource(tool_id)
  57. return redirect(url_for('tool.get_tools'))