Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lazarocosta committed Apr 30, 2020
1 parent 02a4472 commit 6d06d46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
13 changes: 7 additions & 6 deletions src/Routes/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
# print(document)


def update_data(uid, data):
def update_data_in_mongo(uid, data):
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
result = db.data.find_one({"uid": uid})
if result is None:
db.data.insert_one({"uid": uid, "data": data})
db.data.insert_one({"uid": uid, "data": data, "timestamp": date_str})
print("created")
else:
new_node = db.data.find_one_and_replace({"_id": result["_id"]}, {"data": data})
db.data.find_one_and_replace({"_id": result["_id"]}, {"uid": uid, "data": data, "timestamp": date_str})
print("updated")
print(new_node)


def update_template(classes_name, template):
def update_template_in_mongo(classes_name, template):
template_str = json.dumps(template)
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
Expand Down Expand Up @@ -90,7 +91,7 @@ def delete_collection(collection):
db[collection].delete_many({})


delete_collection("template")
delete_collection("data")
# unique = datetime.now()
# #update_data(5552,"aaa")
# class_name = ['ola', '1', '2', '3']
Expand Down
14 changes: 10 additions & 4 deletions src/Routes/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os, sys
import argparse

from src.Routes.mongo import update_template, get_all_records_from_collection, update_data
from src.Routes.mongo import update_template_in_mongo, get_all_records_from_collection, update_data_in_mongo

parser = argparse.ArgumentParser(description="Starts the archgraph server.")

Expand Down Expand Up @@ -99,7 +99,7 @@ def update_schema_of_node_in_mongodb(uid):
if node is not None:
template_of_node = node.get_schema_with_template(template)
classes_name = node.get_superclasses_name()
message = update_template(classes_name, template_of_node)
message = update_template_in_mongo(classes_name, template_of_node)
get_all_records_from_collection("template")

return make_response(jsonify(message=message), 200)
Expand Down Expand Up @@ -131,13 +131,19 @@ def response_get_schema_node_with_template(uid):
@app.route("/<uid>", methods=["POST"])
@cross_origin()
def response_update(uid):
template = {
"E52_Time_Span": {
"has_value": "DataObject",
}
}
node = get_node_by_uid(uid)
if node is not None:
data = request.json
merged = updated_node(node, data)
if merged:
new_data = node.encodeJSON()
#update_data(uid, new_data)
new_data = nested_json(node, template)
update_data_in_mongo(uid, new_data)
get_all_records_from_collection("data")
return make_response(jsonify(new_data), 201)
else:
return make_response(jsonify(message="Unsaved node"), 404)
Expand Down
48 changes: 24 additions & 24 deletions src/Utils/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ def get_driver():
return self.driver


def read_relationships(search_node, search_node_uid, relationship_name):
def read(tx, search_node, search_node_uid, relationship_name):
array_uids = []
records = tx.run(
"MATCH (a: "
+ search_node
+ "{ uid:'"
+ search_node_uid
+ "'}"
+ ")-[: "
+ relationship_name
+ "]->(nested_node) "
"Return nested_node.uid"
)
for record in records:
array_uids.append(record[0])
return array_uids

with get_driver().session() as session:
return session.read_transaction(
read, search_node, search_node_uid, relationship_name
)


def nested_json(node, template):
if isinstance(template, str):
return node.decodeJSON()
Expand Down Expand Up @@ -97,30 +121,6 @@ def nested_json(node, template):
return dict(node.decodeJSON(), **new_object)


def read_relationships(search_node, search_node_uid, relationship_name):
def read(tx, search_node, search_node_uid, relationship_name):
array_uids = []
records = tx.run(
"MATCH (a: "
+ search_node
+ "{ uid:'"
+ search_node_uid
+ "'}"
+ ")-[: "
+ relationship_name
+ "]->(nested_node) "
"Return nested_node.uid"
)
for record in records:
array_uids.append(record[0])
return array_uids

with get_driver().session() as session:
return session.read_transaction(
read, search_node, search_node_uid, relationship_name
)


def get_node_by_uid(uid):
try:
return DataObject.nodes.get(uid=uid)
Expand Down

0 comments on commit 6d06d46

Please sign in to comment.