db.session.add(new_reference) | db.session.add(new_reference) | ||||
db.session.commit() | db.session.commit() | ||||
practice_dropdown = Resource.query.filter_by(type='practice') | |||||
return render_template('create.html', practice_dropdown=practice_dropdown) | |||||
resource_dropdown = Resource.query | |||||
return render_template('create.html', resource_dropdown=resource_dropdown) |
# @author: Simon Bowie <ad7588@coventry.ac.uk> | # @author: Simon Bowie <ad7588@coventry.ac.uk> | ||||
# @purpose: JavaScript functions for various functions | # @purpose: JavaScript functions for various functions | ||||
# @acknowledgements: | # @acknowledgements: | ||||
# https://stackoverflow.com/questions/67942546/bootstrap-5-select-dropdown-with-the-multiple-attribute-collapsed | |||||
*/ | */ | ||||
// Dynamic HTML forms based on dropdown menu | // Dynamic HTML forms based on dropdown menu |
# https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3 | # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3 | ||||
# Bootstrap 5.1.3: https://getbootstrap.com/ | # Bootstrap 5.1.3: https://getbootstrap.com/ | ||||
# Flask-Moment: https://flask-moment.readthedocs.io/en/latest/ | # Flask-Moment: https://flask-moment.readthedocs.io/en/latest/ | ||||
# Boostrap select: https://stackoverflow.com/questions/67942546/bootstrap-5-select-dropdown-with-the-multiple-attribute-collapsed | |||||
--> | --> | ||||
<!DOCTYPE html> | <!DOCTYPE html> |
<div class="mb-3 mt-3"> | <div class="mb-3 mt-3"> | ||||
<label for="linked_practice_id">Linked resources</label> | <label for="linked_practice_id">Linked resources</label> | ||||
<!--<select class="form-select" aria-label="Linked practices" name="linked_practice_id" multiple="multiple" id="linked_practice_id">--> | |||||
</div> | |||||
<div class="mb-3 mt-3"> | |||||
<select name="linked_resources" id="linked_resources" aria-label="Linked resources" class="selectpicker" data-live-search="true" multiple> | <select name="linked_resources" id="linked_resources" aria-label="Linked resources" class="selectpicker" data-live-search="true" multiple> | ||||
<optgroup label="Practices"> | |||||
{% for practice_dropdown in practice_dropdown %} | |||||
<option value="{{ practice_dropdown['id'] }}">{{ practice_dropdown['name'] }}</option> | |||||
{% for resource_dropdown in resource_dropdown %} | |||||
{% if resource_dropdown['type'] != 'tool' %} | |||||
<option value="{{ resource_dropdown['id'] }}">{{ resource_dropdown['name'] }}</option> | |||||
{% endif %} | |||||
{% endfor %} | {% endfor %} | ||||
</optgroup> | |||||
</select> | </select> | ||||
</div> | </div> | ||||
</div> | </div> |
value="{{ request.form['status'] or resource['status'] }}"> | value="{{ request.form['status'] or resource['status'] }}"> | ||||
</input> | </input> | ||||
</div> | </div> | ||||
<div class="mb-3 mt-3"> | |||||
<label for="linked_practice_id">Linked resources</label> | |||||
</div> | |||||
<div class="mb-3 mt-3"> | |||||
{{ links }} | |||||
<select name="linked_resources" id="linked_resources" aria-label="Linked resources" class="selectpicker" data-live-search="true" multiple> | |||||
{% for resource_dropdown in resource_dropdown %} | |||||
{% if resource_dropdown['type'] != 'tool' %} | |||||
<option value="{{ resource_dropdown['id'] }}">{{ resource_dropdown['name'] }}</option> | |||||
{% endif %} | |||||
{% endfor %} | |||||
</select> | |||||
</div> | |||||
{% endif %} | {% endif %} | ||||
<div class="mb-3 mt-3"> | <div class="mb-3 mt-3"> |
@login_required | @login_required | ||||
def edit_tool(tool_id): | def edit_tool(tool_id): | ||||
tool = get_resource(tool_id) | tool = get_resource(tool_id) | ||||
resource_dropdown = Resource.query | |||||
links = get_linked_resources(tool_id) | |||||
if request.method == 'POST': | if request.method == 'POST': | ||||
name = request.form['name'] | name = request.form['name'] | ||||
ingestFormats = request.form['ingestFormats'] | ingestFormats = request.form['ingestFormats'] | ||||
outputFormats = request.form['outputFormats'] | outputFormats = request.form['outputFormats'] | ||||
status = request.form['status'] | status = request.form['status'] | ||||
linked_resource = request.form.getlist('linked_resources') | |||||
if not name: | if not name: | ||||
flash('Name is required!') | flash('Name is required!') | ||||
tool.outputFormats = outputFormats | tool.outputFormats = outputFormats | ||||
tool.status = status | tool.status = status | ||||
db.session.commit() | db.session.commit() | ||||
if linked_resource: | |||||
for linked_resource in request.form.getlist('linked_resources'): | |||||
first_resource_id = tool_id | |||||
second_resource_id = linked_resource | |||||
new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id) | |||||
# add the new relationship to the database | |||||
db.session.add(new_relationship) | |||||
db.session.commit() | |||||
return redirect(url_for('tool.get_tools')) | return redirect(url_for('tool.get_tools')) | ||||
return render_template('edit.html', resource=tool) | |||||
return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=links) | |||||
# route for function to delete a single tool from the edit page | # route for function to delete a single tool from the edit page | ||||
@tool.route('/tools/<int:tool_id>/delete', methods=('POST',)) | @tool.route('/tools/<int:tool_id>/delete', methods=('POST',)) |