name = request.form['name'] | name = request.form['name'] | ||||
description = request.form['description'] | description = request.form['description'] | ||||
linked_resources = request.form.getlist('linked_resources') | linked_resources = request.form.getlist('linked_resources') | ||||
remove_linked_resources = request.form.getlist('remove_linked_resources') | |||||
if not name: | if not name: | ||||
flash('Name is required!') | flash('Name is required!') | ||||
add_linked_resource(practice_id, linked_resource) | add_linked_resource(practice_id, linked_resource) | ||||
elif not links: | elif not links: | ||||
add_linked_resource(practice_id, linked_resource) | add_linked_resource(practice_id, linked_resource) | ||||
if remove_linked_resources: | |||||
for remove_linked_resource in remove_linked_resources: | |||||
delete_relationship(practice_id, remove_linked_resource) | |||||
return redirect(url_for('practice.get_practices')) | return redirect(url_for('practice.get_practices')) | ||||
return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, links=links) | return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, links=links) |
flash('Successfully deleted!') | flash('Successfully deleted!') | ||||
# function to delete a single relationship | # function to delete a single relationship | ||||
def delete_relationship(first_resource_id, second_resource_id): | |||||
relation = Relationship.query.filter((Relationship.first_resource_id == first_resource_id) & (Relationship.second_resource_id == second_resource_id)).first() | |||||
def delete_relationship(main_id, for_deletion_id): | |||||
relation = Relationship.query.filter(((Relationship.first_resource_id == main_id) & (Relationship.second_resource_id == for_deletion_id)) | ((Relationship.first_resource_id == for_deletion_id) & (Relationship.second_resource_id == main_id))).first() | |||||
deletion = Relationship.query.get(relation.id) | deletion = Relationship.query.get(relation.id) | ||||
db.session.delete(deletion) | db.session.delete(deletion) | ||||
db.session.commit() | db.session.commit() |
{% for resource_dropdown in resource_dropdown %} | {% for resource_dropdown in resource_dropdown %} | ||||
{% if resource_dropdown['type'] != 'tool' %} | {% if resource_dropdown['type'] != 'tool' %} | ||||
{% if links and resource_dropdown in links %} | {% if links and resource_dropdown in links %} | ||||
<option value="{{ resource_dropdown['id'] }}" selected>{{ resource_dropdown['name'] }}</option> | |||||
<option value="" selected>{{ resource_dropdown['name'] }}</option> | |||||
{% else %} | {% else %} | ||||
<option value="{{ resource_dropdown['id'] }}">{{ resource_dropdown['name'] }}</option> | <option value="{{ resource_dropdown['id'] }}">{{ resource_dropdown['name'] }}</option> | ||||
{% endif %} | {% endif %} |