Skip to content

Commit

Permalink
Merge pull request galaxyproject#16833 from jdavcs/dev_ts20_sa20_3
Browse files Browse the repository at this point in the history
Another batch of SA2.0 edits in TS2.0 (part 3)
  • Loading branch information
jmchilton authored Oct 17, 2023
2 parents 9bb21a0 + 3c52785 commit 9798e9d
Show file tree
Hide file tree
Showing 20 changed files with 476 additions and 488 deletions.
7 changes: 5 additions & 2 deletions lib/galaxy/managers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,11 @@ def get_users_by_ids(session: Session, user_ids):
# the tool_shed app, which has its own User model, which is different from
# galaxy.model.User. In that case, the tool_shed user model should be passed as
# the model_class argument.
def get_user_by_email(session, email: str, model_class=User):
stmt = select(model_class).filter(model_class.email == email).limit(1)
def get_user_by_email(session, email: str, model_class=User, case_sensitive=True):
filter_clause = model_class.email == email
if not case_sensitive:
filter_clause = func.lower(model_class.email) == func.lower(email)
stmt = select(model_class).where(filter_clause).limit(1)
return session.scalars(stmt).first()


Expand Down
14 changes: 8 additions & 6 deletions lib/tool_shed/managers/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
List,
)

from sqlalchemy import select

