Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

95 lines
4.1KB

  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('resource_type') == 'tool':
  23. name = request.form.get('name')
  24. description = request.form.get('description')
  25. project_url = request.form.get('project_url')
  26. repository_url = request.form.get('repository_url')
  27. platform_status = request.form.get('platform_status')
  28. expertise = request.form.get('expertise')
  29. self_host_expertise = request.form.get('self_host_expertise')
  30. ingest = request.form.get('ingest')
  31. output = request.form.get('output')
  32. saas = request.form.get('saas')
  33. dependencies = request.form.get('dependencies')
  34. if not name:
  35. flash('Name is required!')
  36. else:
  37. tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  38. if tool: # if a tool is found, we want to redirect back to create page
  39. flash('Tool with same name already exists')
  40. return redirect(url_for('create.create'))
  41. # create a new tool with the form data
  42. new_tool = Tool(name=name, description=description, project_url=project_url, repository_url=repository_url, platform_status=platform_status, expertise=expertise, self_host_expertise=self_host_expertise, ingest=ingest, output=output, saas=saas, dependencies=dependencies)
  43. # add the new tool to the database
  44. db.session.add(new_tool)
  45. db.session.commit()
  46. elif request.form.get('resource_type') == 'example':
  47. name = request.form.get('name')
  48. description = request.form.get('description')
  49. if not name:
  50. flash('Name is required!')
  51. else:
  52. example = Example.query.filter_by(name=name).first() # if this returns an example, then the name already exists in database
  53. if example: # if an example is found, we want to redirect back to create page
  54. flash('Example with same name already exists')
  55. return redirect(url_for('create.create'))
  56. # create a new example with the form data
  57. new_example = Example(name=name, description=description)
  58. # add the new example to the database
  59. db.session.add(new_example)
  60. db.session.commit()
  61. elif request.form.get('resource_type') == 'practice':
  62. name = request.form.get('name')
  63. description = request.form.get('description')
  64. if not name:
  65. flash('Name is required!')
  66. else:
  67. practice = Practice.query.filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  68. if practice: # if a practice is found, we want to redirect back to create page
  69. flash('Practice with same name already exists')
  70. return redirect(url_for('create.create'))
  71. # create a new practice with the form data
  72. new_practice = Practice(name=name, description=description)
  73. # add the new practice to the database
  74. db.session.add(new_practice)
  75. db.session.commit()
  76. return render_template('create.html')