2323from ...login .decorators import login_required
2424from ...models import AuthenticatedRequestContext
2525from ...projects import _projects_service
26+ from ...projects .models import ProjectDBGet
2627from ...security .decorators import permission_required
2728from ...utils_aiohttp import envelope_json_response
2829from .. import _functions_service
@@ -88,7 +89,10 @@ async def list_functions(request: web.Request) -> web.Response:
8889 pagination_offset = query_params .offset ,
8990 )
9091
91- chunk = []
92+ chunk : list [RegisteredFunctionGet ] = []
93+ projects_map : dict [str , ProjectDBGet ] = (
94+ {}
95+ ) # ProjectDBGet has to be renamed at some point!
9296
9397 if query_params .include_extras :
9498 project_ids = []
@@ -101,40 +105,34 @@ async def list_functions(request: web.Request) -> web.Response:
101105 request .app ,
102106 project_uuids = project_ids ,
103107 )
104- projects_map = {f"{ p .uuid } " : p for p in projects }
105-
106- for function in functions :
107- if (
108- query_params .include_extras
109- and function .function_class == FunctionClass .PROJECT
110- ):
111- assert isinstance (function , RegisteredProjectFunction ) # nosec
112- project = projects_map .get (f"{ function .project_id } " )
113- if project :
114- chunk .append (
115- TypeAdapter (RegisteredProjectFunctionGet ).validate_python (
116- function .model_dump (mode = "json" )
117- | {
118- "thumbnail" : project .thumbnail ,
119- "template_id" : project .id ,
120- }
121- )
122- )
123- else :
108+ for project in projects :
109+ projects_map [f"{ project .uuid } " ] = project
110+
111+ for function in functions :
112+ if (
113+ query_params .include_extras
114+ and function .function_class == FunctionClass .PROJECT
115+ ):
116+ assert isinstance (function , RegisteredProjectFunction ) # nosec
117+ project = projects_map .get (f"{ function .project_id } " )
118+ if project :
124119 chunk .append (
125- TypeAdapter (RegisteredFunctionGet ).validate_python (
120+ TypeAdapter (RegisteredProjectFunctionGet ).validate_python (
126121 function .model_dump (mode = "json" )
122+ | {
123+ "thumbnail" : (
124+ f"{ project .thumbnail } " if project .thumbnail else None
125+ ),
126+ "template_id" : project .id ,
127+ }
127128 )
128129 )
129- else :
130- chunk .extend (
131- [
130+ else :
131+ chunk .append (
132132 TypeAdapter (RegisteredFunctionGet ).validate_python (
133133 function .model_dump (mode = "json" )
134134 )
135- for function in functions
136- ]
137- )
135+ )
138136
139137 page = Page [RegisteredFunctionGet ].model_validate (
140138 paginate_data (
@@ -188,7 +186,7 @@ async def get_function(request: web.Request) -> web.Response:
188186 registered_function .model_dump (mode = "json" )
189187 | {
190188 "thumbnail" : project_dict .get ("thumbnail" , None ),
191- "template_id" : project_dict .get ("project_id " , None ),
189+ "template_id" : project_dict .get ("id " , None ),
192190 }
193191 )
194192 )
0 commit comments