|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- # @name: tool.py
- # @version: 0.1
- # @creation_date: 2021-10-20
- # @license: The MIT License <https://opensource.org/licenses/MIT>
- # @author: Simon Bowie <ad7588@coventry.ac.uk>
- # @purpose: tool route for tool-related functions and pages
- # @acknowledgements:
- # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
-
- from flask import Blueprint, render_template, request, flash, redirect, url_for
- from flask_login import login_required, current_user
- from .models import Tool
- from werkzeug.exceptions import abort
- from . import db
-
- tool = Blueprint('tool', __name__)
-
- # function to retrieve data about a single tool from the database
- def get_tool(tool_id):
- tool = Tool.query.filter_by(id=tool_id).first()
- if tool is None:
- abort(404)
- return tool
-
- # route for displaying all tools in database
- @tool.route('/tools')
- def get_tools():
- tools = Tool.query
- return render_template('tools.html', tools=tools)
-
- # route for displaying a single tool based on the ID in the database
- @tool.route('/tools/<int:tool_id>')
- def show_tool(tool_id):
- tool = get_tool(tool_id)
- return render_template('tool.html', tool=tool)
-
- # route for editing a single tool based on the ID in the database
- @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
- @login_required
- def edit_tool(tool_id):
- tool = get_tool(tool_id)
-
- if request.method == 'POST':
- name = request.form['name']
- description = request.form['description']
-
- if not name:
- flash('Name is required!')
- else:
- tool = Tool.query.get(tool_id)
- tool.name = name
- tool.description = description
- db.session.commit()
- return redirect(url_for('tool.get_tools'))
-
- return render_template('edit.html', tool=tool)
-
- # route for function to delete a single tool from the edit page
- @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
- @login_required
- def delete_tool(tool_id):
- tool = get_tool(tool_id)
- deletion = Tool.query.get(tool_id)
- db.session.delete(deletion)
- db.session.commit()
- flash('Successfully deleted!')
- return redirect(url_for('tool.get_tools'))
-
- # route for creating a new tool
- @tool.route('/tools/create', methods=('GET', 'POST'))
- @login_required
- def create_tool():
- if request.method == 'POST':
- name = request.form.get('name')
- description = request.form.get('description')
-
- if not name:
- flash('Name is required!')
- else:
- tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database
-
- if tool: # if a tool is found, we want to redirect back to create page
- flash('Tool with same name already exists')
- return redirect(url_for('tool.create'))
-
- # create a new tool with the form data
- new_tool = Tool(name=name, description=description)
-
- # add the new user to the database
- db.session.add(new_tool)
- db.session.commit()
-
- return render_template('create.html')
|