Skip to content

Commit 2ac7623

Browse files
committed
refactor to reduce complexity (sonarcloud)
1 parent a421591 commit 2ac7623

File tree

1 file changed

+60
-35
lines changed

1 file changed

+60
-35
lines changed

services/web/server/src/simcore_service_webserver/folders/_folders_repository.py

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,40 +95,17 @@ async def create(
9595
return FolderDB.model_validate(row)
9696

9797

98-
async def list_( # pylint: disable=too-many-arguments,too-many-branches
99-
app: web.Application,
100-
connection: AsyncConnection | None = None,
101-
*,
98+
def _create_private_workspace_query(
10299
product_name: ProductName,
103100
user_id: UserID,
104-
# hierarchy filters
105-
folder_query: FolderQuery,
106-
workspace_query: WorkspaceQuery,
107-
# attribute filters
108-
filter_trashed: bool | None,
109-
filter_by_text: str | None,
110-
# pagination
111-
offset: NonNegativeInt,
112-
limit: int,
113-
# order
114-
order_by: OrderBy,
115-
) -> tuple[int, list[UserFolderAccessRightsDB]]:
116-
"""
117-
folder_query - Used to filter in which folder we want to list folders.
118-
trashed - If set to true, it returns folders **explicitly** trashed, if false then non-trashed folders.
119-
"""
120-
121-
workspace_access_rights_subquery = create_my_workspace_access_rights_subquery(
122-
user_id=user_id
123-
)
124-
125-
if workspace_query.workspace_scope is not WorkspaceScope.SHARED:
126-
assert workspace_query.workspace_scope in ( # nosec
101+
workspace_scope: WorkspaceScope,
102+
):
103+
if workspace_scope is not WorkspaceScope.SHARED:
104+
assert workspace_scope in ( # nosec
127105
WorkspaceScope.PRIVATE,
128106
WorkspaceScope.ALL,
129107
)
130-
131-
private_workspace_query = (
108+
return (
132109
select(
133110
*_SELECTION_ARGS,
134111
func.json_build_object(
@@ -146,15 +123,25 @@ async def list_( # pylint: disable=too-many-arguments,too-many-branches
146123
& (folders_v2.c.user_id == user_id)
147124
)
148125
)
149-
else:
150-
private_workspace_query = None
126+
return None
151127

152-
if workspace_query.workspace_scope is not WorkspaceScope.PRIVATE:
153-
assert workspace_query.workspace_scope in ( # nosec
128+
129+
def _create_shared_workspace_query(
130+
product_name: ProductName,
131+
user_id: UserID,
132+
workspace_scope: WorkspaceScope,
133+
workspace_id: WorkspaceID | None,
134+
):
135+
if workspace_scope is not WorkspaceScope.PRIVATE:
136+
assert workspace_scope in ( # nosec
154137
WorkspaceScope.SHARED,
155138
WorkspaceScope.ALL,
156139
)
157140

141+
workspace_access_rights_subquery = create_my_workspace_access_rights_subquery(
142+
user_id=user_id
143+
)
144+
158145
shared_workspace_query = (
159146
select(
160147
*_SELECTION_ARGS, workspace_access_rights_subquery.c.my_access_rights
@@ -172,14 +159,52 @@ async def list_( # pylint: disable=too-many-arguments,too-many-branches
172159
)
173160
)
174161

175-
if workspace_query.workspace_scope == WorkspaceScope.SHARED:
162+
if workspace_scope == WorkspaceScope.SHARED:
176163
shared_workspace_query = shared_workspace_query.where(
177-
folders_v2.c.workspace_id == workspace_query.workspace_id
164+
folders_v2.c.workspace_id == workspace_id
178165
)
179166

180167
else:
181168
shared_workspace_query = None
182169

170+
return shared_workspace_query
171+
172+
173+
async def list_( # pylint: disable=too-many-arguments,too-many-branches
174+
app: web.Application,
175+
connection: AsyncConnection | None = None,
176+
*,
177+
product_name: ProductName,
178+
user_id: UserID,
179+
# hierarchy filters
180+
folder_query: FolderQuery,
181+
workspace_query: WorkspaceQuery,
182+
# attribute filters
183+
filter_trashed: bool | None,
184+
filter_by_text: str | None,
185+
# pagination
186+
offset: NonNegativeInt,
187+
limit: int,
188+
# order
189+
order_by: OrderBy,
190+
) -> tuple[int, list[UserFolderAccessRightsDB]]:
191+
"""
192+
folder_query - Used to filter in which folder we want to list folders.
193+
trashed - If set to true, it returns folders **explicitly** trashed, if false then non-trashed folders.
194+
"""
195+
196+
private_workspace_query = _create_private_workspace_query(
197+
workspace_scope=workspace_query.workspace_scope,
198+
product_name=product_name,
199+
user_id=user_id,
200+
)
201+
shared_workspace_query = _create_shared_workspace_query(
202+
workspace_scope=workspace_query.workspace_scope,
203+
product_name=product_name,
204+
user_id=user_id,
205+
workspace_id=workspace_query.workspace_id,
206+
)
207+
183208
attributes_filters: list[ColumnElement] = []
184209

185210
if filter_trashed is not None:

0 commit comments

Comments
 (0)