Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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