Skip to content

Commit

Permalink
fix: Normalising Children and New Nodes (#111)
Browse files Browse the repository at this point in the history
Code added normalises the nodes using the normalising function in the API calls of:
* Created
* Updated Nodes
* Updated Nodes Children

Fixes #79 (Children and new node id should be normalized in the backend)
  • Loading branch information
aerial-ace1 authored Oct 19, 2022
1 parent 6fb1b4c commit 118a60f
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions backend/editor/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ def create_node(label, entry, main_language_code):
"""
Helper function used for creating a node with given id and label
"""
#Normalising new Node ID

normalised_entry = normalizing(entry, main_language_code)

query = [f"""CREATE (n:{label})\n"""]
params = {"id": entry}
params = {"id": normalised_entry}

# Build all basic keys of a node
if (label == "ENTRY"):
canonical_tag = entry.split(":", 1)[1]
canonical_tag = normalised_entry.split(":", 1)[1]
query.append(f""" SET n.main_language = $main_language_code """) # Required for only an entry
params["main_language_code"] = main_language_code
else:
Expand Down Expand Up @@ -173,13 +177,30 @@ def update_nodes(label, entry, new_node_keys):
continue
query.append(f"""\nREMOVE n.{key}\n""")

# Adding normalized tags ids corresponding to entry tags
normalised_new_node_key = {}
for keys in new_node_keys.keys():
if keys.startswith("tags_") and not keys.endswith("_str"):
if "_ids_" not in keys:
keys_language_code = keys.split('_', 1)[1]
normalised_value = []
for values in new_node_keys[keys]:
normalised_value.append(normalizing(values, keys_language_code))
normalised_new_node_key[keys] = normalised_value
normalised_new_node_key["tags_ids_"+keys_language_code] = normalised_value
else:
pass # we generate tags_ids, and ignore the one sent
else:
# No need to normalise
normalised_new_node_key[keys] = new_node_keys[keys]

# Update keys
for key in new_node_keys.keys():
for key in normalised_new_node_key.keys():
query.append(f"""\nSET n.{key} = ${key}\n""")

query.append(f"""RETURN n""")

params = dict(new_node_keys, id=entry)
params = dict(normalised_new_node_key, id=entry)
result = get_current_transaction().run(" ".join(query), params)
return result

Expand All @@ -206,16 +227,19 @@ def update_node_children(entry, new_children_ids):
existing_ids = [record['child.id'] for record in get_current_transaction().run(query, ids=list(added_children))]
to_create = added_children - set(existing_ids)

# Normalising new children node ID
created_child_ids = []
for child in to_create:
main_language_code = child.split(":", 1)[0]
create_node("ENTRY", child, main_language_code)
created_node = create_node("ENTRY", child, main_language_code)
created_child_ids.append(created_node.id)

# TODO: We would prefer to add the node just after its parent entry
add_node_to_end("ENTRY", child)

# Stores result of last query executed
result = []
for child in added_children:
for child in created_child_ids:
# Create new relationships if it doesn't exist
query = f"""
MATCH (parent:ENTRY), (new_child:ENTRY) WHERE parent.id = $id AND new_child.id = $child
Expand Down

0 comments on commit 118a60f

Please sign in to comment.