Skip to content

Commit

Permalink
Use APIKeys model from app
Browse files Browse the repository at this point in the history
Galaxy and the ToolShed have duplicated APIKeys models and the ApiKeyManager can be used from both apps
  • Loading branch information
davelopez committed Sep 30, 2022
1 parent 0356c2e commit e31d575
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/galaxy/managers/api_keys.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down

0 comments on commit e31d575

Please sign in to comment.