Skip to content

Commit 14dee50

Browse files
committed
handlers return item
1 parent 4ffcd5f commit 14dee50

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

services/web/server/src/simcore_service_webserver/projects/_crud_api_read.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from simcore_postgres_database.models.projects import ProjectType
1717
from simcore_postgres_database.webserver_models import ProjectType as ProjectTypeDB
1818
from simcore_service_webserver.projects._projects_db import (
19-
get_trashed_by_primary_gid_from_project,
19+
batch_get_trashed_by_primary_gid,
2020
)
2121

2222
from ..catalog.client import get_services_for_user_in_product
@@ -49,16 +49,18 @@ async def _update_project_dict(
4949
return project
5050

5151

52-
async def _update_list_of_project_dict(
52+
async def _batch_update_list_of_project_dict(
5353
app: web.Application, list_of_project_dict: list[ProjectDict]
5454
) -> list[ProjectDict]:
5555

5656
# updating `trashed_by_primary_gid`
57-
updates_values = await get_trashed_by_primary_gid_from_project(
57+
trashed_by_primary_gid_values = await batch_get_trashed_by_primary_gid(
5858
app, projects_uuids=[ProjectID(p["uuid"]) for p in list_of_project_dict]
5959
)
6060

61-
for project_dict, value in zip(list_of_project_dict, updates_values, strict=True):
61+
for project_dict, value in zip(
62+
list_of_project_dict, trashed_by_primary_gid_values, strict=True
63+
):
6264
project_dict.update(trashed_by_primary_gid=value)
6365

6466
return list_of_project_dict
@@ -140,7 +142,7 @@ async def list_projects( # pylint: disable=too-many-arguments
140142
order_by=order_by,
141143
)
142144

143-
db_projects = await _update_list_of_project_dict(app, db_projects)
145+
db_projects = await _batch_update_list_of_project_dict(app, db_projects)
144146

145147
projects: list[ProjectDict] = await logged_gather(
146148
*(
@@ -195,7 +197,7 @@ async def list_projects_full_depth(
195197
order_by=order_by,
196198
)
197199

198-
db_projects = await _update_list_of_project_dict(request.app, db_projects)
200+
db_projects = await _batch_update_list_of_project_dict(request.app, db_projects)
199201

200202
projects: list[ProjectDict] = await logged_gather(
201203
*(

services/web/server/src/simcore_service_webserver/projects/_crud_handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ async def get_active_project(request: web.Request) -> web.Response:
302302
project_uuid=user_active_projects[0],
303303
user_id=req_ctx.user_id,
304304
include_state=True,
305+
include_trashed_by_primary_gid=True,
305306
)
306307

307308
# updates project's permalink field
@@ -342,6 +343,7 @@ async def get_project(request: web.Request):
342343
project_uuid=f"{path_params.project_id}",
343344
user_id=req_ctx.user_id,
344345
include_state=True,
346+
include_trashed_by_primary_gid=True,
345347
)
346348
if not await project_uses_available_services(project, user_available_services):
347349
unavilable_services = get_project_unavailable_services(

services/web/server/src/simcore_service_webserver/projects/_projects_db.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,33 @@ async def patch_project(
5050
return ProjectDB.model_validate(row)
5151

5252

53-
async def get_trashed_by_primary_gid_from_project(
53+
async def get_trashed_by_primary_gid(
54+
app: web.Application,
55+
connection: AsyncConnection | None = None,
56+
*,
57+
projects_uuid: ProjectID,
58+
) -> GroupID | None:
59+
async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn:
60+
query = (
61+
sa.select(
62+
users.c.primary_gid.label("trashed_by_primary_gid"),
63+
)
64+
.select_from(projects.outerjoin(users, projects.c.trashed_by == users.c.id))
65+
.where(projects.c.uuid == projects_uuid)
66+
)
67+
result = await conn.stream(query)
68+
row = await result.first()
69+
return row.trashed_by_primary_gid if row else None
70+
71+
72+
async def batch_get_trashed_by_primary_gid(
5473
app: web.Application,
5574
connection: AsyncConnection | None = None,
5675
*,
5776
projects_uuids: list[ProjectID],
5877
) -> list[GroupID | None]:
5978
"""
60-
Returns trashed_by as GroupID instead of UserID as is in the database
79+
Batch version of get_trashed_by_primary_gid preserving ORDER of projects_uuids
6180
"""
6281
async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn:
6382
query = (

services/web/server/src/simcore_service_webserver/projects/projects_service.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,13 @@ async def get_project_for_user(
216216
user_id, project, project_type is ProjectType.TEMPLATE, app
217217
)
218218

219+
# adds `trashed_by_primary_gid`
219220
if include_trashed_by_primary_gid and project.get("trashed_by") is not None:
220-
_values = await _projects_db.get_trashed_by_primary_gid_from_project(
221-
app, projects_uuids=[project["uuid"]]
221+
project.update(
222+
trashed_by_primary_gid=await _projects_db.get_trashed_by_primary_gid(
223+
app, projects_uuid=project["uuid"]
224+
)
222225
)
223-
assert len(_values) == 1
224-
project.update(trashed_by_primary_gid=_values[0])
225226

226227
if project["workspaceId"] is not None:
227228
workspace: UserWorkspaceWithAccessRights = (

0 commit comments

Comments
 (0)