選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123 行
5.7KB

  1. # @name: tool.py
  2. # @creation_date: 2021-10-20
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: tool route for tool-related functions and pages
  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 .resources import *
  12. from .relationships import *
  13. from . import db
  14. import os
  15. from sqlalchemy.sql import func
  16. import markdown
  17. tool = Blueprint('tool', __name__)
  18. # route for displaying all tools in database
  19. @tool.route('/tools')
  20. def get_tools():
  21. # GET PARAMETERS
  22. # get URL parameters for views and pages
  23. view = request.args.get('view')
  24. page = request.args.get('page', 1, type=int)
  25. # set resource type
  26. resource_type = 'tool'
  27. # get introductory paragraph Markdown
  28. with open('content/tools.md', 'r') as f:
  29. intro_text = f.read()
  30. intro_text = markdown.markdown(intro_text)
  31. # DATABASE QUERY
  32. tools_query = Resource.query.filter_by(type=resource_type).filter_by(published=True)
  33. # FILTERING
  34. for key in request.args.keys():
  35. if key != 'view' and key != 'page':
  36. if (key == 'practice' and request.args.get(key) != ''):
  37. tools_1 = tools_query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Relationship.second_resource_id==request.args.get(key))
  38. tools_2 = tools_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key))
  39. tools_query = tools_1.union(tools_2)
  40. if (key == 'scriptingLanguage' and request.args.get(key) != ''):
  41. regex = request.args.get(key) + "$|" + request.args.get(key) + "\s\/"
  42. tools_query = tools_query.filter(Resource.scriptingLanguage.regexp_match(regex))
  43. if ((key != 'practice' and key != 'scriptingLanguage') and request.args.get(key) != ''):
  44. kwargs = {key: request.args.get(key)}
  45. tools_query = tools_query.filter_by(**kwargs)
  46. # finalise the query and add pagination
  47. tools = tools_query.order_by(Resource.name).paginate(page=page, per_page=25)
  48. # FILTERS MENU
  49. # get values for filter menu dropdowns
  50. # practices
  51. practices_filter = Resource.query.filter_by(type='practice').filter_by(published=True).with_entities(Resource.id, Resource.name).all()
  52. # license
  53. licenses_filter = get_filter_values('license', resource_type)
  54. # language
  55. languages_filter = get_filter_values('scriptingLanguage', resource_type)
  56. # status
  57. status_filter = get_filter_values('status', resource_type)
  58. # POST-FILTERING PROCESSING
  59. # if view is 'expanded' then append relationships
  60. if view != 'list':
  61. # append relationships to each book
  62. append_relationships_multiple_paginated(tools)
  63. return render_template('resources.html', resources=tools, type=resource_type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter, status_filter=status_filter, view=view, page=page, intro_text=intro_text)
  64. # route for displaying a single tool based on the ID in the database
  65. @tool.route('/tools/<int:tool_id>')
  66. def show_tool(tool_id):
  67. tool = get_full_resource(tool_id)
  68. return render_template('resource.html', resource=tool)
  69. # route for editing a single tool based on the ID in the database
  70. @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
  71. @login_required
  72. def edit_tool(tool_id):
  73. tool = get_resource(tool_id)
  74. resource_dropdown = Resource.query.order_by(Resource.name)
  75. existing_relationships = get_relationships(tool_id)
  76. if request.method == 'POST':
  77. if not request.form['name']:
  78. flash('Name is required!')
  79. else:
  80. tool = Resource.query.get(tool_id)
  81. tool.name = request.form['name']
  82. tool.description = request.form['description']
  83. tool.developer = request.form['developer']
  84. tool.developerUrl = request.form['developerUrl']
  85. tool.projectUrl = request.form['projectUrl']
  86. tool.repositoryUrl = request.form['repositoryUrl']
  87. tool.license = request.form['license']
  88. tool.scriptingLanguage = request.form['scriptingLanguage']
  89. tool.expertiseToUse = request.form['expertiseToUse']
  90. tool.expertiseToHost = request.form['expertiseToHost']
  91. tool.dependencies = request.form['dependencies']
  92. tool.ingestFormats = request.form['ingestFormats']
  93. tool.outputFormats = request.form['outputFormats']
  94. tool.status = request.form['status']
  95. db.session.commit()
  96. linked_resources = request.form.getlist('linked_practices') + request.form.getlist('linked_books')
  97. remove_linked_resources = request.form.getlist('remove_linked_resources')
  98. edit_relationships(tool_id, linked_resources, remove_linked_resources, existing_relationships)
  99. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  100. return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, relationships=existing_relationships)
  101. # route for function to delete a single tool from the edit page
  102. @tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
  103. @login_required
  104. def delete_tool(tool_id):
  105. delete_resource(tool_id)
  106. return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))