@@ -10,6 +10,7 @@ from flask import Blueprint, render_template, request, flash, redirect, url_for | |||
from flask_login import login_required, current_user | |||
from .models import Resource | |||
from .models import Relationship | |||
from .resources import * | |||
from werkzeug.exceptions import abort | |||
from . import db | |||
@@ -52,13 +53,7 @@ def create_resource(): | |||
if 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() | |||
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': | |||
type = 'practice' |
@@ -33,10 +33,13 @@ def show_practice(practice_id): | |||
@login_required | |||
def edit_practice(practice_id): | |||
practice = get_resource(practice_id) | |||
resource_dropdown = Resource.query | |||
links = get_linked_resources(practice_id) | |||
if request.method == 'POST': | |||
name = request.form['name'] | |||
description = request.form['description'] | |||
linked_resources = request.form.getlist('linked_resources') | |||
if not name: | |||
flash('Name is required!') | |||
@@ -45,9 +48,14 @@ def edit_practice(practice_id): | |||
practice.name = name | |||
practice.description = description | |||
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 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 | |||
@practice.route('/practices/<int:practice_id>/delete', methods=('POST',)) |
@@ -20,23 +20,39 @@ def get_resource(resource_id): | |||
return resource | |||
# 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: | |||
links = [] | |||
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 | |||
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: | |||
links = [] | |||
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 | |||
# 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 | |||
def delete_resource(resource_id): | |||
deletion = Resource.query.get(resource_id) |
@@ -91,6 +91,24 @@ | |||
{% endfor %} | |||
</select> | |||
</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 %} | |||
<div class="mb-3 mt-3"> |
@@ -47,7 +47,7 @@ def edit_tool(tool_id): | |||
ingestFormats = request.form['ingestFormats'] | |||
outputFormats = request.form['outputFormats'] | |||
status = request.form['status'] | |||
linked_resource = request.form.getlist('linked_resources') | |||
linked_resources = request.form.getlist('linked_resources') | |||
if not name: | |||
flash('Name is required!') | |||
@@ -64,17 +64,11 @@ def edit_tool(tool_id): | |||
tool.outputFormats = outputFormats | |||
tool.status = status | |||
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) | |||
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 render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=links) |