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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 .relationships import *
  14. from werkzeug.exceptions import abort
  15. from . import db
  16. import os
  17. create = Blueprint('create', __name__)
  18. # route for creating a new resource
  19. @create.route('/create', methods=('GET', 'POST'))
  20. @login_required
  21. def create_resource():
  22. if request.method == 'POST':
  23. if request.form.get('resource_type') == 'tool':
  24. type = 'tool'
  25. name = request.form.get('tool_name')
  26. description = request.form.get('description')
  27. projectUrl = request.form.get('projectUrl')
  28. repositoryUrl = request.form.get('repositoryUrl')
  29. expertiseToUse = request.form.get('expertiseToUse')
  30. expertiseToHost = request.form.get('expertiseToHost')
  31. dependencies = request.form.get('dependencies')
  32. ingestFormats = request.form.get('ingestFormats')
  33. outputFormats = request.form.get('outputFormats')
  34. status = request.form.get('status')
  35. if not name:
  36. flash('Name is required!')
  37. else:
  38. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  39. if tool: # if a tool is found, we want to redirect back to create page
  40. flash('Tool with same name already exists')
  41. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  42. # create a new tool with the form data
  43. 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)
  44. # add the new tool to the database
  45. db.session.add(new_tool)
  46. db.session.commit()
  47. if request.form.getlist('linked_resources'):
  48. for linked_resource in request.form.getlist('linked_resources'):
  49. tool = Resource.query.filter_by(type='tool').filter_by(name=name).first()
  50. add_relationship(tool.id, linked_resource)
  51. elif request.form.get('resource_type') == 'practice':
  52. type = 'practice'
  53. name = request.form.get('practice_name')
  54. description = request.form.get('description')
  55. if not name:
  56. flash('Name is required!')
  57. else:
  58. practice = Resource.query.filter_by(type='practice').filter_by(name=name).first() # if this returns a practice, then the name already exists in database
  59. if practice: # if a practice is found, we want to redirect back to create page
  60. flash('Practice with same name already exists')
  61. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  62. # create a new practice with the form data
  63. new_practice = Resource(type=type, name=name, description=description)
  64. # add the new practice to the database
  65. db.session.add(new_practice)
  66. db.session.commit()
  67. elif request.form.get('resource_type') == 'publisher':
  68. type = 'publisher'
  69. name = request.form.get('publisher_name')
  70. description = request.form.get('description')
  71. publisherUrl = request.form.get('publisherUrl')
  72. if not name:
  73. flash('Name is required!')
  74. else:
  75. publisher = Publisher.query.filter_by(name=name).first() # if this returns a publisher, then the name already exists in database
  76. if publisher: # if a publisher is found, we want to redirect back to create page
  77. flash('Publisher with same name already exists')
  78. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  79. # create a new publisher with the form data
  80. new_publisher = Resource(type=type, name=name, description=description, publisherUrl=publisherUrl)
  81. # add the new publisher to the database
  82. db.session.add(new_publisher)
  83. db.session.commit()
  84. elif request.form.get('resource_type') == 'book':
  85. type = 'book'
  86. name = request.form.get('book_name')
  87. description = request.form.get('description')
  88. if not name:
  89. flash('Name is required!')
  90. else:
  91. book = Book.query.filter_by(name=name).first() # if this returns a book, then the name already exists in database
  92. if book: # if a book is found, we want to redirect back to create page
  93. flash('Book with same name already exists')
  94. return redirect(url_for('create.create_resource',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  95. # create a new book with the form data
  96. new_book = Resource(type=type, name=name, description=description)
  97. # add the new book to the database
  98. db.session.add(new_book)
  99. db.session.commit()
  100. resource_dropdown = Resource.query
  101. return render_template('create.html', resource_dropdown=resource_dropdown)