Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Normalising Children and New Nodes #111

Merged
merged 7 commits into from
Oct 19, 2022

Conversation

aerial-ace1
Copy link
Contributor

@aerial-ace1 aerial-ace1 commented Oct 11, 2022

What

Code added normalises the nodes using the normalising function in the API calls of:

  1. Created
  2. Updated Nodes
  3. Updated Nodes Children

Fixes bug(s)

@aerial-ace1 aerial-ace1 requested a review from a team as a code owner October 11, 2022 12:12
@aadarsh-ram aadarsh-ram changed the title Normalising Children and New Nodes fix: Normalising Children and New Nodes Oct 11, 2022
Copy link
Collaborator

@aadarsh-ram aadarsh-ram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aerial-ace1 Thank you for your contribution :) I think this PR requires a few changes which need to be addressed, after which it can be merged.

backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
backend/editor/entries.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@aadarsh-ram aadarsh-ram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment, I think I'll add it.

backend/editor/entries.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@aadarsh-ram aadarsh-ram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'd like @alexgarel to review this.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @aerial-ace1 great job for you spotted more than the ticket was saying

Still I would like you to make some fix, and we will be good.

@@ -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).split(':', 1)[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not make the split here, should you ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... yup.


normalised_new_node_keys["tags_ids_"+keys_language_code] = normalised_value
else:
normalised_new_node_keys[keys] = new_node_keys[keys]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
normalised_new_node_keys[keys] = new_node_keys[keys]
# no need to normalize
normalised_new_node_keys[keys] = new_node_keys[keys]

@@ -173,13 +177,28 @@ def update_nodes(label, entry, new_node_keys):
continue
query.append(f"""\nREMOVE n.{key}\n""")

#Normalising Node to be Updated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#Normalising Node to be Updated
# Adding normalized tags ids corresponding to entry tags

@@ -173,13 +177,28 @@ def update_nodes(label, entry, new_node_keys):
continue
query.append(f"""\nREMOVE n.{key}\n""")

#Normalising Node to be Updated
normalised_new_node_keys = {}
for keys in new_node_keys.keys():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should name it key, not keys

Comment on lines 183 to 184
if keys.startswith("tags") and not keys.endswith("str"):
if "ids" not in keys:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the "_" to make the condition even more specific and explicit

Suggested change
if keys.startswith("tags") and not keys.endswith("str"):
if "ids" not in keys:
if keys.startswith("tags_") and not keys.endswith("_str"):
if "_ids_" not in keys:
# generate tags_ids from values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

normalised_value.append(normalizing(values, keys_language_code))
normalised_new_node_keys[keys] = new_node_keys[keys]

normalised_new_node_keys["tags_ids_"+keys_language_code] = normalised_value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a else to make explicit it's intended

Suggested change
normalised_new_node_keys["tags_ids_"+keys_language_code] = normalised_value
normalised_new_node_keys["tags_ids_"+keys_language_code] = normalised_value
else:
pass # we generate tags_ids, and ignore the one sent

@@ -206,7 +225,13 @@ 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)

for child in to_create:
#Normalising new children node ID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pep8 ask for space after the #

Suggested change
#Normalising new children node ID
# Normalising new children node ID

normalised_added_children = set()
for child_node in to_create:
child_language_code, new_child_name = child_node.split(":",1)
normalised_added_children.add(normalizing(new_child_name,child_language_code))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to put back language code !

Suggested change
normalised_added_children.add(normalizing(new_child_name,child_language_code))
normalised_added_children.add(
f"{child_language_code}:{normalizing(new_child_name, child_language_code)}")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but see below because I propose you to remove this part.

Comment on lines 228 to 232
#Normalising new children node ID
normalised_added_children = set()
for child_node in to_create:
child_language_code, new_child_name = child_node.split(":",1)
normalised_added_children.add(normalizing(new_child_name,child_language_code))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As create_node is used below and it already normalize id I don't think we need this (and this avoid duplication of code).

Instead I propose to build normalised_added_children while creating nodes (see below)

Comment on lines 234 to 236
for child in normalised_added_children:
main_language_code = child.split(":", 1)[0]
create_node("ENTRY", child, main_language_code)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to instead do

Suggested change
for child in normalised_added_children:
main_language_code = child.split(":", 1)[0]
create_node("ENTRY", child, main_language_code)
normalised_added_children = []
for child in to_create:
main_language_code = child.split(":", 1)[0]
created_node = create_node("ENTRY", child, main_language_code)
normalised_added_children.append(created_node.id)

By the way we can use a shorter name like created_ids instead of normalised_added_children

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @aerial-ace1,
I will just add one comment before merging :-)

Thanks, it's a much appreciated contribution.

backend/editor/entries.py Outdated Show resolved Hide resolved
@alexgarel alexgarel merged commit 118a60f into openfoodfacts:main Oct 19, 2022
@alexgarel
Copy link
Member

@aerial-ace1: a small tip: don't make changes on your fork's main branch, as it will prevent you from cleanly update you fork from upstream project. Create a new branch for each PR.

@aadarsh-ram
Copy link
Collaborator

Thanks for the contribution @aerial-ace1!🎉

@aerial-ace1
Copy link
Contributor Author

Sure will do so from now on @alexgarel. Was fun doing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Children and new node id should be normalized in the backend
3 participants