Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

58 lines
2.2KB

  1. # @name: workflow.py
  2. # @creation_date: 2022-04-05
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: workflow route for workflow-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. workflow = Blueprint('workflow', __name__)
  15. # route for displaying all workflows in database
  16. @workflow.route('/workflows')
  17. def get_workflows():
  18. workflows = Resource.query.filter_by(type='workflow')
  19. return render_template('resources.html', resources=workflows, type='workflow')
  20. # route for displaying a single workflow based on the ID in the database
  21. @workflow.route('/workflows/<int:workflow_id>')
  22. def show_workflow(workflow_id):
  23. workflow = get_resource(workflow_id)
  24. links = get_linked_resources(workflow_id)
  25. return render_template('resource.html', resource=workflow, links=links)
  26. # route for editing a single workflow based on the ID in the database
  27. @workflow.route('/workflows/<int:workflow_id>/edit', methods=('GET', 'POST'))
  28. @login_required
  29. def edit_workflow(workflow_id):
  30. workflow = get_resource(workflow_id)
  31. if request.method == 'POST':
  32. name = request.form['name']
  33. description = request.form['description']
  34. if not name:
  35. flash('Name is required!')
  36. else:
  37. workflow = Resource.query.get(workflow_id)
  38. workflow.name = name
  39. workflow.description = description
  40. db.session.commit()
  41. return redirect(url_for('workflow.get_workflows'))
  42. return render_template('edit.html', resource=workflow)
  43. # route for function to delete a single workflow from the edit page
  44. @workflow.route('/workflows/<int:workflow_id>/delete', methods=('POST',))
  45. @login_required
  46. def delete_workflow(workflow_id):
  47. delete_resource(workflow_id)
  48. return redirect(url_for('workflow.get_workflows'))