Skip to content

Commit 019e04b

Browse files
committed
fixing nodes
1 parent 5b7935a commit 019e04b

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

packages/models-library/src/models_library/projects.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class BaseProjectModel(BaseModel):
102102
last_change_date: datetime
103103

104104
# Pipeline of nodes (SEE projects_nodes.py)
105+
# FIXME: pedro removes this one
105106
workbench: Annotated[NodesDict, Field(description="Project's pipeline")]
106107

107108
# validators

packages/pytest-simcore/src/pytest_simcore/db_entries_mocks.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)
3030
from sqlalchemy.ext.asyncio import AsyncEngine
3131

32+
from .helpers.faker_factories import random_service_key, random_service_version
3233
from .helpers.postgres_tools import insert_and_get_row_lifespan
3334
from .helpers.postgres_users import sync_insert_and_get_user_and_secrets_lifespan
3435

@@ -92,6 +93,7 @@ async def _(
9293
project_nodes_overrides: dict[str, Any] | None = None,
9394
**project_overrides,
9495
) -> ProjectAtDB:
96+
9597
project_uuid = uuid4()
9698
with log_context(
9799
logging.INFO,
@@ -100,22 +102,22 @@ async def _(
100102
logger=_logger,
101103
) as log_ctx:
102104

103-
default_project_config = {
105+
project_values = {
104106
"uuid": f"{project_uuid}",
105107
"name": faker.name(),
106108
"type": ProjectType.STANDARD.name,
107109
"description": faker.text(),
108110
"prj_owner": user["id"],
109111
"access_rights": {"1": {"read": True, "write": True, "delete": True}},
110112
"thumbnail": "",
113+
**project_overrides,
111114
}
112-
default_project_config.update(**project_overrides)
113-
project_workbench = default_project_config.pop("workbench", {})
115+
project_workbench = project_values.pop("workbench", {})
114116

115117
async with sqlalchemy_async_engine.connect() as con, con.begin():
116118
result = await con.execute(
117119
projects.insert()
118-
.values(**default_project_config)
120+
.values(**project_values)
119121
.returning(sa.literal_column("*"))
120122
)
121123

@@ -124,25 +126,27 @@ async def _(
124126
)
125127

126128
project_nodes_repo = ProjectNodesRepo(project_uuid=project_uuid)
127-
# NOTE: currently no resources is passed until it becomes necessary
128-
default_node_config = {
129-
"required_resources": {},
130-
"key": faker.pystr(),
131-
"version": faker.pystr(),
132-
"label": faker.pystr(),
133-
}
134-
if project_nodes_overrides:
135-
default_node_config.update(project_nodes_overrides)
136-
137-
await project_nodes_repo.add(
138-
con,
139-
nodes=[
140-
ProjectNodeCreate(
141-
node_id=NodeID(node_id), **default_node_config
142-
)
143-
for node_id in inserted_project.workbench
144-
],
145-
)
129+
130+
for node_id, node_data in project_workbench.items():
131+
# NOTE: currently no resources is passed until it becomes necessary
132+
node_values = {
133+
"required_resources": {},
134+
"key": random_service_key(fake=faker),
135+
"version": random_service_version(fake=faker),
136+
"label": faker.pystr(),
137+
**node_data,
138+
}
139+
140+
if project_nodes_overrides:
141+
node_values.update(project_nodes_overrides)
142+
143+
await project_nodes_repo.add(
144+
con,
145+
nodes=[
146+
ProjectNodeCreate(node_id=NodeID(node_id), **node_values)
147+
],
148+
)
149+
146150
await con.execute(
147151
projects_to_products.insert().values(
148152
project_uuid=f"{inserted_project.uuid}",

services/director-v2/src/simcore_service_director_v2/modules/db/repositories/projects.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import sqlalchemy as sa
44
from models_library.projects import ProjectAtDB, ProjectID
5+
from models_library.projects_nodes import Node
56
from models_library.projects_nodes_io import NodeID
6-
from simcore_postgres_database.utils_projects_nodes import ProjectNodesRepo
7+
from simcore_postgres_database.utils_projects_nodes import ProjectNode, ProjectNodesRepo
78
from simcore_postgres_database.utils_repos import pass_or_acquire_connection
89

910
from ....core.errors import ProjectNotFoundError
@@ -13,6 +14,21 @@
1314
logger = logging.getLogger(__name__)
1415

1516

17+
def _project_node_to_node(project_node: ProjectNode) -> Node:
18+
"""Converts a ProjectNode from the database to a Node model for the API.
19+
20+
Handles field mapping and excludes database-specific fields that are not
21+
part of the Node model.
22+
"""
23+
# Get all ProjectNode fields except those that don't belong in Node
24+
exclude_fields = {"node_id", "required_resources", "created", "modified"}
25+
node_data = project_node.model_dump(
26+
exclude=exclude_fields, exclude_none=True, exclude_unset=True
27+
)
28+
29+
return Node.model_validate(node_data)
30+
31+
1632
class ProjectsRepository(BaseRepository):
1733
async def get_project(self, project_id: ProjectID) -> ProjectAtDB:
1834
async with self.db_engine.connect() as conn:
@@ -21,9 +37,17 @@ async def get_project(self, project_id: ProjectID) -> ProjectAtDB:
2137
sa.select(projects).where(projects.c.uuid == str(project_id))
2238
)
2339
).one_or_none()
24-
if not row:
25-
raise ProjectNotFoundError(project_id=project_id)
26-
return ProjectAtDB.model_validate(row)
40+
if not row:
41+
raise ProjectNotFoundError(project_id=project_id)
42+
43+
repo = ProjectNodesRepo(project_uuid=project_id)
44+
nodes = await repo.list(conn)
45+
46+
project_workbench = {
47+
f"{node.node_id}": _project_node_to_node(node) for node in nodes
48+
}
49+
data = {**row._asdict(), "workbench": project_workbench}
50+
return ProjectAtDB.model_validate(data)
2751

2852
async def is_node_present_in_workbench(
2953
self, project_id: ProjectID, node_uuid: NodeID

0 commit comments

Comments
 (0)