Skip to content

Commit 258a979

Browse files
author
Andrei Neagu
committed
fixed tests
1 parent fb9ff38 commit 258a979

File tree

13 files changed

+114
-54
lines changed

13 files changed

+114
-54
lines changed

services/web/server/src/simcore_service_webserver/projects/_controller/_rest_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
from models_library.rest_pagination_utils import paginate_data
55
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
66
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
7+
from yarl import URL
78

89
from .. import _permalink_service
910
from .._crud_api_read import _paralell_update
1011
from ..models import ProjectDict
1112

1213

1314
async def aggregate_data_to_projects_from_request(
14-
request: web.Request,
15+
app: web.Application,
16+
url: URL,
17+
headers: dict[str, str],
1518
projects: list[ProjectDict],
1619
) -> list[ProjectDict]:
1720

1821
update_permalink_per_project = [
1922
# permalink
20-
_permalink_service.aggregate_permalink_in_project(request, project=prj)
23+
_permalink_service.aggregate_permalink_in_project(
24+
app, url, headers, project=prj
25+
)
2126
for prj in projects
2227
]
2328

services/web/server/src/simcore_service_webserver/projects/_controller/nodes_rest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ async def _stop_dynamic_service_task(
322322
return web.json_response(status=status.HTTP_204_NO_CONTENT)
323323

324324

325-
TaskRegistry.register(_stop_dynamic_service_task)
325+
def register_stop_dynamic_service_task(app: web.Application) -> None:
326+
TaskRegistry.register_partial(_stop_dynamic_service_task, app=app)
326327

327328

328329
@routes.post(
@@ -352,7 +353,6 @@ async def stop_node(request: web.Request) -> web.Response:
352353
_stop_dynamic_service_task.__name__,
353354
task_context=jsonable_encoder(req_ctx),
354355
# task arguments from here on ---
355-
app=request.app,
356356
dynamic_service_stop=DynamicServiceStop(
357357
user_id=req_ctx.user_id,
358358
project_id=path_params.project_id,

services/web/server/src/simcore_service_webserver/projects/_controller/projects_rest.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ async def create_project(request: web.Request):
101101
fire_and_forget=True,
102102
task_context=jsonable_encoder(req_ctx),
103103
# arguments
104-
request=request,
104+
url=request.url,
105+
headers=dict(request.headers),
105106
new_project_was_hidden_before_data_was_copied=query_params.hidden,
106107
from_study=query_params.from_study,
107108
as_template=query_params.as_template,
@@ -158,7 +159,7 @@ async def list_projects(request: web.Request):
158159
)
159160

160161
projects = await _rest_utils.aggregate_data_to_projects_from_request(
161-
request, projects
162+
request.app, request.url, dict(request.headers), projects
162163
)
163164

164165
return _rest_utils.create_page_response(
@@ -197,7 +198,7 @@ async def list_projects_full_search(request: web.Request):
197198
)
198199

199200
projects = await _rest_utils.aggregate_data_to_projects_from_request(
200-
request, projects
201+
request.app, request.url, dict(request.headers), projects
201202
)
202203

203204
return _rest_utils.create_page_response(
@@ -247,7 +248,9 @@ async def get_active_project(request: web.Request) -> web.Response:
247248
)
248249

249250
# updates project's permalink field
250-
await update_or_pop_permalink_in_project(request, project)
251+
await update_or_pop_permalink_in_project(
252+
request.app, request.url, dict(request.headers), project
253+
)
251254

252255
data = ProjectGet.from_domain_model(project).data(exclude_unset=True)
253256

@@ -280,7 +283,9 @@ async def get_project(request: web.Request):
280283
)
281284

282285
# Adds permalink
283-
await update_or_pop_permalink_in_project(request, project)
286+
await update_or_pop_permalink_in_project(
287+
request.app, request.url, dict(request.headers), project
288+
)
284289

285290
data = ProjectGet.from_domain_model(project).data(exclude_unset=True)
286291
return envelope_json_response(data)
@@ -419,7 +424,8 @@ async def clone_project(request: web.Request):
419424
fire_and_forget=True,
420425
task_context=jsonable_encoder(req_ctx),
421426
# arguments
422-
request=request,
427+
url=request.url,
428+
headers=dict(request.headers),
423429
new_project_was_hidden_before_data_was_copied=False,
424430
from_study=path_params.project_id,
425431
as_template=False,

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ProjectNodeCreate,
2828
)
2929
from simcore_postgres_database.webserver_models import ProjectType as ProjectTypeDB
30+
from yarl import URL
3031

