|
9 | 9 | ) |
10 | 10 | from models_library.api_schemas_webserver.users import MyFunctionPermissionsGet |
11 | 11 | from models_library.functions import FunctionClass, RegisteredProjectFunction |
| 12 | +from models_library.products import ProductName |
12 | 13 | from models_library.rest_pagination import Page |
13 | 14 | from models_library.rest_pagination_utils import paginate_data |
| 15 | +from models_library.users import UserID |
14 | 16 | from pydantic import TypeAdapter |
15 | 17 | from servicelib.aiohttp import status |
16 | 18 | from servicelib.aiohttp.requests_validation import ( |
|
37 | 39 | routes = web.RouteTableDef() |
38 | 40 |
|
39 | 41 |
|
| 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 | + |
40 | 68 | @routes.post(f"/{VTAG}/functions", name="register_function") |
41 | 69 | @login_required |
42 | 70 | @handle_rest_requests_exceptions |
@@ -177,26 +205,16 @@ async def get_function(request: web.Request) -> web.Response: |
177 | 205 | query_params.include_extras |
178 | 206 | and registered_function.function_class == FunctionClass.PROJECT |
179 | 207 | ): |
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, |
183 | 210 | app=request.app, |
184 | | - project_uuid=f"{registered_function.project_id}", |
185 | 211 | user_id=req_ctx.user_id, |
| 212 | + product_name=req_ctx.product_name, |
186 | 213 | ) |
187 | 214 |
|
188 | 215 | return envelope_json_response( |
189 | 216 | 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 |
200 | 218 | ) |
201 | 219 | ) |
202 | 220 |
|
@@ -231,6 +249,20 @@ async def update_function(request: web.Request) -> web.Response: |
231 | 249 | function=function_update, |
232 | 250 | ) |
233 | 251 |
|
| 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 | + |
234 | 266 | return envelope_json_response( |
235 | 267 | TypeAdapter(RegisteredFunctionGet).validate_python( |
236 | 268 | updated_function.model_dump(mode="json") |
|
0 commit comments