No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

create.py 8.1KB

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