@practice.route('/practices/<int:practice_id>') | @practice.route('/practices/<int:practice_id>') | ||||
def show_practice(practice_id): | def show_practice(practice_id): | ||||
practice = get_full_resource(practice_id) | practice = get_full_resource(practice_id) | ||||
practice.references = replace_urls(practice.references) | |||||
return render_template('resource.html', resource=practice) | return render_template('resource.html', resource=practice) | ||||
# route for editing a single practice based on the ID in the database | # route for editing a single practice based on the ID in the database |
links.extend(Resource.query.filter_by(id=primary_id).all()) | links.extend(Resource.query.filter_by(id=primary_id).all()) | ||||
return links | return links | ||||
# function to append relationships to a resource object | |||||
def append_relationships(resource): | |||||
relationships = get_relationships(resource.id) | |||||
if relationships: | |||||
for relationship in relationships: | |||||
if relationship.type == 'tool': | |||||
if 'tools' not in resource.__dict__.keys(): | |||||
resource.__dict__['tools'] = relationship | |||||
elif type(resource.__dict__['tools']) == list: | |||||
resource.__dict__['tools'].append(relationship) | |||||
else: | |||||
resource.__dict__['tools'] = [resource.__dict__['tools'], relationship] | |||||
elif relationship.type == 'practice': | |||||
if 'practices' not in resource.__dict__.keys(): | |||||
resource.__dict__['practices'] = relationship | |||||
elif type(resource.__dict__['practices']) == list: | |||||
resource.__dict__['practices'].append(relationship) | |||||
else: | |||||
resource.__dict__['practices'] = [resource.__dict__['practices'], relationship] | |||||
elif relationship.type == 'book': | |||||
if 'books' not in resource.__dict__.keys(): | |||||
resource.__dict__['books'] = relationship | |||||
elif type(resource.__dict__['books']) == list: | |||||
resource.__dict__['books'].append(relationship) | |||||
else: | |||||
resource.__dict__['books'] = [resource.__dict__['books'], relationship] | |||||
return resource | |||||
else: | |||||
return resource | |||||
# function to add a relationship to a linked resource | # function to add a relationship to a linked resource | ||||
def add_relationship(resource_id, linked_resource_id): | def add_relationship(resource_id, linked_resource_id): | ||||
first_resource_id = resource_id | first_resource_id = resource_id |
# function to retrieve data about a resource and its relationships | # function to retrieve data about a resource and its relationships | ||||
def get_full_resource(resource_id): | def get_full_resource(resource_id): | ||||
resource = get_resource(resource_id) | resource = get_resource(resource_id) | ||||
relationships = get_relationships(resource_id) | |||||
for relationship in relationships: | |||||
if relationship.type == 'tool': | |||||
if 'tools' not in resource.__dict__.keys(): | |||||
resource.__dict__['tools'] = relationship | |||||
elif type(resource.__dict__['tools']) == list: | |||||
resource.__dict__['tools'].append(relationship) | |||||
else: | |||||
resource.__dict__['tools'] = [resource.__dict__['tools'], relationship] | |||||
elif relationship.type == 'practice': | |||||
if 'practices' not in resource.__dict__.keys(): | |||||
resource.__dict__['practices'] = relationship | |||||
elif type(resource.__dict__['practices']) == list: | |||||
resource.__dict__['practices'].append(relationship) | |||||
else: | |||||
resource.__dict__['practices'] = [resource.__dict__['practices'], relationship] | |||||
elif relationship.type == 'book': | |||||
if 'books' not in resource.__dict__.keys(): | |||||
resource.__dict__['books'] = relationship | |||||
elif type(resource.__dict__['books']) == list: | |||||
resource.__dict__['books'].append(relationship) | |||||
else: | |||||
resource.__dict__['books'] = [resource.__dict__['books'], relationship] | |||||
resource = append_relationships(resource) | |||||
if resource.type == 'book': | if resource.type == 'book': | ||||
book_data = get_book_data(resource.isbn) | book_data = get_book_data(resource.isbn) | ||||
resource.__dict__.update(book_data) | |||||
if book_data: | |||||
resource.__dict__.update(book_data) | |||||
return resource | return resource | ||||
# function to delete a single resource | # function to delete a single resource |