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.

174 lines
7.6KB

  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 Book
  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. projectUrl = request.form.get('projectUrl')
  26. repositoryUrl = request.form.get('repositoryUrl')
  27. expertiseToUse = request.form.get('expertiseToUse')
  28. expertiseToHost = request.form.get('expertiseToHost')
  29. dependencies = request.form.get('dependencies')
  30. ingestFormats = request.form.get('ingestFormats')
  31. outputFormats = request.form.get('outputFormats')
  32. status = request.form.get('status')
  33. if not name:
  34. flash('Name is required!')
  35. else:
  36. tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  37. if tool: # if a tool is found, we want to redirect back to create page
  38. flash('Tool with same name already exists')
  39. return redirect(url_for('create.create'))
  40. # create a new tool with the form data
  41. new_tool = Tool(name=name, description=description, projectUrl=projectUrl, repositoryUrl=repositoryUrl, expertiseToUse=expertiseToUse, expertiseToHost=expertiseToHost, dependencies=dependencies, ingestFormats=ingestFormats, outputFormats=outputFormats, status=status)
  42. # add the new tool to the database
  43. db.session.add(new_tool)
  44. db.session.commit()
  45. elif request.form.get('resource_type') == 'practice':
  46. name = request.form.get('name')
  47. description = request.form.get('description')
  48. if not name:
  49. flash('Name is required!')
  50. else:
  51. practice = Practice.query.filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  52. if practice: # if a practice is found, we want to redirect back to create page
  53. flash('Practice with same name already exists')
  54. return redirect(url_for('create.create'))
  55. # create a new practice with the form data
  56. new_practice = Practice(name=name, description=description)
  57. # add the new practice to the database
  58. db.session.add(new_practice)
  59. db.session.commit()
  60. elif request.form.get('resource_type') == 'sensitivity':
  61. name = request.form.get('name')
  62. description = request.form.get('description')
  63. if not name:
  64. flash('Name is required!')
  65. else:
  66. sensitivity = Sensitivty.query.filter_by(name=name).first() # if this returns a sensitivity, then the name already exists in database
  67. if sensitivity: # if a sensitivity is found, we want to redirect back to create page
  68. flash('Sensitivity with same name already exists')
  69. return redirect(url_for('create.create'))
  70. # create a new sensitivity with the form data
  71. new_sensitivity = Sensitivity(name=name, description=description)
  72. # add the new sensitivity to the database
  73. db.session.add(new_sensitivity)
  74. db.session.commit()
  75. elif request.form.get('resource_type') == 'typology':
  76. name = request.form.get('name')
  77. description = request.form.get('description')
  78. if not name:
  79. flash('Name is required!')
  80. else:
  81. typology = Typology.query.filter_by(name=name).first() # if this returns a typology, then the name already exists in database
  82. if typology: # if a typology is found, we want to redirect back to create page
  83. flash('Typology with same name already exists')
  84. return redirect(url_for('create.create'))
  85. # create a new typology with the form data
  86. new_typology = Typology(name=name, description=description)
  87. # add the new typology to the database
  88. db.session.add(new_typology)
  89. db.session.commit()
  90. elif request.form.get('resource_type') == 'publisher':
  91. name = request.form.get('name')
  92. description = request.form.get('description')
  93. publisherUrl = request.form.get('publisherUrl')
  94. if not name:
  95. flash('Name is required!')
  96. else:
  97. publisher = Publisher.query.filter_by(name=name).first() # if this returns a publisher, then the name already exists in database
  98. if publisher: # if a publisher is found, we want to redirect back to create page
  99. flash('Publisher with same name already exists')
  100. return redirect(url_for('create.create'))
  101. # create a new publisher with the form data
  102. new_publisher = Publisher(name=name, description=description, publisherUrl=publisherUrl)
  103. # add the new publisher to the database
  104. db.session.add(new_publisher)
  105. db.session.commit()
  106. elif request.form.get('resource_type') == 'book':
  107. name = request.form.get('name')
  108. description = request.form.get('description')
  109. if not name:
  110. flash('Name is required!')
  111. else:
  112. book = Book.query.filter_by(name=name).first() # if this returns a book, then the name already exists in database
  113. if book: # if a book is found, we want to redirect back to create page
  114. flash('Book with same name already exists')
  115. return redirect(url_for('create.create'))
  116. # create a new book with the form data
  117. new_book = Book(name=name, description=description)
  118. # add the new book to the database
  119. db.session.add(new_book)
  120. db.session.commit()
  121. elif request.form.get('resource_type') == 'reference':
  122. zoteroUrl = request.form.get('zoteroUrl')
  123. if not zoteroUrl:
  124. flash('Zotero URL is required!')
  125. else:
  126. reference = Reference.query.filter_by(zoteroUrl=zoteroUrl).first() # if this returns a reference, then the name already exists in database
  127. if reference: # if a reference is found, we want to redirect back to create page
  128. flash('Reference with same URL already exists')
  129. return redirect(url_for('create.create'))
  130. # create a new reference with the form data
  131. new_reference = Reference(zoteroUrl=zoteroUrl)
  132. # add the new reference to the database
  133. db.session.add(new_reference)
  134. db.session.commit()
  135. return render_template('create.html')