From b545931d5c1815f89a9f600482dfe8887b5a38fd Mon Sep 17 00:00:00 2001 From: John Davis Date: Wed, 16 Aug 2023 18:22:28 -0400 Subject: [PATCH] Move get_user_by_username into repositories --- lib/galaxy/managers/users.py | 13 ------------- lib/galaxy/model/repositories/user.py | 11 +++++++++++ lib/tool_shed/webapp/controllers/repository.py | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/galaxy/managers/users.py b/lib/galaxy/managers/users.py index 4489a00689e6..bd739a169ffe 100644 --- a/lib/galaxy/managers/users.py +++ b/lib/galaxy/managers/users.py @@ -20,7 +20,6 @@ and_, exc, func, - select, true, ) from sqlalchemy.orm.exc import NoResultFound @@ -838,15 +837,3 @@ def _add_parsers(self): ) self.fn_filter_parsers.update({}) - - -def get_user_by_username(session, user_class, username): - """ - Get a user from the database by username. - (We pass the session and the user_class to accommodate usage from the tool_shed app.) - """ - try: - stmt = select(user_class).filter(user_class.username == username) - return session.execute(stmt).scalar_one() - except Exception: - return None diff --git a/lib/galaxy/model/repositories/user.py b/lib/galaxy/model/repositories/user.py index 9eaefe6f4845..37114252953f 100644 --- a/lib/galaxy/model/repositories/user.py +++ b/lib/galaxy/model/repositories/user.py @@ -1,5 +1,7 @@ from typing import cast +from sqlalchemy import select + from galaxy.model import User from galaxy.model.repositories import ( BaseRepository, @@ -13,3 +15,12 @@ def __init__(self, session: SessionType): def get(self, primary_key: int) -> User: return cast(User, super().get(primary_key)) + + +def get_user_by_username(session: SessionType, user_class, username: str): + """Get a user from the database by username.""" + # This may be called from the tool_shed app, which has a different + # definition of the User mapped class. Therefore, we must pass the User + # class as an argument instead of importing from galaxy.model. + stmt = select(user_class).filter(user_class.username == username) + return session.execute(stmt).scalar_one() # type:ignore[union-attr] diff --git a/lib/tool_shed/webapp/controllers/repository.py b/lib/tool_shed/webapp/controllers/repository.py index e7627292f426..69c0c6fd0aaf 100644 --- a/lib/tool_shed/webapp/controllers/repository.py +++ b/lib/tool_shed/webapp/controllers/repository.py @@ -24,8 +24,8 @@ util, web, ) -from galaxy.managers.users import get_user_by_username from galaxy.model.base import transaction +from galaxy.model.repositories.user import get_user_by_username from galaxy.tool_shed.util import dependency_display from galaxy.tools.repositories import ValidationContext from galaxy.web.form_builder import (