Skip to content

Commit

Permalink
Move get_user_by_username into repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Aug 16, 2023
1 parent aebdc8a commit b545931
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
13 changes: 0 additions & 13 deletions lib/galaxy/managers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
and_,
exc,
func,
select,
true,
)
from sqlalchemy.orm.exc import NoResultFound
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions lib/galaxy/model/repositories/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import cast

from sqlalchemy import select

from galaxy.model import User
from galaxy.model.repositories import (
BaseRepository,
Expand All @@ -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]
2 changes: 1 addition & 1 deletion lib/tool_shed/webapp/controllers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit b545931

Please sign in to comment.