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.

create.py 8.7KB

2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.get('linked_practice_id'):
  45. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first()
  46. first_resource_id = tool.id
  47. second_resource_id = request.form.get('linked_practice_id')
  48. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  49. # add the new relationship to the database
  50. db.session.add(new_relationship)
  51. db.session.commit()
  52. elif request.form.get('resource_type') == 'practice':
  53. type = 'practice'
  54. name = request.form.get('practice_name')
  55. description = request.form.get('description')
  56. if not name:
  57. flash('Name is required!')
  58. else:
  59. practice = Resource.query.filter_by(type='practice').filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  60. if practice: # if a practice is found, we want to redirect back to create page
  61. flash('Practice with same name already exists')
  62. return redirect(url_for('create.create_resource'))
  63. # create a new practice with the form data
  64. new_practice = Resource(type=type, name=name, description=description)
  65. # add the new practice to the database
  66. db.session.add(new_practice)
  67. db.session.commit()
  68. elif request.form.get('resource_type') == 'sensitivity':
  69. type = 'sensitivity'
  70. name = request.form.get('sensitivity_name')
  71. description = request.form.get('description')
  72. if not name:
  73. flash('Name is required!')
  74. else:
  75. sensitivity = Sensitivity.query.filter_by(name=name).first() # if this returns a sensitivity, then the name already exists in database
  76. if sensitivity: # if a sensitivity is found, we want to redirect back to create page
  77. flash('Sensitivity with same name already exists')
  78. return redirect(url_for('create.create_resource'))
  79. # create a new sensitivity with the form data
  80. new_sensitivity = Resource(type=type, name=name, description=description)
  81. # add the new sensitivity to the database
  82. db.session.add(new_sensitivity)
  83. db.session.commit()
  84. elif request.form.get('resource_type') == 'typology':
  85. type = 'typology'
  86. name = request.form.get('typology_name')
  87. description = request.form.get('description')
  88. if not name:
  89. flash('Name is required!')
  90. else:
  91. typology = Typology.query.filter_by(name=name).first() # if this returns a typology, then the name already exists in database
  92. if typology: # if a typology is found, we want to redirect back to create page
  93. flash('Typology with same name already exists')
  94. return redirect(url_for('create.create_resource'))
  95. # create a new typology with the form data
  96. new_typology = Resource(type=type, name=name, description=description)
  97. # add the new typology to the database
  98. db.session.add(new_typology)
  99. db.session.commit()
  100. elif request.form.get('resource_type') == 'publisher':
  101. type = 'publisher'
  102. name = request.form.get('publisher_name')
  103. description = request.form.get('description')
  104. publisherUrl = request.form.get('publisherUrl')
  105. if not name:
  106. flash('Name is required!')
  107. else:
  108. publisher = Publisher.query.filter_by(name=name).first() # if this returns a publisher, then the name already exists in database
  109. if publisher: # if a publisher is found, we want to redirect back to create page
  110. flash('Publisher with same name already exists')
  111. return redirect(url_for('create.create_resource'))
  112. # create a new publisher with the form data
  113. new_publisher = Resource(type=type, name=name, description=description, publisherUrl=publisherUrl)
  114. # add the new publisher to the database
  115. db.session.add(new_publisher)
  116. db.session.commit()
  117. elif request.form.get('resource_type') == 'book':
  118. type = 'book'
  119. name = request.form.get('book_name')
  120. description = request.form.get('description')
  121. if not name:
  122. flash('Name is required!')
  123. else:
  124. book = Book.query.filter_by(name=name).first() # if this returns a book, then the name already exists in database
  125. if book: # if a book is found, we want to redirect back to create page
  126. flash('Book with same name already exists')
  127. return redirect(url_for('create.create_resource'))
  128. # create a new book with the form data
  129. new_book = Resource(type=type, name=name, description=description)
  130. # add the new book to the database
  131. db.session.add(new_book)
  132. db.session.commit()
  133. elif request.form.get('resource_type') == 'reference':
  134. type = 'reference'
  135. zoteroUrl = request.form.get('zoteroUrl')
  136. if not zoteroUrl:
  137. flash('Zotero URL is required!')
  138. else:
  139. reference = Reference.query.filter_by(zoteroUrl=zoteroUrl).first() # if this returns a reference, then the name already exists in database
  140. if reference: # if a reference is found, we want to redirect back to create page
  141. flash('Reference with same URL already exists')
  142. return redirect(url_for('create.create_resource'))
  143. # create a new reference with the form data
  144. new_reference = Resource(type=type, zoteroUrl=zoteroUrl)
  145. # add the new reference to the database
  146. db.session.add(new_reference)
  147. db.session.commit()
  148. practice_dropdown = Resource.query.filter_by(type='practice')
  149. return render_template('create.html', practice_dropdown=practice_dropdown)