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 3.1KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Tool
  12. from werkzeug.exceptions import abort
  13. from . import db
  14. tool = Blueprint('tool', __name__)
  15. # function to retrieve data about a single tool from the database
  16. def get_tool(tool_id):
  17. tool = Tool.query.filter_by(id=tool_id).first()
  18. if tool is None:
  19. abort(404)
  20. return tool
  21. # route for displaying all tools in database
  22. @tool.route('/tools')
  23. def get_tools():
  24. tools = Tool.query
  25. return render_template('tools.html', tools=tools)
  26. # route for displaying a single tool based on the ID in the database
  27. @tool.route('/tools/<int:tool_id>')
  28. def show_tool(tool_id):
  29. tool = get_tool(tool_id)
  30. return render_template('tool.html', tool=tool)
  31. # route for editing a single tool based on the ID in the database
  32. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  33. @login_required
  34. def edit_tool(tool_id):
  35. tool = get_tool(tool_id)
  36. if request.method == 'POST':
  37. name = request.form['name']
  38. description = request.form['description']
  39. if not name:
  40. flash('Name is required!')
  41. else:
  42. tool = Tool.query.get(tool_id)
  43. tool.name = name
  44. tool.description = description
  45. db.session.commit()
  46. return redirect(url_for('tool.get_tools'))
  47. return render_template('edit.html', tool=tool)
  48. # route for function to delete a single tool from the edit page
  49. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  50. @login_required
  51. def delete_tool(tool_id):
  52. tool = get_tool(tool_id)
  53. deletion = Tool.query.get(tool_id)
  54. db.session.delete(deletion)
  55. db.session.commit()
  56. flash('Successfully deleted!')
  57. return redirect(url_for('tool.get_tools'))
  58. # route for creating a new tool
  59. @tool.route('/tools/create', methods=('GET', 'POST'))
  60. @login_required
  61. def create_tool():
  62. if request.method == 'POST':
  63. name = request.form.get('name')
  64. description = request.form.get('description')
  65. if not name:
  66. flash('Name is required!')
  67. else:
  68. tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  69. if tool: # if a tool is found, we want to redirect back to create page
  70. flash('Tool with same name already exists')
  71. return redirect(url_for('tool.create'))
  72. # create a new tool with the form data
  73. new_tool = Tool(name=name, description=description)
  74. # add the new user to the database
  75. db.session.add(new_tool)
  76. db.session.commit()
  77. return render_template('create.html')