Skip to content

Commit d6488ae

Browse files
committed
_add_extras_to_project_function
1 parent 3b98ab4 commit d6488ae

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

services/web/server/src/simcore_service_webserver/functions/_controller/_functions_rest.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
)
1010
from models_library.api_schemas_webserver.users import MyFunctionPermissionsGet
1111
from models_library.functions import FunctionClass, RegisteredProjectFunction
12+
from models_library.products import ProductName
1213
from models_library.rest_pagination import Page
1314
from models_library.rest_pagination_utils import paginate_data
15+
from models_library.users import UserID
1416
from pydantic import TypeAdapter
1517
from servicelib.aiohttp import status
1618
from servicelib.aiohttp.requests_validation import (
@@ -37,6 +39,32 @@
3739
routes = web.RouteTableDef()
3840

3941

42+
async def _add_extras_to_project_function(
43+
function: RegisteredProjectFunction,
44+
app: web.Application,
45+
user_id: UserID,
46+
product_name: ProductName,
47+
) -> dict:
48+
assert isinstance(function, RegisteredProjectFunction) # nosec
49+
50+
project_dict = await _projects_service.get_project_for_user(
51+
app=app,
52+
project_uuid=f"{function.project_id}",
53+
user_id=user_id,
54+
)
55+
56+
function_with_extras = function.model_dump(mode="json") | {
57+
"access_rights": await _functions_service.get_function_user_permissions(
58+
app,
59+
user_id=user_id,
60+
product_name=product_name,
61+
function_id=function.uid,
62+
),
63+
"thumbnail": project_dict.get("thumbnail", None),
64+
}
65+
return function_with_extras
66+
67+
4068
@routes.post(f"/{VTAG}/functions", name="register_function")
4169
@login_required
4270
@handle_rest_requests_exceptions
@@ -177,26 +205,16 @@ async def get_function(request: web.Request) -> web.Response:
177205
query_params.include_extras
178206
and registered_function.function_class == FunctionClass.PROJECT
179207
):
180-
assert isinstance(registered_function, RegisteredProjectFunctionGet) # nosec
181-
182-
project_dict = await _projects_service.get_project_for_user(
208+
function_with_extras = await _add_extras_to_project_function(
209+
function=registered_function,
183210
app=request.app,
184-
project_uuid=f"{registered_function.project_id}",
185211
user_id=req_ctx.user_id,
212+
product_name=req_ctx.product_name,
186213
)
187214

188215
return envelope_json_response(
189216
TypeAdapter(RegisteredProjectFunctionGet).validate_python(
190-
registered_function.model_dump(mode="json")
191-
| {
192-
"access_rights": await _functions_service.get_function_user_permissions(
193-
request.app,
194-
user_id=req_ctx.user_id,
195-
product_name=req_ctx.product_name,
196-
function_id=function_id,
197-
),
198-
"thumbnail": project_dict.get("thumbnail", None),
199-
}
217+
function_with_extras
200218
)
201219
)
202220

@@ -231,6 +249,20 @@ async def update_function(request: web.Request) -> web.Response:
231249
function=function_update,
232250
)
233251

252+
if updated_function.function_class == FunctionClass.PROJECT:
253+
function_with_extras = await _add_extras_to_project_function(
254+
function=updated_function,
255+
app=request.app,
256+
user_id=req_ctx.user_id,
257+
product_name=req_ctx.product_name,
258+
)
259+
260+
return envelope_json_response(
261+
TypeAdapter(RegisteredProjectFunctionGet).validate_python(
262+
function_with_extras
263+
)
264+
)
265+
234266
return envelope_json_response(
235267
TypeAdapter(RegisteredFunctionGet).validate_python(
236268
updated_function.model_dump(mode="json")

0 commit comments

Comments
 (0)