3132
from ..application_settings import get_application_settings
3233
from ..catalog import catalog_service
@@ -249,7 +250,9 @@ async def _compose_project_data(
249250
async def create_project( # pylint: disable=too-many-arguments,too-many-branches,too-many-statements # noqa: C901, PLR0913
250251
progress: TaskProgress,
251252
*,
252-
request: web.Request,
253+
app: web.Application,
254+
url: URL,
255+
headers: dict[str, str],
253256
new_project_was_hidden_before_data_was_copied: bool,
254257
from_study: ProjectID | None,
255258
as_template: bool,
@@ -281,7 +284,6 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
281284
web.HTTPUnauthorized:
282285
283286
"""
284-
assert request.app # nosec
285287
_logger.info(
286288
"create_project for '%s' with %s %s %s",
287289
f"{user_id=}",
@@ -290,7 +292,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
290292
f"{from_study=}",
291293
)
292294

293-
_projects_repository_legacy = ProjectDBAPI.get_from_app_context(request.app)
295+
_projects_repository_legacy = ProjectDBAPI.get_from_app_context(app)
294296

295297
new_project: ProjectDict = {}
296298
copy_file_coro = None
@@ -303,7 +305,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
303305
if predefined_project:
304306
if workspace_id := predefined_project.get("workspaceId", None):
305307
await check_user_workspace_access(
306-
request.app,
308+
app,
307309
user_id=user_id,
308310
workspace_id=workspace_id,
309311
product_name=product_name,
@@ -312,7 +314,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
312314
if folder_id := predefined_project.get("folderId", None):
313315
# Check user has access to folder
314316
await folders_folders_repository.get_for_user_or_workspace(
315-
request.app,
317+
app,
316318
folder_id=folder_id,
317319
product_name=product_name,
318320
user_id=user_id if workspace_id is None else None,
@@ -328,7 +330,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
328330
project_node_coro,
329331
copy_file_coro,
330332
) = await _prepare_project_copy(
331-
request.app,
333+
app,
332334
user_id=user_id,
333335
product_name=product_name,
334336
src_project_uuid=from_study,
@@ -342,7 +344,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
342344
# 1.2 does project belong to some folder?
343345
workspace_id = new_project["workspaceId"]
344346
prj_to_folder_db = await _folders_repository.get_project_to_folder(
345-
request.app,
347+
app,
346348
project_id=from_study,
347349
private_workspace_user_id_or_none=(
348350
user_id if workspace_id is None else None
@@ -361,7 +363,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
361363
if predefined_project:
362364
# 2. overrides with optional body and re-validate
363365
new_project, project_nodes = await _compose_project_data(
364-
request.app,
366+
app,
365367
user_id=user_id,
366368
new_project=new_project,
367369
predefined_project=predefined_project,
@@ -378,7 +380,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
378380
)
379381
# add parent linking if needed
380382
await set_project_ancestors(
381-
request.app,
383+
app,
382384
user_id=user_id,
383385
project_uuid=new_project["uuid"],
384386
parent_project_uuid=parent_project_uuid,
@@ -389,7 +391,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
389391
# 3.2 move project to proper folder
390392
if folder_id:
391393
await _folders_repository.insert_project_to_folder(
392-
request.app,
394+
app,
393395
project_id=new_project["uuid"],
394396
folder_id=folder_id,
395397
private_workspace_user_id_or_none=(
@@ -405,20 +407,20 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
405407
# 5. unhide the project if needed since it is now complete
406408
if not new_project_was_hidden_before_data_was_copied:
407409
await _projects_repository.patch_project(
408-
request.app,
410+
app,
409411
project_uuid=new_project["uuid"],
410412
new_partial_project_data={"hidden": False},
411413
)
412414

413415
# update the network information in director-v2
414416
await dynamic_scheduler_service.update_projects_networks(
415-
request.app, project_id=ProjectID(new_project["uuid"])
417+
app, project_id=ProjectID(new_project["uuid"])
416418
)
417419
await progress.update()
418420

419421
# This is a new project and every new graph needs to be reflected in the pipeline tables
420422
await director_v2_service.create_or_update_pipeline(
421-
request.app,
423+
app,
422424
user_id,
423425
new_project["uuid"],
424426
product_name,
@@ -433,12 +435,12 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
433435
user_id=user_id,
434436
project=new_project,
435437
is_template=as_template,
436-
app=request.app,
438+
app=app,
437439
)
438440
await progress.update()
439441

440442
# Adds permalink
441-
await update_or_pop_permalink_in_project(request, new_project)
443+
await update_or_pop_permalink_in_project(app, url, headers, new_project)
442444

443445
# Adds folderId
444446
user_specific_project_data_db = (
@@ -454,7 +456,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
454456
# Overwrite project access rights
455457
if workspace_id:
456458
workspace: UserWorkspaceWithAccessRights = await get_user_workspace(
457-
request.app,
459+
app,
458460
user_id=user_id,
459461
workspace_id=workspace_id,
460462
product_name=product_name,
@@ -497,7 +499,7 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
497499
except (ParentProjectNotFoundError, ParentNodeNotFoundError) as exc:
498500
if project_uuid := new_project.get("uuid"):
499501
await _projects_service.submit_delete_project_task(
500-
app=request.app,
502+
app=app,
501503
project_uuid=project_uuid,
502504
user_id=user_id,
503505
simcore_user_agent=simcore_user_agent,
@@ -511,12 +513,13 @@ async def create_project( # pylint: disable=too-many-arguments,too-many-branche
511513
)
512514
if project_uuid := new_project.get("uuid"):
513515
await _projects_service.submit_delete_project_task(
514-
app=request.app,
516+
app=app,
515517
project_uuid=project_uuid,
516518
user_id=user_id,
517519
simcore_user_agent=simcore_user_agent,
518520
)
519521
raise
520522

521523

522-
TaskRegistry.register(create_project)
524+
def register_create_project_task(app: web.Application) -> None:
525+
TaskRegistry.register_partial(create_project, app=app)

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from aiohttp import web
66
from models_library.api_schemas_webserver.permalinks import ProjectPermalink
77
from models_library.projects import ProjectID
8+
from yarl import URL
89

910
from .exceptions import PermalinkFactoryError, PermalinkNotAllowedError
1011
from .models import ProjectDict
@@ -15,9 +16,12 @@
1516

1617
class CreateLinkCoroutine(Protocol):
1718
async def __call__(
18-
self, request: web.Request, project_uuid: ProjectID
19-
) -> ProjectPermalink:
20-
...
19+
self,
20+
app: web.Application,
21+
url: URL,
22+
headers: dict[str, str],
23+
project_uuid: ProjectID,
24+
) -> ProjectPermalink: ...
2125

2226

2327
def register_factory(app: web.Application, factory_coro: CreateLinkCoroutine):
@@ -39,13 +43,13 @@ def _get_factory(app: web.Application) -> CreateLinkCoroutine:
3943

4044

4145
async def _create_permalink(
42-
request: web.Request, project_uuid: ProjectID
46+
app: web.Application, url: URL, headers: dict[str, str], project_uuid: ProjectID
4347
) -> ProjectPermalink:
44-
create_coro: CreateLinkCoroutine = _get_factory(request.app)
48+
create_coro: CreateLinkCoroutine = _get_factory(app)
4549

4650
try:
4751
permalink: ProjectPermalink = await asyncio.wait_for(
48-
create_coro(request=request, project_uuid=project_uuid),
52+
create_coro(app, url, headers, project_uuid),
4953
timeout=_PERMALINK_CREATE_TIMEOUT_S,
5054
)
5155
return permalink
@@ -55,7 +59,7 @@ async def _create_permalink(
5559

5660

5761
async def update_or_pop_permalink_in_project(
58-
request: web.Request, project: ProjectDict
62+
app: web.Application, url: URL, headers: dict[str, str], project: ProjectDict
5963
) -> ProjectPermalink | None:
6064
"""Updates permalink entry in project
6165
@@ -64,7 +68,9 @@ async def update_or_pop_permalink_in_project(
6468
If fails, it pops it from project (so it is not set in the pydantic model. SEE ProjectGet.permalink)
6569
"""
6670
try:
67-
permalink = await _create_permalink(request, project_uuid=project["uuid"])
71+
permalink = await _create_permalink(
72+
app, url, headers, project_uuid=project["uuid"]
73+
)
6874

6975
assert permalink # nosec
7076
project["permalink"] = permalink
@@ -78,12 +84,12 @@ async def update_or_pop_permalink_in_project(
7884

7985

8086
async def aggregate_permalink_in_project(
81-
request: web.Request, project: ProjectDict
87+
app: web.Application, url: URL, headers: dict[str, str], project: ProjectDict
8288
) -> ProjectDict:
8389
"""
8490
Adapter to use in parallel aggregation of fields in a project dataset
8591
"""
86-
await update_or_pop_permalink_in_project(request, project)
92+
await update_or_pop_permalink_in_project(app, url, headers, project)
8793
return project
8894

8995

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
wallets_rest,
3030
workspaces_rest,
3131
)
32+
from ._controller.nodes_rest import register_stop_dynamic_service_task
33+
from ._crud_api_create import register_create_project_task
3234
from ._projects_repository_legacy import setup_projects_db
3335
from ._security_service import setup_projects_access
3436

@@ -75,4 +77,7 @@ def setup_projects(app: web.Application) -> bool:
7577
app.router.add_routes(workspaces_rest.routes)
7678
app.router.add_routes(trash_rest.routes)
7779

80+
register_create_project_task(app)
81+
register_stop_dynamic_service_task(app)
82+
7883
return True

0 commit comments

Comments
 (0)