選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

191 行
8.8KB

  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 werkzeug.exceptions import abort
  13. from . import db
  14. create = Blueprint('create', __name__)
  15. # route for creating a new resource
  16. @create.route('/create', methods=('GET', 'POST'))
  17. @login_required
  18. def create_resource():
  19. if request.method == 'POST':
  20. if request.form.get('resource_type') == 'tool':
  21. type = 'tool'
  22. name = request.form.get('tool_name')
  23. description = request.form.get('description')
  24. projectUrl = request.form.get('projectUrl')
  25. repositoryUrl = request.form.get('repositoryUrl')
  26. expertiseToUse = request.form.get('expertiseToUse')
  27. expertiseToHost = request.form.get('expertiseToHost')
  28. dependencies = request.form.get('dependencies')
  29. ingestFormats = request.form.get('ingestFormats')
  30. outputFormats = request.form.get('outputFormats')
  31. status = request.form.get('status')
  32. if not name:
  33. flash('Name is required!')
  34. else:
  35. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  36. if tool: # if a tool is found, we want to redirect back to create page
  37. flash('Tool with same name already exists')
  38. return redirect(url_for('create.create_resource'))
  39. # create a new tool with the form data
  40. 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)
  41. # add the new tool to the database
  42. db.session.add(new_tool)
  43. db.session.commit()
  44. if request.form.getlist('linked_resources'):
  45. for linked_resource in request.form.getlist('linked_resources'):
  46. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first()
  47. first_resource_id = tool.id
  48. second_resource_id = linked_resource
  49. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  50. # add the new relationship to the database
  51. db.session.add(new_relationship)
  52. db.session.commit()
  53. elif request.form.get('resource_type') == 'practice':
  54. type = 'practice'
  55. name = request.form.get('practice_name')
  56. description = request.form.get('description')
  57. if not name:
  58. flash('Name is required!')
  59. else:
  60. practice = Resource.query.filter_by(type='practice').filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  61. if practice: # if a practice is found, we want to redirect back to create page
  62. flash('Practice with same name already exists')
  63. return redirect(url_for('create.create_resource'))
  64. # create a new practice with the form data
  65. new_practice = Resource(type=type, name=name, description=description)
  66. # add the new practice to the database
  67. db.session.add(new_practice)
  68. db.session.commit()
  69. elif request.form.get('resource_type') == 'sensitivity':
  70. type = 'sensitivity'
  71. name = request.form.get('sensitivity_name')
  72. description = request.form.get('description')
  73. if not name:
  74. flash('Name is required!')
  75. else:
  76. sensitivity = Sensitivity.query.filter_by(name=name).first() # if this returns a sensitivity, then the name already exists in database
  77. if sensitivity: # if a sensitivity is found, we want to redirect back to create page
  78. flash('Sensitivity with same name already exists')
  79. return redirect(url_for('create.create_resource'))
  80. # create a new sensitivity with the form data
  81. new_sensitivity = Resource(type=type, name=name, description=description)
  82. # add the new sensitivity to the database
  83. db.session.add(new_sensitivity)
  84. db.session.commit()
  85. elif request.form.get('resource_type') == 'typology':
  86. type = 'typology'
  87. name = request.form.get('typology_name')
  88. description = request.form.get('description')
  89. if not name:
  90. flash('Name is required!')
  91. else:
  92. typology = Typology.query.filter_by(name=name).first() # if this returns a typology, then the name already exists in database
  93. if typology: # if a typology is found, we want to redirect back to create page
  94. flash('Typology with same name already exists')
  95. return redirect(url_for('create.create_resource'))
  96. # create a new typology with the form data
  97. new_typology = Resource(type=type, name=name, description=description)
  98. # add the new typology to the database
  99. db.session.add(new_typology)
  100. db.session.commit()
  101. elif request.form.get('resource_type') == 'publisher':
  102. type = 'publisher'
  103. name = request.form.get('publisher_name')
  104. description = request.form.get('description')
  105. publisherUrl = request.form.get('publisherUrl')
  106. if not name:
  107. flash('Name is required!')
  108. else:
  109. publisher = Publisher.query.filter_by(name=name).first() # if this returns a publisher, then the name already exists in database
  110. if publisher: # if a publisher is found, we want to redirect back to create page
  111. flash('Publisher with same name already exists')
  112. return redirect(url_for('create.create_resource'))
  113. # create a new publisher with the form data
  114. new_publisher = Resource(type=type, name=name, description=description, publisherUrl=publisherUrl)
  115. # add the new publisher to the database
  116. db.session.add(new_publisher)
  117. db.session.commit()
  118. elif request.form.get('resource_type') == 'book':
  119. type = 'book'
  120. name = request.form.get('book_name')
  121. description = request.form.get('description')
  122. if not name:
  123. flash('Name is required!')
  124. else:
  125. book = Book.query.filter_by(name=name).first() # if this returns a book, then the name already exists in database
  126. if book: # if a book is found, we want to redirect back to create page
  127. flash('Book with same name already exists')
  128. return redirect(url_for('create.create_resource'))
  129. # create a new book with the form data
  130. new_book = Resource(type=type, name=name, description=description)
  131. # add the new book to the database
  132. db.session.add(new_book)
  133. db.session.commit()
  134. elif request.form.get('resource_type') == 'reference':
  135. type = 'reference'
  136. zoteroUrl = request.form.get('zoteroUrl')
  137. if not zoteroUrl:
  138. flash('Zotero URL is required!')
  139. else:
  140. reference = Reference.query.filter_by(zoteroUrl=zoteroUrl).first() # if this returns a reference, then the name already exists in database
  141. if reference: # if a reference is found, we want to redirect back to create page
  142. flash('Reference with same URL already exists')
  143. return redirect(url_for('create.create_resource'))
  144. # create a new reference with the form data
  145. new_reference = Resource(type=type, zoteroUrl=zoteroUrl)
  146. # add the new reference to the database
  147. db.session.add(new_reference)
  148. db.session.commit()
  149. resource_dropdown = Resource.query
  150. return render_template('create.html', resource_dropdown=resource_dropdown)