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(backend): fix create_node function #393

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions backend/editor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,26 +403,22 @@ async def upload_taxonomy(
return result


@app.post("/{taxonomy_name}/{branch}/nodes")
async def create_node(request: Request, branch: str, taxonomy_name: str):
@app.post("/{taxonomy_name}/{branch}/entry")
async def create_entry_node(request: Request, branch: str, taxonomy_name: str):
perierc marked this conversation as resolved.
Show resolved Hide resolved
"""
Creating a new node in a taxonomy
Creating a new entry node in a taxonomy
"""
taxonomy = TaxonomyGraph(branch, taxonomy_name)
incoming_data = await request.json()
id = incoming_data["id"]
name = incoming_data["name"]
main_language = incoming_data["main_language"]
if id is None:
raise HTTPException(status_code=400, detail="Invalid id")
if name is None:
raise HTTPException(status_code=400, detail="Invalid node name")
if main_language is None:
raise HTTPException(status_code=400, detail="Invalid main language code")
perierc marked this conversation as resolved.
Show resolved Hide resolved

label = taxonomy.get_label(id)
normalized_id = await taxonomy.create_node(label, id, main_language)
if label == "ENTRY":
await taxonomy.add_node_to_end(label, normalized_id)
else:
await taxonomy.add_node_to_beginning(label, normalized_id)
normalized_id = await taxonomy.create_entry_node(name, main_language)
await taxonomy.add_node_to_end("ENTRY", normalized_id)
perierc marked this conversation as resolved.
Show resolved Hide resolved


@app.post("/{taxonomy_name}/{branch}/entry/{entry}")
Expand Down
27 changes: 27 additions & 0 deletions backend/editor/controllers/node_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from openfoodfacts_taxonomy_parser import utils as parser_utils

from ..graph_db import get_current_transaction
from ..models.node_models import EntryNodeCreate


async def delete_project_nodes(project_id: str):
Expand All @@ -12,3 +15,27 @@ async def delete_project_nodes(project_id: str):
DETACH DELETE n
"""
await get_current_transaction().run(query)


async def create_entry_node(
project_id: str, entry_node: EntryNodeCreate, stopwords: dict[str, list[str]]
) -> str:
"""
Creates a new entry node in the database
"""
name, language_code = entry_node.name, entry_node.main_language_code
normalized_name = parser_utils.normalize_text(name, language_code, stopwords=stopwords)

# Create params dict
params = {"entry_node": {"main_language": language_code, "preceding_lines": []}}
params["entry_node"]["id"] = language_code + ":" + normalized_name
params["entry_node"][f"tags_{language_code}"] = [name]
params["entry_node"][f"tags_ids_{language_code}"] = [normalized_name]
perierc marked this conversation as resolved.
Show resolved Hide resolved

query = f"""
CREATE (n:{project_id}:ENTRY $entry_node)
RETURN n.id
"""

result = await get_current_transaction().run(query, params)
return (await result.data())[0]["n.id"]
44 changes: 12 additions & 32 deletions backend/editor/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from openfoodfacts_taxonomy_parser import utils as parser_utils

from . import settings
from .controllers.node_controller import create_entry_node
from .controllers.project_controller import create_project, edit_project, get_project
from .exceptions import GithubBranchExistsError # Custom exceptions
from .exceptions import (
Expand All @@ -30,6 +31,7 @@
TransactionCtx,
get_current_transaction,
)
from .models.node_models import EntryNodeCreate
from .models.project_models import ProjectCreate, ProjectEdit, ProjectStatus
from .utils import file_cleanup

Expand Down Expand Up @@ -59,40 +61,17 @@ def get_label(self, id):
else:
return "ENTRY"

async def create_node(self, label, entry, main_language_code):
async def create_entry_node(self, name, main_language_code) -> str:
"""
Helper function used for creating a node with given id and label
Helper function used to create an entry node with given name and main language
"""
params = {"id": entry}
query = [f"""CREATE (n:{self.project_name}:{label})\n"""]
stopwords = await self.get_stopwords_dict()

# Build all basic keys of a node
if label == "ENTRY":
# Normalizing new canonical tag
language_code, canonical_tag = entry.split(":", 1)
normalised_canonical_tag = parser_utils.normalize_text(
canonical_tag, main_language_code, stopwords=stopwords
)

# Reconstructing and updation of node ID
params["id"] = language_code + ":" + normalised_canonical_tag
params["main_language_code"] = main_language_code

query.append(
""" SET n.main_language = $main_language_code """
) # Required for only an entry
else:
canonical_tag = ""

query.append(""" SET n.id = $id """)
query.append(f""" SET n.tags_{main_language_code} = [$canonical_tag] """)
query.append(""" SET n.preceding_lines = [] """)
query.append(""" RETURN n.id """)

params["canonical_tag"] = canonical_tag
result = await get_current_transaction().run(" ".join(query), params)
return (await result.data())[0]["n.id"]
return await create_entry_node(
self.project_name,
EntryNodeCreate(name=name, main_language_code=main_language_code),
stopwords,
)
perierc marked this conversation as resolved.
Show resolved Hide resolved

async def get_local_taxonomy_file(self, tmpdir: str, uploadfile: UploadFile):
filename = uploadfile.filename
Expand Down Expand Up @@ -345,6 +324,7 @@ async def add_node_to_end(self, label, entry):
"""
await get_current_transaction().run(query, {"id": entry, "endnodeid": end_node["id"]})

# UNUSED FOR NOW
async def add_node_to_beginning(self, label, entry):
"""
Helper function which adds an existing node to beginning of taxonomy
Expand Down Expand Up @@ -609,8 +589,8 @@ async def update_node_children(self, entry, new_children_ids):
created_child_ids = []

for child in to_create:
main_language_code = child.split(":", 1)[0]
created_node_id = await self.create_node("ENTRY", child, main_language_code)
main_language_code, child_name = child.split(":", 1)
created_node_id = await self.create_entry_node(child_name, main_language_code)
created_child_ids.append(created_node_id)

# TODO: We would prefer to add the node just after its parent entry
Expand Down
13 changes: 6 additions & 7 deletions backend/editor/models/node_models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""
Required pydantic models for API
"""
from typing import List

from .base_models import BaseModel


class Marginal(BaseModel):
preceding_lines: List
class Header(BaseModel):
pass


class Header(Marginal):
class Footer(BaseModel):
pass


class Footer(Marginal):
pass
class EntryNodeCreate(BaseModel):
name: str
main_language_code: str
perierc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ const CreateNodeDialogContent = ({

const baseUrl = createBaseURL(taxonomyName, branchName);

const handleAddNode = () => {
const handleAddEntryNode = () => {
setIsFailedSubmit(false);

// TODO: Add support for synonyms and stopwords
const data = { name: newNodeName, main_language: newLanguageCode };

const newNodeID = newLanguageCode + ":" + newNodeName; // Reconstructing node ID
const data = { id: newNodeID, main_language: newLanguageCode };

fetch(`${baseUrl}nodes`, {
fetch(`${baseUrl}entry`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
Expand Down Expand Up @@ -95,7 +92,7 @@ const CreateNodeDialogContent = ({
<Button onClick={onCloseDialog}>Cancel</Button>
<Button
disabled={!newLanguageCode || !newNodeName}
onClick={handleAddNode}
onClick={handleAddEntryNode}
>
Create
</Button>
Expand Down
2 changes: 1 addition & 1 deletion taxonomy-editor-frontend/src/pages/root-nodes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const RootNodes = ({
<Table style={{ border: "solid", borderWidth: 1.5 }}>
<TableHead>
<TableCell align="left">
<Typography variant="h6">Taxonony Name</Typography>
<Typography variant="h6">Taxonomy Name</Typography>
</TableCell>
<TableCell align="left">
<Typography variant="h6">Branch Name</Typography>
Expand Down
Loading