Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another batch of SA2.0 edits in TS2.0 (part 3) #16833

Merged
merged 22 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
852cb9f
Fix SA2.0 usage, refactor tool_shed.webapp.api.repository_revisions
jdavcs Oct 6, 2023
49cafd0
Fix SA2.0 usage in tool_shed.webapp.api.groups
jdavcs Oct 6, 2023
c4e1c9b
Fix SA2.0 usage in tool_shed.webapp.controllers.user
jdavcs Oct 6, 2023
3238b21
Fix SA2.0 usage in tool_shed.webapp.controllers.repository
jdavcs Oct 6, 2023
f553b75
Fix SA2.0 usage in tool_shed.webapp.controllers.repository
jdavcs Oct 10, 2023
6a6e5bf
Fix SA2.0 formatting in tool_shed.repository_registry + refactor
jdavcs Oct 10, 2023
70c7f03
Fix SA2.0 usage in tool_shed.test.base.twilltestcase
jdavcs Oct 11, 2023
a5110bc
Drop unused functions from tool_shed.test.base.test_db_util
jdavcs Oct 11, 2023
5a70f2f
Fix SA2.0 usage in tool_shed.test.base.test_db_util; refactor
jdavcs Oct 11, 2023
08b5a43
Fix SA2.0 usage in tool_shed.managers.categories
jdavcs Oct 11, 2023
ff8f629
Fix SA2.0 usage in tool_shed.managers.users
jdavcs Oct 11, 2023
77df9c9
Fix SA2.0 usage in tool_shed.managers.repositories
jdavcs Oct 11, 2023
4db5e31
Fix SA2.0 usage in tool_shed.managers.groups
jdavcs Oct 11, 2023
a5a7d06
Fix SA2.0 usage in tool_shed.util.metadata_util
jdavcs Oct 11, 2023
1711e96
Fix SA2.0 usage in tool_shed.util.shed_util_common
jdavcs Oct 11, 2023
d4a0cce
Fix SA2.0 usage in tool_shed.util.shed_index
jdavcs Oct 11, 2023
b064064
Fix SA2.0 usage in tool_shed.util.search_util
jdavcs Oct 11, 2023
43500cf
Fix SA2.0 usage in tool_shed.util.commit_util
jdavcs Oct 11, 2023
bf919c0
Fix SA2.0 usage in tool_shed.util.repository_util
jdavcs Oct 12, 2023
68831dd
Fix SA2.0 usage in tool_shed.metadata, refactor
jdavcs Oct 12, 2023
b0684fa
Import models
jdavcs Oct 12, 2023
3c52785
Ignore typing error (see note)
jdavcs Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/galaxy/managers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,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