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.

130 lines
6.2KB

  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 .relationships import *
  14. from werkzeug.exceptions import abort
  15. from . import db
  16. import os
  17. create = Blueprint('create', __name__)
  18. # route for creating a new resource
  19. @create.route('/create', methods=('GET', 'POST'))
  20. @login_required
  21. def create_resource():
  22. if request.method == 'POST':
  23. if request.form.get('resource_type') == 'tool':
  24. type = 'tool'
  25. name = request.form.get('tool_name')
  26. description = request.form.get('description')
  27. developer = request.form.get('developer')
  28. developerUrl = request.form.get('developerUrl')
  29. projectUrl = request.form.get('projectUrl')
  30. repositoryUrl = request.form.get('repositoryUrl')
  31. license = request.form.get('license')
  32. scriptingLanguage = request.form.get('scriptingLanguage')
  33. expertiseToUse = request.form.get('expertiseToUse')
  34. expertiseToHost = request.form.get('expertiseToHost')
  35. dependencies = request.form.get('dependencies')
  36. ingestFormats = request.form.get('ingestFormats')
  37. outputFormats = request.form.get('outputFormats')
  38. status = request.form.get('status')
  39. if not name:
  40. flash('Name is required!')
  41. else:
  42. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  43. if tool: # if a tool is found, we want to redirect back to create page
  44. flash('Tool with same name already exists')
  45. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  46. # create a new tool with the form data
  47. new_tool = Resource(type=type, name=name, description=description, developer=developer, developerUrl=developerUrl, projectUrl=projectUrl, repositoryUrl=repositoryUrl, license=license, scriptingLanguage=scriptingLanguage, expertiseToUse=expertiseToUse, expertiseToHost=expertiseToHost, dependencies=dependencies, ingestFormats=ingestFormats, outputFormats=outputFormats, status=status)
  48. # add the new tool to the database
  49. db.session.add(new_tool)
  50. db.session.commit()
  51. if request.form.getlist('linked_resources'):
  52. for linked_resource in request.form.getlist('linked_resources'):
  53. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first()
  54. add_relationship(tool.id, linked_resource)
  55. elif request.form.get('resource_type') == 'practice':
  56. type = 'practice'
  57. name = request.form.get('practice_name')
  58. description = request.form.get('description')
  59. if not name:
  60. flash('Name is required!')
  61. else:
  62. practice = Resource.query.filter_by(type='practice').filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  63. if practice: # if a practice is found, we want to redirect back to create page
  64. flash('Practice with same name already exists')
  65. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  66. # create a new practice with the form data
  67. new_practice = Resource(type=type, name=name, description=description)
  68. # add the new practice to the database
  69. db.session.add(new_practice)
  70. db.session.commit()
  71. elif request.form.get('resource_type') == 'publisher':
  72. type = 'publisher'
  73. name = request.form.get('publisher_name')
  74. description = request.form.get('description')
  75. publisherUrl = request.form.get('publisherUrl')
  76. if not name:
  77. flash('Name is required!')
  78. else:
  79. publisher = Publisher.query.filter_by(name=name).first() # if this returns a publisher, then the name already exists in database
  80. if publisher: # if a publisher is found, we want to redirect back to create page
  81. flash('Publisher with same name already exists')
  82. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  83. # create a new publisher with the form data
  84. new_publisher = Resource(type=type, name=name, description=description, publisherUrl=publisherUrl)
  85. # add the new publisher to the database
  86. db.session.add(new_publisher)
  87. db.session.commit()
  88. elif request.form.get('resource_type') == 'book':
  89. type = 'book'
  90. name = request.form.get('book_name')
  91. description = request.form.get('description')
  92. if not name:
  93. flash('Name is required!')
  94. else:
  95. book = Book.query.filter_by(name=name).first() # if this returns a book, then the name already exists in database
  96. if book: # if a book is found, we want to redirect back to create page
  97. flash('Book with same name already exists')
  98. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  99. # create a new book with the form data
  100. new_book = Resource(type=type, name=name, description=description)
  101. # add the new book to the database
  102. db.session.add(new_book)
  103. db.session.commit()
  104. resource_dropdown = Resource.query
  105. return render_template('create.html', resource_dropdown=resource_dropdown)