Explorar el Código

changes to linked resources functions

joel
Simon Bowie hace 2 años
padre
commit
0ddf1382e1
Se han modificado 5 ficheros con 56 adiciones y 25 borrados
  1. +2
    -7
      web/app/create.py
  2. +9
    -1
      web/app/practice.py
  3. +23
    -7
      web/app/resources.py
  4. +18
    -0
      web/app/templates/edit.html
  5. +4
    -10
      web/app/tool.py

+ 2
- 7
web/app/create.py Ver fichero

@@ -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'

+ 9
- 1
web/app/practice.py Ver fichero

@@ -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',))

+ 23
- 7
web/app/resources.py Ver fichero

@@ -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)

+ 18
- 0
web/app/templates/edit.html Ver fichero

@@ -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">

+ 4
- 10
web/app/tool.py Ver fichero

@@ -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)

Cargando…
Cancelar
Guardar