From e3db2b88c103a83c277022fe9c9ac461b2b4a195 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Tue, 31 Oct 2023 09:17:09 -0700 Subject: [PATCH] Adds default org for api calls (#3912) --- src/dispatch/main.py | 35 +++++++++---------------- src/dispatch/static/dispatch/src/api.js | 2 +- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/dispatch/main.py b/src/dispatch/main.py index cc07e4595e9f..a9d8322cfaa0 100644 --- a/src/dispatch/main.py +++ b/src/dispatch/main.py @@ -114,33 +114,24 @@ async def db_session_middleware(request: Request, call_next): path_params = get_path_params_from_request(request) # if this call is organization specific set the correct search path - organization_slug = path_params.get("organization") - if organization_slug: - request.state.organization = organization_slug - schema = f"dispatch_organization_{organization_slug}" - # validate slug exists - schema_names = inspect(engine).get_schema_names() - if schema in schema_names: - # add correct schema mapping depending on the request - schema_engine = engine.execution_options( - schema_translate_map={ - None: schema, - } - ) - else: - return JSONResponse( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - content={"detail": [{"msg": f"Unknown database schema name: {schema}"}]}, - ) - else: + organization_slug = path_params.get("organization", "default") + request.state.organization = organization_slug + schema = f"dispatch_organization_{organization_slug}" + # validate slug exists + schema_names = inspect(engine).get_schema_names() + if schema in schema_names: # add correct schema mapping depending on the request - # can we set some default here? - request.state.organization = "default" schema_engine = engine.execution_options( schema_translate_map={ - None: "dispatch_organization_default", + None: schema, } ) + else: + return JSONResponse( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={"detail": [{"msg": f"Unknown database schema name: {schema}"}]}, + ) + try: session = scoped_session(sessionmaker(bind=schema_engine), scopefunc=get_request_id) request.state.db = session() diff --git a/src/dispatch/static/dispatch/src/api.js b/src/dispatch/static/dispatch/src/api.js index 58be015771fe..183b38d979d2 100644 --- a/src/dispatch/static/dispatch/src/api.js +++ b/src/dispatch/static/dispatch/src/api.js @@ -34,7 +34,7 @@ instance.interceptors.request.use( instance.interceptors.request.use(function (config) { if (!config.url.includes("organization")) { - let currentOrganization = router.currentRoute.value.params.organization || null + let currentOrganization = router.currentRoute.value.params.organization || "default" if (currentOrganization) { config.url = `${currentOrganization}${config.url}`