Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #424 from multinet-app/check-exists-before-upload
Browse files Browse the repository at this point in the history
Check graph/table existence before reading request body
  • Loading branch information
jjnesbitt committed Jul 13, 2020
2 parents 22684b2 + f7d6529 commit 8cb5eca
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
14 changes: 7 additions & 7 deletions multinet/uploaders/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ def upload(
`data` - the CSV data, passed in the request body. If the CSV data contains
`_from` and `_to` fields, it will be treated as an edge table.
"""
space = db.get_workspace_db(workspace)
if space.has_collection(table):
raise AlreadyExists("table", table)

app.logger.info("Bulk Loading")

# Read the request body into CSV format
Expand All @@ -187,13 +191,9 @@ def upload(

# Set the collection, paying attention to whether the data contains
# _from/_to fields.
space = db.get_workspace_db(workspace)
if space.has_collection(table):
raise AlreadyExists("table", table)
else:
fieldnames = rows[0].keys()
edges = "_from" in fieldnames and "_to" in fieldnames
coll = space.create_collection(table, edge=edges)
fieldnames = rows[0].keys()
edges = "_from" in fieldnames and "_to" in fieldnames
coll = space.create_collection(table, edge=edges)

# Insert the data into the collection.
results = coll.insert_many(rows)
Expand Down
8 changes: 4 additions & 4 deletions multinet/uploaders/d3_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def upload(workspace: str, graph: str) -> Any:
`data` - the json data, passed in the request body. The json data should contain
nodes: [] and links: []
"""
space = db.get_workspace_db(workspace)
if space.has_graph(graph):
raise AlreadyExists("graph", graph)

# Get data from the request and load it as json
body = decode_data(request.data)
data = json.load(StringIO(body), object_pairs_hook=OrderedDict)
Expand All @@ -84,10 +88,6 @@ def upload(workspace: str, graph: str) -> Any:
if len(errors) > 0:
raise ValidationFailed(errors)

space = db.get_workspace_db(workspace)
if space.has_graph(graph):
raise AlreadyExists("graph", graph)

node_table_name = f"{graph}_nodes"
edge_table_name = f"{graph}_links"

Expand Down
6 changes: 3 additions & 3 deletions multinet/uploaders/nested_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ def upload(workspace: str, graph: str) -> Any:
`graph` - the target graph.
`data` - the nested_json data, passed in the request body.
"""
# Set up the parameters.
data = request.data.decode("utf8")

space = db.get_workspace_db(workspace)
if space.has_graph(graph):
raise AlreadyExists("graph", graph)

# Set up the parameters.
data = request.data.decode("utf8")

edgetable_name = f"{graph}_edges"
int_nodetable_name = f"{graph}_internal_nodes"
leaf_nodetable_name = f"{graph}_leaf_nodes"
Expand Down
10 changes: 4 additions & 6 deletions multinet/uploaders/newick.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ def upload(workspace: str, graph: str) -> Any:
"""
app.logger.info("newick tree")

body = decode_data(request.data)

tree = newick.loads(body)

validate_newick(tree)

space = db.get_workspace_db(workspace)
if space.has_graph(graph):
raise AlreadyExists("graph", graph)

body = decode_data(request.data)
tree = newick.loads(body)
validate_newick(tree)

edgetable_name = f"{graph}_edges"
nodetable_name = f"{graph}_nodes"

Expand Down

0 comments on commit 8cb5eca

Please sign in to comment.