浏览代码

further work on consolidating resources into one database table

joel
Simon Bowie 2 年前
父节点
当前提交
f438598893
共有 4 个文件被更改,包括 52 次插入35 次删除
  1. +2
    -2
      web/app/create.py
  2. +2
    -2
      web/app/models.py
  3. +36
    -0
      web/app/resources.py
  4. +12
    -31
      web/app/tool.py

+ 2
- 2
web/app/create.py 查看文件

@@ -10,7 +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 Relationships
from .models import Relationship
from werkzeug.exceptions import abort
from . import db

@@ -54,7 +54,7 @@ def create_resource():
tool = Resource.query.filter_by(type='tool').filter_by(name=name).first()
first_resource_id = tool.id
second_resource_id = request.form.get('linked_practice_id')
new_relationship = Relationships(first_resource_id=first_resource_id, second_resource_id=second_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)

+ 2
- 2
web/app/models.py 查看文件

@@ -41,8 +41,8 @@ class Resource(db.Model):
zoteroUrl = db.Column(db.Text)

# table for relationships
class Relationships(db.Model):
__tablename__ = 'Relationships'
class Relationship(db.Model):
__tablename__ = 'Relationship'

id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
first_resource_id = db.Column(db.Integer)

+ 36
- 0
web/app/resources.py 查看文件

@@ -0,0 +1,36 @@
# @name: resources.py
# @version: 0.1
# @creation_date: 2022-02-23
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: functions for resources
# @acknowledgements:

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 werkzeug.exceptions import abort
from . import db

# function to retrieve data about a single resource from the database
def get_resource(resource_id):
resource = Resource.query.filter_by(id=resource_id).first()
if resource is None:
abort(404)
return resource

# function to retrieve linked resources
def get_linked_resources(resource_id):
relationships = Relationship.query.filter_by(first_resource_id=resource_id).first()
if relationships:
resource_id = relationships.second_resource_id
resources = Resource.query.filter_by(id=resource_id).all()
return resources

# function to delete a single resource
def delete_resource(resource_id):
deletion = Resource.query.get(resource_id)
db.session.delete(deletion)
db.session.commit()
flash('Successfully deleted!')

+ 12
- 31
web/app/tool.py 查看文件

@@ -10,27 +10,12 @@
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 Relationships
from .resources import *
from werkzeug.exceptions import abort
from . import db

tool = Blueprint('tool', __name__)

# function to retrieve data about a single tool from the database
def get_tool(tool_id):
tool = Resource.query.filter_by(id=tool_id).first()
if tool is None:
abort(404)
return tool

# function to retrieve linked resources
def get_linked_resources(tool_id):
relationships = Relationships.query.filter_by(first_resource_id=tool_id).first()
if relationships:
resource_id = relationships.second_resource_id
resources = Resource.query.filter_by(id=resource_id).all()
return resources

# route for displaying all tools in database
@tool.route('/tools')
def get_tools():
@@ -40,7 +25,7 @@ def get_tools():
# route for displaying a single tool based on the ID in the database
@tool.route('/tools/<int:tool_id>')
def show_tool(tool_id):
tool = get_tool(tool_id)
tool = get_resource(tool_id)
resources = get_linked_resources(tool_id)
return render_template('tool.html', tool=tool, resources=resources)

@@ -48,7 +33,7 @@ def show_tool(tool_id):
@tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))
@login_required
def edit_tool(tool_id):
tool = get_tool(tool_id)
tool = get_resource(tool_id)

if request.method == 'POST':
name = request.form['name']
@@ -60,14 +45,14 @@ def edit_tool(tool_id):
tool = Resource.query.get(tool_id)
tool.name = name
tool.description = description
tool.projectUrl = project_url
tool.repositoryUrl = repository_url
tool.dependencies = dependencies
tool.expertiseToUse = expertise
tool.expertiseToHost = self_host_expertise
tool.ingestFormats = ingest
tool.outputFormats = output
tool.status = status
# tool.projectUrl = project_url
# tool.repositoryUrl = repository_url
# tool.dependencies = dependencies
# tool.expertiseToUse = expertise
# tool.expertiseToHost = self_host_expertise
# tool.ingestFormats = ingest
# tool.outputFormats = output
# tool.status = status
db.session.commit()
return redirect(url_for('tool.get_tools'))

@@ -77,9 +62,5 @@ def edit_tool(tool_id):
@tool.route('/tools/<int:tool_id>/delete', methods=('POST',))
@login_required
def delete_tool(tool_id):
tool = get_tool(tool_id)
deletion = Resource.query.get(tool_id)
db.session.delete(deletion)
db.session.commit()
flash('Successfully deleted!')
delete_resource(tool_id)
return redirect(url_for('tool.get_tools'))

正在加载...
取消
保存