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.

125 satır
5.9KB

  1. # @name: create.py
  2. # @creation_date: 2021-10-25
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: create route for creating tools, examples, and practices
  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 .models import Relationship
  12. from .resources import *
  13. from werkzeug.exceptions import abort
  14. from . import db
  15. import os
  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. type = 'tool'
  24. name = request.form.get('tool_name')
  25. description = request.form.get('description')
  26. projectUrl = request.form.get('projectUrl')
  27. repositoryUrl = request.form.get('repositoryUrl')
  28. expertiseToUse = request.form.get('expertiseToUse')
  29. expertiseToHost = request.form.get('expertiseToHost')
  30. dependencies = request.form.get('dependencies')
  31. ingestFormats = request.form.get('ingestFormats')
  32. outputFormats = request.form.get('outputFormats')
  33. status = request.form.get('status')
  34. if not name:
  35. flash('Name is required!')
  36. else:
  37. tool = Resource.query.filter_by(type='tool').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_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  41. # create a new tool with the form data
  42. new_tool = Resource(type=type, name=name, description=description, projectUrl=projectUrl, repositoryUrl=repositoryUrl, expertiseToUse=expertiseToUse, expertiseToHost=expertiseToHost, dependencies=dependencies, ingestFormats=ingestFormats, outputFormats=outputFormats, status=status)
  43. # add the new tool to the database
  44. db.session.add(new_tool)
  45. db.session.commit()
  46. if request.form.getlist('linked_resources'):
  47. for linked_resource in request.form.getlist('linked_resources'):
  48. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first()
  49. add_linked_resource(tool.id, linked_resource)
  50. elif request.form.get('resource_type') == 'practice':
  51. type = 'practice'
  52. name = request.form.get('practice_name')
  53. description = request.form.get('description')
  54. if not name:
  55. flash('Name is required!')
  56. else:
  57. practice = Resource.query.filter_by(type='practice').filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  58. if practice: # if a practice is found, we want to redirect back to create page
  59. flash('Practice with same name already exists')
  60. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  61. # create a new practice with the form data
  62. new_practice = Resource(type=type, name=name, description=description)
  63. # add the new practice to the database
  64. db.session.add(new_practice)
  65. db.session.commit()
  66. elif request.form.get('resource_type') == 'publisher':
  67. type = 'publisher'
  68. name = request.form.get('publisher_name')
  69. description = request.form.get('description')
  70. publisherUrl = request.form.get('publisherUrl')
  71. if not name:
  72. flash('Name is required!')
  73. else:
  74. publisher = Publisher.query.filter_by(name=name).first() # if this returns a publisher, then the name already exists in database
  75. if publisher: # if a publisher is found, we want to redirect back to create page
  76. flash('Publisher with same name already exists')
  77. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  78. # create a new publisher with the form data
  79. new_publisher = Resource(type=type, name=name, description=description, publisherUrl=publisherUrl)
  80. # add the new publisher to the database
  81. db.session.add(new_publisher)
  82. db.session.commit()
  83. elif request.form.get('resource_type') == 'book':
  84. type = 'book'
  85. name = request.form.get('book_name')
  86. description = request.form.get('description')
  87. if not name:
  88. flash('Name is required!')
  89. else:
  90. book = Book.query.filter_by(name=name).first() # if this returns a book, then the name already exists in database
  91. if book: # if a book is found, we want to redirect back to create page
  92. flash('Book with same name already exists')
  93. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  94. # create a new book with the form data
  95. new_book = Resource(type=type, name=name, description=description)
  96. # add the new book to the database
  97. db.session.add(new_book)
  98. db.session.commit()
  99. resource_dropdown = Resource.query
  100. return render_template('create.html', resource_dropdown=resource_dropdown)