From e31d5755981112ad7dfe78ccaf46005096d923f3 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Fri, 30 Sep 2022 13:33:26 +0200 Subject: [PATCH] Use APIKeys model from app Galaxy and the ToolShed have duplicated APIKeys models and the ApiKeyManager can be used from both apps --- lib/galaxy/managers/api_keys.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/galaxy/managers/api_keys.py b/lib/galaxy/managers/api_keys.py index a155a9bce103..a31e9d72a618 100644 --- a/lib/galaxy/managers/api_keys.py +++ b/lib/galaxy/managers/api_keys.py @@ -1,29 +1,32 @@ -from typing import Optional - -from galaxy.model import ( - APIKeys, - User, +from typing import ( + Optional, + TYPE_CHECKING, ) + +from galaxy.model import User from galaxy.structured_app import BasicSharedApp +if TYPE_CHECKING: + from galaxy.model import APIKeys + class ApiKeyManager: def __init__(self, app: BasicSharedApp): self.app = app - def get_api_key(self, user: User) -> Optional[APIKeys]: + def get_api_key(self, user: User) -> Optional["APIKeys"]: sa_session = self.app.model.context api_key = ( - sa_session.query(APIKeys) + sa_session.query(self.app.model.APIKeys) .filter_by(user_id=user.id, deleted=False) - .order_by(APIKeys.create_time.desc()) + .order_by(self.app.model.APIKeys.create_time.desc()) .first() ) return api_key - def create_api_key(self, user: User) -> APIKeys: + def create_api_key(self, user: User) -> "APIKeys": guid = self.app.security.get_new_guid() - new_key = APIKeys() + new_key = self.app.model.APIKeys() new_key.user_id = user.id new_key.key = guid sa_session = self.app.model.context @@ -44,7 +47,7 @@ def delete_api_key(self, user: User) -> None: sa_session = self.app.model.context # Before it was possible to create multiple API keys for the same user although they were not considered valid # So all non-deleted keys are marked as deleted for backward compatibility - api_keys = sa_session.query(APIKeys).filter_by(user_id=user.id, deleted=False) + api_keys = sa_session.query(self.app.model.APIKeys).filter_by(user_id=user.id, deleted=False) for api_key in api_keys: api_key.deleted = True sa_session.add(api_key)