from flask_login import login_required, current_user | from flask_login import login_required, current_user | ||||
from .models import Resource | from .models import Resource | ||||
from .models import Relationship | from .models import Relationship | ||||
from .resources import * | |||||
from werkzeug.exceptions import abort | from werkzeug.exceptions import abort | ||||
from . import db | from . import db | ||||
if request.form.getlist('linked_resources'): | if request.form.getlist('linked_resources'): | ||||
for linked_resource in request.form.getlist('linked_resources'): | for linked_resource in request.form.getlist('linked_resources'): | ||||
tool = Resource.query.filter_by(type='tool').filter_by(name=name).first() | tool = Resource.query.filter_by(type='tool').filter_by(name=name).first() | ||||
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() | |||||
add_linked_resource(tool.id, linked_resource) | |||||
elif request.form.get('resource_type') == 'practice': | elif request.form.get('resource_type') == 'practice': | ||||
type = 'practice' | type = 'practice' |
@login_required | @login_required | ||||
def edit_practice(practice_id): | def edit_practice(practice_id): | ||||
practice = get_resource(practice_id) | practice = get_resource(practice_id) | ||||
resource_dropdown = Resource.query | |||||
links = get_linked_resources(practice_id) | |||||
if request.method == 'POST': | if request.method == 'POST': | ||||
name = request.form['name'] | name = request.form['name'] | ||||
description = request.form['description'] | description = request.form['description'] | ||||
linked_resources = request.form.getlist('linked_resources') | |||||
if not name: | if not name: | ||||
flash('Name is required!') | flash('Name is required!') | ||||
practice.name = name | practice.name = name | ||||
practice.description = description | practice.description = description | ||||
db.session.commit() | db.session.commit() | ||||
if linked_resources: | |||||
for linked_resource in request.form.getlist('linked_resources'): | |||||
link = Resource.query.get(linked_resource) | |||||
if link not in links: | |||||
add_linked_resource(practice_id, linked_resource) | |||||
return redirect(url_for('practice.get_practices')) | return redirect(url_for('practice.get_practices')) | ||||
return render_template('edit.html', resource=practice) | |||||
return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, links=links) | |||||
# route for function to delete a single practice from the edit page | # route for function to delete a single practice from the edit page | ||||
@practice.route('/practices/<int:practice_id>/delete', methods=('POST',)) | @practice.route('/practices/<int:practice_id>/delete', methods=('POST',)) |
return resource | return resource | ||||
# function to retrieve linked resources | # function to retrieve linked resources | ||||
def get_linked_resources(resource_id): | |||||
relationships = Relationship.query.filter_by(first_resource_id=resource_id).all() | |||||
def get_linked_resources(primary_id): | |||||
relationships = Relationship.query.filter_by(first_resource_id=primary_id).all() | |||||
links = [] | |||||
if relationships: | if relationships: | ||||
links = [] | links = [] | ||||
for relationship in relationships: | for relationship in relationships: | ||||
resource_id = relationship.second_resource_id | |||||
links.extend(Resource.query.filter_by(id=resource_id).all()) | |||||
secondary_id = relationship.second_resource_id | |||||
links.extend(Resource.query.filter_by(id=secondary_id).all()) | |||||
secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all() | |||||
if secondary_relationships: | |||||
for relationship in secondary_relationships: | |||||
primary_id = relationship.first_resource_id | |||||
links.extend(Resource.query.filter_by(id=primary_id).all()) | |||||
return links | return links | ||||
else: | else: | ||||
relationships = Relationship.query.filter_by(second_resource_id=resource_id).all() | |||||
relationships = Relationship.query.filter_by(second_resource_id=primary_id).all() | |||||
if relationships: | if relationships: | ||||
links = [] | links = [] | ||||
for relationship in relationships: | for relationship in relationships: | ||||
resource_id = relationship.first_resource_id | |||||
links.extend(Resource.query.filter_by(id=resource_id).all()) | |||||
primary_id = relationship.first_resource_id | |||||
links.extend(Resource.query.filter_by(id=primary_id).all()) | |||||
return links | return links | ||||
# function to add a relationship to a linked resource | |||||
def add_linked_resource(resource_id, linked_resource_id): | |||||
first_resource_id = resource_id | |||||
second_resource_id = linked_resource_id | |||||
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() | |||||
# function to delete a single resource | # function to delete a single resource | ||||
def delete_resource(resource_id): | def delete_resource(resource_id): | ||||
deletion = Resource.query.get(resource_id) | deletion = Resource.query.get(resource_id) |
{% endfor %} | {% endfor %} | ||||
</select> | </select> | ||||
</div> | </div> | ||||
{% elif resource['type'] == 'practice' %} | |||||
<div class="mb-3 mt-3"> | |||||
<label for="linked_practice_id">Linked resources</label> | |||||
</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> | |||||
{% for resource_dropdown in resource_dropdown %} | |||||
{% if resource_dropdown['type'] != 'practice' %} | |||||
{% if resource_dropdown in links %} | |||||
<option value="{{ resource_dropdown['id'] }}" selected>{{ resource_dropdown['name'] }}</option> | |||||
{% else %} | |||||
<option value="{{ resource_dropdown['id'] }}">{{ resource_dropdown['name'] }}</option> | |||||
{% endif %} | |||||
{% endif %} | |||||
{% endfor %} | |||||
</select> | |||||
</div> | |||||
{% endif %} | {% endif %} | ||||
<div class="mb-3 mt-3"> | <div class="mb-3 mt-3"> |
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') | |||||
linked_resources = 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'): | |||||
if linked_resources: | |||||
for linked_resource in linked_resources: | |||||
link = Resource.query.get(linked_resource) | link = Resource.query.get(linked_resource) | ||||
if link not in links: | if link not in links: | ||||
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() | |||||
add_linked_resource(tool_id, linked_resource) | |||||
return redirect(url_for('tool.get_tools')) | return redirect(url_for('tool.get_tools')) | ||||
return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=links) | return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=links) |