Browse Source

moved some duplicate code to its own function

joel
Simon Bowie 2 years ago
parent
commit
5266a31de3
3 changed files with 17 additions and 20 deletions
  1. +2
    -10
      web/app/practice.py
  2. +13
    -0
      web/app/relationships.py
  3. +2
    -10
      web/app/tool.py

+ 2
- 10
web/app/practice.py View File

@@ -49,16 +49,8 @@ def edit_practice(practice_id):
linked_resources = request.form.getlist('linked_resources')
remove_linked_resources = request.form.getlist('remove_linked_resources')

if linked_resources:
for linked_resource in linked_resources:
link = Resource.query.get(linked_resource)
if existing_relationships and link not in existing_relationships:
add_relationship(practice_id, linked_resource)
elif not existing_relationships:
add_relationship(practice_id, linked_resource)
if remove_linked_resources:
for remove_linked_resource in remove_linked_resources:
delete_relationship(practice_id, remove_linked_resource)
edit_relationships(practice_id, linked_resources, remove_linked_resources, existing_relationships)

return redirect(url_for('practice.get_practices',_external=True,_scheme=os.environ.get('SSL_SCHEME')))

return render_template('edit.html', resource=practice, resource_dropdown=resource_dropdown, links=existing_relationships)

+ 13
- 0
web/app/relationships.py View File

@@ -51,3 +51,16 @@ def delete_relationship(main_id, for_deletion_id):
deletion = Relationship.query.get(relation.id)
db.session.delete(deletion)
db.session.commit()
# logic for editing relationships
def edit_relationships(resource_id, linked_resources, remove_linked_resources, existing_relationships):
if linked_resources:
for linked_resource in linked_resources:
link = Resource.query.get(linked_resource)
if existing_relationships and link not in existing_relationships:
add_relationship(resource_id, linked_resource)
elif not existing_relationships:
add_relationship(resource_id, linked_resource)
if remove_linked_resources:
for remove_linked_resource in remove_linked_resources:
delete_relationship(resource_id, remove_linked_resource)

+ 2
- 10
web/app/tool.py View File

@@ -61,16 +61,8 @@ def edit_tool(tool_id):
linked_resources = request.form.getlist('linked_resources')
remove_linked_resources = request.form.getlist('remove_linked_resources')

if linked_resources:
for linked_resource in linked_resources:
link = Resource.query.get(linked_resource)
if existing_relationships and link not in existing_relationships:
add_relationship(tool_id, linked_resource)
elif not existing_relationships:
add_relationship(tool_id, linked_resource)
if remove_linked_resources:
for remove_linked_resource in remove_linked_resources:
delete_relationship(tool_id, remove_linked_resource)
edit_relationships(tool_id, linked_resources, remove_linked_resources, existing_relationships)
return redirect(url_for('tool.get_tools',_external=True,_scheme=os.environ.get('SSL_SCHEME')))

return render_template('edit.html', resource=tool, resource_dropdown=resource_dropdown, links=existing_relationships)

Loading…
Cancel
Save