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ů.

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