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.

86 lines
3.4KB

  1. # @name: create.py
  2. # @version: 0.1
  3. # @creation_date: 2021-10-25
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  6. # @purpose: create route for creating tools, examples, and practices
  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 .models import Example
  13. from .models import Practice
  14. from werkzeug.exceptions import abort
  15. from . import db
  16. create = Blueprint('create', __name__)
  17. # route for creating a new resource
  18. @create.route('/create', methods=('GET', 'POST'))
  19. @login_required
  20. def create_resource():
  21. if request.method == 'POST':
  22. if request.form.get('type') == 'tool':
  23. name = request.form.get('name')
  24. description = request.form.get('description')
  25. if not name:
  26. flash('Name is required!')
  27. else:
  28. tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  29. if tool: # if a tool is found, we want to redirect back to create page
  30. flash('Tool with same name already exists')
  31. return redirect(url_for('create.create'))
  32. # create a new tool with the form data
  33. new_tool = Tool(name=name, description=description)
  34. # add the new tool to the database
  35. db.session.add(new_tool)
  36. db.session.commit()
  37. elif request.form.get('type') == 'example':
  38. name = request.form.get('name')
  39. description = request.form.get('description')
  40. if not name:
  41. flash('Name is required!')
  42. else:
  43. example = Example.query.filter_by(name=name).first() # if this returns an example, then the name already exists in database
  44. if example: # if an example is found, we want to redirect back to create page
  45. flash('Example with same name already exists')
  46. return redirect(url_for('create.create'))
  47. # create a new example with the form data
  48. new_example = Example(name=name, description=description)
  49. # add the new example to the database
  50. db.session.add(new_example)
  51. db.session.commit()
  52. elif request.form.get('type') == 'practice':
  53. name = request.form.get('name')
  54. description = request.form.get('description')
  55. if not name:
  56. flash('Name is required!')
  57. else:
  58. practice = Practice.query.filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  59. if practice: # if a practice is found, we want to redirect back to create page
  60. flash('Practice with same name already exists')
  61. return redirect(url_for('create.create'))
  62. # create a new practice with the form data
  63. new_practice = Practice(name=name, description=description)
  64. # add the new practice to the database
  65. db.session.add(new_practice)
  66. db.session.commit()
  67. return render_template('create.html')