import tool_shed.util.shed_util_common as suc
from galaxy import (
exceptions,
Expand Down Expand Up @@ -41,14 +43,9 @@ def create(self, trans: ProvidesUserContext, category_request: CreateCategoryReq
raise exceptions.RequestParameterMissingException('Missing required parameter "name".')

def index_db(self, trans: ProvidesUserContext, deleted: bool) -> List[Category]:
category_db_objects: List[Category] = []
if deleted and not trans.user_is_admin:
raise exceptions.AdminRequiredException("Only administrators can query deleted categories.")
for category in (
trans.sa_session.query(Category).filter(Category.table.c.deleted == deleted).order_by(Category.table.c.name)
):
category_db_objects.append(category)
return category_db_objects
return list(get_categories_by_deleted(trans.sa_session, deleted))

def index(self, trans: ProvidesUserContext, deleted: bool) -> List[Dict[str, Any]]:
category_dicts: List[Dict[str, Any]] = []
Expand Down Expand Up @@ -80,3 +77,8 @@ def to_model(self, category: Category) -> CategoryResponse:
def get_value_mapper(app: ToolShedApp) -> Dict[str, Callable]:
value_mapper = {"id": app.security.encode_id}
return value_mapper


def get_categories_by_deleted(session, deleted):
stmt = select(Category).where(Category.deleted == deleted).order_by(Category.name)
return session.scalars(stmt)
30 changes: 17 additions & 13 deletions lib/tool_shed/managers/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from sqlalchemy import (
false,
select,
true,
)
from sqlalchemy.orm.exc import (
Expand Down Expand Up @@ -47,13 +48,11 @@ def get(self, trans, decoded_group_id=None, name=None):
if decoded_group_id is None and name is None:
raise RequestParameterInvalidException("You must supply either ID or a name of the group.")

name_query = trans.sa_session.query(trans.app.model.Group).filter(trans.app.model.Group.table.c.name == name)
id_query = trans.sa_session.query(trans.app.model.Group).filter(
trans.app.model.Group.table.c.id == decoded_group_id
)

try:
group = id_query.one() if decoded_group_id else name_query.one()
if decoded_group_id:
group = trans.sa_session.get(trans.app.model.Group, decoded_group_id)
else:
group = get_group_by_name(trans.sa_session, name, trans.app.model.Group)
except MultipleResultsFound:
raise InconsistentDatabase("Multiple groups found with the same identifier.")
except NoResultFound:
Expand Down Expand Up @@ -121,16 +120,21 @@ def list(self, trans, deleted=False):
:returns: query that will emit all groups
:rtype: sqlalchemy query
"""
is_admin = trans.user_is_admin
query = trans.sa_session.query(trans.app.model.Group)
if is_admin:
Group = trans.app.model.Group
stmt = select(Group)
if trans.user_is_admin:
if deleted is None:
# Flag is not specified, do not filter on it.
pass
elif deleted:
query = query.filter(trans.app.model.Group.table.c.deleted == true())
stmt = stmt.where(Group.deleted == true())
else:
query = query.filter(trans.app.model.Group.table.c.deleted == false())
stmt = stmt.where(Group.deleted == false())
else:
query = query.filter(trans.app.model.Group.table.c.deleted == false())
return query
stmt = stmt.where(Group.deleted == false())
return trans.sa_session.scalars(stmt)


def get_group_by_name(session, name, group_model):
stmt = select(group_model).where(group_model.name == name)
return session.execute(stmt).scalar_one()
71 changes: 28 additions & 43 deletions lib/tool_shed/managers/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

from pydantic import BaseModel
from sqlalchemy import (
and_,
false,
select,
)

from galaxy import web
Expand Down Expand Up @@ -202,17 +202,7 @@ def check_updates(app: ToolShedApp, request: UpdatesRequest) -> Union[str, Dict[
def guid_to_repository(app: ToolShedApp, tool_id: str) -> "Repository":
# tool_id = remove_protocol_and_user_from_clone_url(tool_id)
shed, _, owner, name, rest = tool_id.split("/", 5)
clause_list = [
and_(
app.model.Repository.table.c.deprecated == false(),
app.model.Repository.table.c.deleted == false(),
app.model.Repository.table.c.name == name,
app.model.User.table.c.username == owner,
app.model.Repository.table.c.user_id == app.model.User.table.c.id,
)
]
repository = app.model.context.query(app.model.Repository).filter(*clause_list).first()
return repository
return _get_repository_by_name_and_owner(app.model.context, name, owner, app.model.User)


def index_tool_ids(app: ToolShedApp, tool_ids: List[str]) -> Dict[str, Any]:
Expand All @@ -222,16 +212,7 @@ def index_tool_ids(app: ToolShedApp, tool_ids: List[str]) -> Dict[str, Any]:
repository = guid_to_repository(app, tool_id)
owner = repository.user.username
name = repository.name
clause_list = [
and_(
app.model.Repository.table.c.deprecated == false(),
app.model.Repository.table.c.deleted == false(),
app.model.Repository.table.c.name == name,
app.model.User.table.c.username == owner,
app.model.Repository.table.c.user_id == app.model.User.table.c.id,
)
]
repository = app.model.context.current.sa_session.query(app.model.Repository).filter(*clause_list).first()
repository = _get_repository_by_name_and_owner(app.model.context.current, name, owner, app.model.User)
if not repository:
log.warning(f"Repository {owner}/{name} does not exist, skipping")
continue
Expand Down Expand Up @@ -273,27 +254,7 @@ def index_tool_ids(app: ToolShedApp, tool_ids: List[str]) -> Dict[str, Any]:


def index_repositories(app: ToolShedApp, name: Optional[str], owner: Optional[str], deleted: bool):
clause_list = [
and_(
app.model.Repository.table.c.deprecated == false(),
app.model.Repository.table.c.deleted == deleted,
)
]
if owner is not None:
clause_list.append(
and_(
app.model.User.table.c.username == owner,
app.model.Repository.table.c.user_id == app.model.User.table.c.id,
)
)
if name is not None:
clause_list.append(app.model.Repository.table.c.name == name)
repositories = []
for repository in (
app.model.context.query(app.model.Repository).filter(*clause_list).order_by(app.model.Repository.table.c.name)
):
repositories.append(repository)
return repositories
return list(_get_repository_by_name_and_owner_and_deleted(app.model.context, name, owner, deleted, app.model.User))


def can_manage_repo(trans: ProvidesUserContext, repository: Repository) -> bool:
Expand Down Expand Up @@ -616,3 +577,27 @@ def upload_tar_and_set_metadata(
else:
raise InternalServerError(message)
return message


def _get_repository_by_name_and_owner(session, name, owner, user_model):
stmt = (
select(Repository)
.where(Repository.deprecated == false())
.where(Repository.deleted == false())
.where(Repository.name == name)
.where(user_model.username == owner)
.where(Repository.user_id == user_model.id)
.limit(1)
)
return session.scalars(stmt).first()


def _get_repository_by_name_and_owner_and_deleted(session, name, owner, deleted, user_model):
stmt = select(Repository).where(Repository.deprecated == false()).where(Repository.deleted == deleted)
if owner is not None:
stmt = stmt.where(user_model.username == owner)
stmt = stmt.where(Repository.user_id == user_model.id)
if name is not None:
stmt = stmt.where(Repository.name == name)
stmt = stmt.order_by(Repository.name).limit(1)
return session.scalars(stmt).first()
13 changes: 8 additions & 5 deletions lib/tool_shed/managers/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List

from sqlalchemy import select

from galaxy.exceptions import RequestParameterInvalidException
from galaxy.model.base import transaction
from galaxy.security.validate_user_input import (
Expand All @@ -18,11 +20,7 @@

def index(app: ToolShedApp, deleted: bool) -> List[ApiUser]:
users: List[ApiUser] = []
for user in (
app.model.context.query(app.model.User)
.filter(app.model.User.table.c.deleted == deleted)
.order_by(app.model.User.table.c.username)
):
for user in get_users_by_deleted(app.model.context, app.model.User, deleted):
users.append(get_api_user(app, user))
return users

Expand Down Expand Up @@ -77,3 +75,8 @@ def _validate(trans: ProvidesUserContext, email: str, password: str, confirm: st
)
).rstrip()
return message


def get_users_by_deleted(session, user_model, deleted):
stmt = select(user_model).where(user_model.deleted == deleted).order_by(user_model.username)
return session.scalars(stmt)
Loading

0 comments on commit 9798e9d

Please sign in to comment.