Skip to content

Commit

Permalink
Refactor library content routes and controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
arash77 committed Sep 26, 2024
1 parent e10fd62 commit 7a57443
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 50 deletions.
36 changes: 24 additions & 12 deletions client/src/api/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12920,8 +12920,8 @@ export interface components {
*/
upload_option: components["schemas"]["UploadOption"] | null;
};
/** LibraryContentsCreateDatasetListResponse */
LibraryContentsCreateDatasetListResponse: components["schemas"]["LibraryContentsCreateDatasetResponse"][];
/** LibraryContentsCreateDatasetCollectionResponse */
LibraryContentsCreateDatasetCollectionResponse: components["schemas"]["LibraryContentsCreateDatasetResponse"][];
/** LibraryContentsCreateDatasetResponse */
LibraryContentsCreateDatasetResponse: {
/** Created From Basename */
Expand All @@ -12948,8 +12948,13 @@ export interface components {
misc_blurb: string | null;
/** Misc Info */
misc_info: string | null;
/** Model Class */
model_class: string;
/**
* Model class
* @description The name of the database model class.
* @constant
* @enum {string}
*/
model_class: "LibraryDatasetDatasetAssociation";
/** Name */
name: string;
/** Parent Library Id */
Expand Down Expand Up @@ -13192,7 +13197,13 @@ export interface components {
misc_blurb: string | null;
/** Misc Info */
misc_info: string | null;
model_class: components["schemas"]["ModelClass"];
/**
* Model class
* @description The name of the database model class.
* @constant
* @enum {string}
*/
model_class: "LibraryDataset";
/** Name */
name: string;
/**
Expand Down Expand Up @@ -13231,7 +13242,13 @@ export interface components {
item_count: number;
/** Library Path */
library_path: string[];
model_class: components["schemas"]["ModelClass"];
/**
* Model class
* @description The name of the database model class.
* @constant
* @enum {string}
*/
model_class: "LibraryFolder";
/** Name */
name: string;
/** Parent Id */
Expand Down Expand Up @@ -13807,11 +13824,6 @@ export interface components {
*/
time: string;
};
/**
* ModelClass
* @enum {string}
*/
ModelClass: "LibraryDataset" | "LibraryFolder";
/**
* ModelStoreFormat
* @description Available types of model stores for export.
Expand Down Expand Up @@ -27943,7 +27955,7 @@ export interface operations {
"application/json":
| components["schemas"]["LibraryContentsCreateFolderListResponse"]
| components["schemas"]["LibraryContentsCreateFileListResponse"]
| components["schemas"]["LibraryContentsCreateDatasetListResponse"]
| components["schemas"]["LibraryContentsCreateDatasetCollectionResponse"]
| components["schemas"]["LibraryContentsCreateDatasetResponse"];
};
};
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/actions/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ def _upload_dataset(self, trans, folder_id: int, payload):
trans, payload, full_dir, import_dir_desc, library_bunch
)
elif payload.upload_option == "upload_paths":
uploaded_datasets, _, _ = self._get_path_paste_uploaded_datasets(
uploaded_datasets, response_code, message = self._get_path_paste_uploaded_datasets(
trans, payload.model_dump(), library_bunch, 200, None
)
if response_code != 200:
raise exceptions.RequestParameterInvalidException(message)
if payload.upload_option == "upload_file" and not uploaded_datasets:
raise exceptions.RequestParameterInvalidException("Select a file, enter a URL or enter text")
json_file_path = upload_common.create_paramfile(trans, uploaded_datasets)
Expand Down Expand Up @@ -311,7 +313,7 @@ def _upload_library_dataset(self, trans, payload, library_id):
)
if error:
raise exceptions.RequestParameterInvalidException(message)
created_outputs_dict = self._upload_dataset(trans, folder_id=folder.id, payload=payload)
created_outputs_dict = self._upload_dataset(trans, folder.id, payload)
return self._create_response(trans, payload, created_outputs_dict, library_id)

def _create_folder(self, trans, payload, library_id):
Expand Down
19 changes: 11 additions & 8 deletions lib/galaxy/schema/library_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
Field,
RootModel,
)
from typing_extensions import (
Annotated,
Literal,
)

from galaxy.schema.fields import (
DecodedDatabaseIdField,
EncodedDatabaseIdField,
EncodedLibraryFolderDatabaseIdField,
LibraryFolderDatabaseIdField,
ModelClassField,
)
from galaxy.schema.schema import (
Model,
Expand All @@ -42,11 +47,6 @@ class LinkDataOnly(str, Enum):
link_to_files = "link_to_files"


class ModelClass(str, Enum):
LibraryDataset = "LibraryDataset"
LibraryFolder = "LibraryFolder"


class LibraryContentsCreatePayload(Model):
create_type: CreateType = Field(
...,
Expand Down Expand Up @@ -190,14 +190,14 @@ class LibraryContentsIndexListResponse(RootModel):


class LibraryContentsShowResponse(Model):
model_class: ModelClass
name: str
genome_build: Optional[str]
update_time: str
parent_library_id: EncodedDatabaseIdField


class LibraryContentsShowFolderResponse(LibraryContentsShowResponse):
model_class: Annotated[Literal["LibraryFolder"], ModelClassField(Literal["LibraryFolder"])]
id: EncodedLibraryFolderDatabaseIdField
parent_id: Optional[EncodedLibraryFolderDatabaseIdField]
description: str
Expand All @@ -207,6 +207,7 @@ class LibraryContentsShowFolderResponse(LibraryContentsShowResponse):


class LibraryContentsShowDatasetResponse(LibraryContentsShowResponse):
model_class: Annotated[Literal["LibraryDataset"], ModelClassField(Literal["LibraryDataset"])]
id: EncodedDatabaseIdField
ldda_id: EncodedDatabaseIdField
folder_id: EncodedLibraryFolderDatabaseIdField
Expand Down Expand Up @@ -256,7 +257,9 @@ class LibraryContentsCreateDatasetResponse(Model):
# functions that are shared by LibraryFolderContentsService too
id: str
hda_ldda: str
model_class: str
model_class: Annotated[
Literal["LibraryDatasetDatasetAssociation"], ModelClassField(Literal["LibraryDatasetDatasetAssociation"])
]
name: str
deleted: bool
visible: bool
Expand All @@ -278,7 +281,7 @@ class LibraryContentsCreateDatasetResponse(Model):
model_config = ConfigDict(extra="allow")


class LibraryContentsCreateDatasetListResponse(RootModel):
class LibraryContentsCreateDatasetCollectionResponse(RootModel):
root: List[LibraryContentsCreateDatasetResponse]


Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/webapps/galaxy/api/library_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from galaxy.schema.fields import DecodedDatabaseIdField
from galaxy.schema.library_contents import (
LibraryContentsCollectionCreatePayload,
LibraryContentsCreateDatasetListResponse,
LibraryContentsCreateDatasetCollectionResponse,
LibraryContentsCreateDatasetResponse,
LibraryContentsCreateFileListResponse,
LibraryContentsCreateFolderListResponse,
Expand Down Expand Up @@ -97,7 +97,7 @@ def create(
) -> Union[
LibraryContentsCreateFolderListResponse,
LibraryContentsCreateFileListResponse,
LibraryContentsCreateDatasetListResponse,
LibraryContentsCreateDatasetCollectionResponse,
LibraryContentsCreateDatasetResponse,
]:
"""
Expand Down
40 changes: 14 additions & 26 deletions lib/galaxy/webapps/galaxy/services/library_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from galaxy.schema.fields import DecodedDatabaseIdField
from galaxy.schema.library_contents import (
LibraryContentsCollectionCreatePayload,
LibraryContentsCreateDatasetListResponse,
LibraryContentsCreateDatasetCollectionResponse,
LibraryContentsCreateDatasetResponse,
LibraryContentsCreateFileListResponse,
LibraryContentsCreateFolderListResponse,
Expand Down Expand Up @@ -96,14 +96,13 @@ def index(
# appending all other items in the library recursively
for content in self._traverse(trans, library.root_folder, current_user_roles):
url = self._url_for(trans, library_id, content.id, content.api_type)
response_class: Union[
Type[LibraryContentsIndexFolderResponse], Type[LibraryContentsIndexDatasetResponse]
] = (
LibraryContentsIndexFolderResponse
if content.api_type == "folder"
else LibraryContentsIndexDatasetResponse
)
rval.append(response_class(id=content.id, type=content.api_type, name=content.api_path, url=url))
response_model: Union[LibraryContentsIndexFolderResponse, LibraryContentsIndexDatasetResponse]
common_args = dict(id=content.id, type=content.api_type, name=content.api_path, url=url)
if content.api_type == "folder":
response_model = LibraryContentsIndexFolderResponse(**common_args)
else:
response_model = LibraryContentsIndexDatasetResponse(**common_args)
rval.append(response_model)
return LibraryContentsIndexListResponse(root=rval)

def show(
Expand All @@ -116,20 +115,15 @@ def show(
]:
"""Returns information about library file or folder."""
class_name, content_id = self._decode_library_content_id(id)
rval: Union[
LibraryContentsShowFolderResponse,
LibraryContentsShowDatasetResponse,
]
if class_name == "LibraryFolder":
content = self.get_library_folder(trans, content_id, check_ownership=False, check_accessible=True)
rval = LibraryContentsShowFolderResponse(**content.to_dict(view="element"))
return LibraryContentsShowFolderResponse(**content.to_dict(view="element"))
else:
content = self.get_library_dataset(trans, content_id, check_ownership=False, check_accessible=True)
rval_dict = content.to_dict(view="element")
tag_manager = tags.GalaxyTagHandler(trans.sa_session)
rval_dict["tags"] = tag_manager.get_tags_list(content.library_dataset_dataset_association.tags)
rval = LibraryContentsShowDatasetResponse(**rval_dict)
return rval
return LibraryContentsShowDatasetResponse(**rval_dict)

def create(
self,
Expand All @@ -141,7 +135,7 @@ def create(
) -> Union[
LibraryContentsCreateFolderListResponse,
LibraryContentsCreateFileListResponse,
LibraryContentsCreateDatasetListResponse,
LibraryContentsCreateDatasetCollectionResponse,
LibraryContentsCreateDatasetResponse,
]:
"""Create a new library file or folder."""
Expand All @@ -163,7 +157,7 @@ def create(
rval = self._copy_hdca_to_library_folder(
trans, self.hda_manager, payload.from_hdca_id, payload.folder_id, payload.ldda_message
)
return LibraryContentsCreateDatasetListResponse(root=rval)
return LibraryContentsCreateDatasetCollectionResponse(root=rval)

# Now create the desired content object, either file or folder.
if payload.create_type == "file":
Expand All @@ -174,7 +168,7 @@ def create(
return LibraryContentsCreateFolderListResponse(root=rval)
elif payload.create_type == "collection":
rval = self._create_collection(trans, payload, parent)
return LibraryContentsCreateDatasetListResponse(root=rval)
return LibraryContentsCreateDatasetCollectionResponse(root=rval)
else:
raise exceptions.RequestParameterInvalidException("Invalid create_type specified.")

Expand Down Expand Up @@ -262,13 +256,7 @@ def _decode_library_content_id(
f"Malformed library content id ( {str(content_id)} ) specified, unable to decode."
)

def _url_for(
self,
trans: ProvidesUserContext,
library_id: DecodedDatabaseIdField,
id: int,
type: str,
) -> Optional[str]:
def _url_for(self, trans: ProvidesUserContext, library_id: DecodedDatabaseIdField, id, type):
encoded_id = trans.security.encode_id(id)
if type == "folder":
encoded_id = f"F{encoded_id}"
Expand Down

0 comments on commit 7a57443

Please sign in to comment.