From 24696f54c2644bd7b414b2264337d6f48c16a284 Mon Sep 17 00:00:00 2001 From: Pedro Crespo-Valero <32402063+pcrespov@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:02:18 +0100 Subject: [PATCH] one place for groups --- .../groups/{_db.py => _groups_db.py} | 0 .../{_handlers.py => _groups_handlers.py} | 0 .../simcore_service_webserver/groups/api.py | 40 ++++++++++--------- .../groups/plugin.py | 4 +- .../tests/unit/with_dbs/01/test_groups.py | 2 +- 5 files changed, 25 insertions(+), 21 deletions(-) rename services/web/server/src/simcore_service_webserver/groups/{_db.py => _groups_db.py} (100%) rename services/web/server/src/simcore_service_webserver/groups/{_handlers.py => _groups_handlers.py} (100%) diff --git a/services/web/server/src/simcore_service_webserver/groups/_db.py b/services/web/server/src/simcore_service_webserver/groups/_groups_db.py similarity index 100% rename from services/web/server/src/simcore_service_webserver/groups/_db.py rename to services/web/server/src/simcore_service_webserver/groups/_groups_db.py diff --git a/services/web/server/src/simcore_service_webserver/groups/_handlers.py b/services/web/server/src/simcore_service_webserver/groups/_groups_handlers.py similarity index 100% rename from services/web/server/src/simcore_service_webserver/groups/_handlers.py rename to services/web/server/src/simcore_service_webserver/groups/_groups_handlers.py diff --git a/services/web/server/src/simcore_service_webserver/groups/api.py b/services/web/server/src/simcore_service_webserver/groups/api.py index 503eee73839..ae758926a35 100644 --- a/services/web/server/src/simcore_service_webserver/groups/api.py +++ b/services/web/server/src/simcore_service_webserver/groups/api.py @@ -8,7 +8,7 @@ from ..db.plugin import get_database_engine from ..users.api import get_user -from . import _db +from . import _groups_db from ._utils import AccessRightsDict from .exceptions import GroupsError @@ -22,7 +22,9 @@ async def list_user_groups_with_read_access( # NOTE: Careful! It seems we are filtering out groups, such as Product Groups, # because they do not have read access. I believe this was done because the frontend did not want to display them. async with get_database_engine(app).acquire() as conn: - return await _db.get_all_user_groups_with_read_access(conn, user_id=user_id) + return await _groups_db.get_all_user_groups_with_read_access( + conn, user_id=user_id + ) async def list_all_user_groups(app: web.Application, user_id: UserID) -> list[Group]: @@ -30,7 +32,7 @@ async def list_all_user_groups(app: web.Application, user_id: UserID) -> list[Gr Return all user groups """ async with get_database_engine(app).acquire() as conn: - groups_db = await _db.get_all_user_groups(conn, user_id=user_id) + groups_db = await _groups_db.get_all_user_groups(conn, user_id=user_id) return [Group.model_construct(**group.model_dump()) for group in groups_db] @@ -45,7 +47,7 @@ async def get_user_group( raises UserInsufficientRightsError """ async with get_database_engine(app).acquire() as conn: - return await _db.get_user_group(conn, user_id=user_id, gid=gid) + return await _groups_db.get_user_group(conn, user_id=user_id, gid=gid) async def get_product_group_for_user( @@ -56,7 +58,7 @@ async def get_product_group_for_user( raises GroupNotFoundError """ async with get_database_engine(app).acquire() as conn: - return await _db.get_product_group_for_user( + return await _groups_db.get_product_group_for_user( conn, user_id=user_id, product_gid=product_gid ) @@ -65,7 +67,9 @@ async def create_user_group( app: web.Application, user_id: UserID, new_group: dict ) -> dict[str, Any]: async with get_database_engine(app).acquire() as conn: - return await _db.create_user_group(conn, user_id=user_id, new_group=new_group) + return await _groups_db.create_user_group( + conn, user_id=user_id, new_group=new_group + ) async def update_user_group( @@ -75,7 +79,7 @@ async def update_user_group( new_group_values: dict[str, str], ) -> dict[str, str]: async with get_database_engine(app).acquire() as conn: - return await _db.update_user_group( + return await _groups_db.update_user_group( conn, user_id=user_id, gid=gid, new_group_values=new_group_values ) @@ -84,28 +88,28 @@ async def delete_user_group( app: web.Application, user_id: UserID, gid: GroupID ) -> None: async with get_database_engine(app).acquire() as conn: - return await _db.delete_user_group(conn, user_id=user_id, gid=gid) + return await _groups_db.delete_user_group(conn, user_id=user_id, gid=gid) async def list_users_in_group( app: web.Application, user_id: UserID, gid: GroupID ) -> list[dict[str, str]]: async with get_database_engine(app).acquire() as conn: - return await _db.list_users_in_group(conn, user_id=user_id, gid=gid) + return await _groups_db.list_users_in_group(conn, user_id=user_id, gid=gid) async def auto_add_user_to_groups(app: web.Application, user_id: UserID) -> None: user: dict = await get_user(app, user_id) async with get_database_engine(app).acquire() as conn: - return await _db.auto_add_user_to_groups(conn, user=user) + return await _groups_db.auto_add_user_to_groups(conn, user=user) async def auto_add_user_to_product_group( app: web.Application, user_id: UserID, product_name: str ) -> GroupID: async with get_database_engine(app).acquire() as conn: - return await _db.auto_add_user_to_product_group( + return await _groups_db.auto_add_user_to_product_group( conn, user_id=user_id, product_name=product_name ) @@ -114,7 +118,7 @@ async def is_user_by_email_in_group( app: web.Application, user_email: LowerCaseEmailStr, group_id: GroupID ) -> bool: async with get_database_engine(app).acquire() as conn: - return await _db.is_user_by_email_in_group( + return await _groups_db.is_user_by_email_in_group( conn, email=user_email, group_id=group_id, @@ -143,14 +147,14 @@ async def add_user_in_group( async with get_database_engine(app).acquire() as conn: if new_user_email: - user: RowProxy = await _db.get_user_from_email(conn, new_user_email) + user: RowProxy = await _groups_db.get_user_from_email(conn, new_user_email) new_user_id = user["id"] if not new_user_id: msg = "Missing new user in arguments" raise GroupsError(msg=msg) - return await _db.add_new_user_in_group( + return await _groups_db.add_new_user_in_group( conn, user_id=user_id, gid=gid, @@ -163,7 +167,7 @@ async def get_user_in_group( app: web.Application, user_id: UserID, gid: GroupID, the_user_id_in_group: int ) -> dict[str, str]: async with get_database_engine(app).acquire() as conn: - return await _db.get_user_in_group( + return await _groups_db.get_user_in_group( conn, user_id=user_id, gid=gid, the_user_id_in_group=the_user_id_in_group ) @@ -176,7 +180,7 @@ async def update_user_in_group( access_rights: dict, ) -> dict[str, str]: async with get_database_engine(app).acquire() as conn: - return await _db.update_user_in_group( + return await _groups_db.update_user_in_group( conn, user_id=user_id, gid=gid, @@ -189,14 +193,14 @@ async def delete_user_in_group( app: web.Application, user_id: UserID, gid: GroupID, the_user_id_in_group: int ) -> None: async with get_database_engine(app).acquire() as conn: - return await _db.delete_user_in_group( + return await _groups_db.delete_user_in_group( conn, user_id=user_id, gid=gid, the_user_id_in_group=the_user_id_in_group ) async def get_group_from_gid(app: web.Application, gid: GroupID) -> Group | None: async with get_database_engine(app).acquire() as conn: - group_db = await _db.get_group_from_gid(conn, gid=gid) + group_db = await _groups_db.get_group_from_gid(conn, gid=gid) if group_db: return Group.model_construct(**group_db.model_dump()) diff --git a/services/web/server/src/simcore_service_webserver/groups/plugin.py b/services/web/server/src/simcore_service_webserver/groups/plugin.py index 663671848c0..39e80c9f96d 100644 --- a/services/web/server/src/simcore_service_webserver/groups/plugin.py +++ b/services/web/server/src/simcore_service_webserver/groups/plugin.py @@ -5,7 +5,7 @@ from .._constants import APP_SETTINGS_KEY from ..products.plugin import setup_products -from . import _classifiers_handlers, _handlers +from . import _classifiers_handlers, _groups_handlers _logger = logging.getLogger(__name__) @@ -23,5 +23,5 @@ def setup_groups(app: web.Application): # plugin dependencies setup_products(app) - app.router.add_routes(_handlers.routes) + app.router.add_routes(_groups_handlers.routes) app.router.add_route(_classifiers_handlers.routes) diff --git a/services/web/server/tests/unit/with_dbs/01/test_groups.py b/services/web/server/tests/unit/with_dbs/01/test_groups.py index 51f2f746a80..197fca37c99 100644 --- a/services/web/server/tests/unit/with_dbs/01/test_groups.py +++ b/services/web/server/tests/unit/with_dbs/01/test_groups.py @@ -24,7 +24,7 @@ from simcore_service_webserver._meta import API_VTAG from simcore_service_webserver.application_settings import setup_settings from simcore_service_webserver.db.plugin import setup_db -from simcore_service_webserver.groups._db import ( +from simcore_service_webserver.groups._groups_db import ( _DEFAULT_GROUP_OWNER_ACCESS_RIGHTS, _DEFAULT_GROUP_READ_ACCESS_RIGHTS, )