Skip to content

Commit

Permalink
Change model_class of LibraryDatasetsManager to LibraryDataset
Browse files Browse the repository at this point in the history
and make `update()` method compatible with supertype.
Fix the following mypy error:

```
lib/galaxy/managers/library_datasets.py:57: error: Signature of "update"
incompatible with supertype "ModelManager"  [override]
        def update(self, trans, ld, payload):
        ^
lib/galaxy/managers/library_datasets.py:57: note:      Superclass:
lib/galaxy/managers/library_datasets.py:57: note:          def update(self, item: Any, new_values: Any, flush: Any = ..., **kwargs: Any) -> DatasetInstance
lib/galaxy/managers/library_datasets.py:57: note:      Subclass:
lib/galaxy/managers/library_datasets.py:57: note:          def update(self, trans: Any, ld: Any, payload: Any) -> Any
```

Also:
- Move `check_modifiable()` check to API method.
- More type annotations.
  • Loading branch information
nsoranzo committed Sep 3, 2024
1 parent bf6fadb commit c065fa9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
65 changes: 39 additions & 26 deletions lib/galaxy/managers/library_datasets.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
"""Manager and Serializer for library datasets."""

import logging
from typing import (
Any,
Dict,
)

from sqlalchemy import select

from galaxy import (
model,
util,
)
from galaxy import util
from galaxy.exceptions import (
InsufficientPermissionsException,
InternalServerError,
ObjectNotFound,
RequestParameterInvalidException,
)
from galaxy.managers import datasets
from galaxy.managers.base import ModelManager
from galaxy.managers.context import ProvidesUserContext
from galaxy.managers.datasets import DatasetAssociationManager
from galaxy.model import (
LibraryDataset,
LibraryDatasetDatasetAssociation,
LibraryFolder,
)
from galaxy.model.base import transaction
Expand All @@ -27,13 +30,14 @@
log = logging.getLogger(__name__)


class LibraryDatasetsManager(datasets.DatasetAssociationManager):
class LibraryDatasetsManager(ModelManager[LibraryDataset]):
"""Interface/service object for interacting with library datasets."""

model_class = model.LibraryDatasetDatasetAssociation
model_class = LibraryDataset

def __init__(self, app: MinimalManagerApp):
self.app = app
super().__init__(app)
self.dataset_assoc_manager = DatasetAssociationManager(app)

def get(self, trans, decoded_library_dataset_id, check_accessible=True) -> LibraryDataset:
"""
Expand All @@ -54,14 +58,14 @@ def get(self, trans, decoded_library_dataset_id, check_accessible=True) -> Libra
ld = self.secure(trans, ld, check_accessible)
return ld

def update(self, trans, ld, payload):
def update(self, item: LibraryDataset, new_values: Dict[str, Any], flush: bool = True, **kwargs) -> LibraryDataset:
"""
Update the given library dataset - the latest linked ldda.
Updating older lddas (versions) is not allowed.
:param ld: library dataset to change
:type ld: LibraryDataset
:param payload: dictionary structure containing::
:param item: library dataset to change
:type item: LibraryDataset
:param new_values: dictionary structure containing::
:param name: new ld's name, must be longer than 0
:type name: str
:param misc_info: new ld's misc info
Expand All @@ -72,19 +76,27 @@ def update(self, trans, ld, payload):
:type genome_build: str
:param tags: list of dataset tags
:type tags: list
:type payload: dict
:type new_values: dict
:returns: the changed library dataset
:rtype: galaxy.model.LibraryDataset
"""
self.check_modifiable(trans, ld)
trans = kwargs.get("trans")
if not trans:
raise ValueError("Missing trans parameter")
# we are going to operate on the actual latest ldda
ldda = ld.library_dataset_dataset_association
payload = self._validate_and_parse_update_payload(payload)
self._set_from_dict(trans, ldda, payload)
return ld

def _set_from_dict(self, trans: ProvidesUserContext, ldda, new_data):
ldda = item.library_dataset_dataset_association
new_values = self._validate_and_parse_update_payload(new_values)
self._set_from_dict(trans, ldda, new_values, flush=flush)
return item

def _set_from_dict(
self,
trans: ProvidesUserContext,
ldda: LibraryDatasetDatasetAssociation,
new_data: Dict[str, Any],
flush: bool = True,
) -> None:
changed = False
new_name = new_data.get("name", None)
if new_name is not None and new_name != ldda.name:
Expand All @@ -100,10 +112,10 @@ def _set_from_dict(self, trans: ProvidesUserContext, ldda, new_data):
changed = True
new_file_ext = new_data.get("file_ext", None)
if new_file_ext == "auto":
self.detect_datatype(trans, ldda)
self.dataset_assoc_manager.detect_datatype(trans, ldda)
elif new_file_ext is not None and new_file_ext != ldda.extension:
ldda.extension = new_file_ext
self.set_metadata(trans, ldda)
self.dataset_assoc_manager.set_metadata(trans, ldda)
changed = True
new_genome_build = new_data.get("genome_build", None)
if new_genome_build is not None and new_genome_build != ldda.dbkey:
Expand All @@ -118,10 +130,11 @@ def _set_from_dict(self, trans: ProvidesUserContext, ldda, new_data):
changed = True
if changed:
ldda.update_parent_folder_update_times()
trans.sa_session.add(ldda)
with transaction(trans.sa_session):
trans.sa_session.commit()
return changed
session = self.session()
session.add(ldda)
if flush:
with transaction(session):
session.commit()

def _validate_and_parse_update_payload(self, payload):
MINIMUM_STRING_LENGTH = 1
Expand Down
3 changes: 2 additions & 1 deletion lib/galaxy/webapps/galaxy/api/library_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def update(self, trans, encoded_dataset_id, payload=None, **kwd):
:rtype: dictionary
"""
library_dataset = self.ld_manager.get(trans, managers_base.decode_id(self.app, encoded_dataset_id))
updated = self.ld_manager.update(trans, library_dataset, payload)
self.ld_manager.check_modifiable(trans, library_dataset)
updated = self.ld_manager.update(library_dataset, payload, trans=trans)
serialized = self.ld_manager.serialize(trans, updated)
return serialized

Expand Down
5 changes: 3 additions & 2 deletions lib/galaxy_test/api/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,11 @@ def test_update_dataset_in_folder(self):
"name": "updated_name",
"file_ext": "fasta",
"misc_info": "updated_info",
"genome_build": "updated_genome_build",
"message": "update message",
}
ld_updated = self._patch_library_dataset(ld["id"], data)
self._assert_has_keys(ld_updated, "name", "file_ext", "misc_info", "genome_build")
for key, value in data.items():
assert ld_updated[key] == value

@requires_new_library
def test_update_dataset_tags(self):
Expand Down

0 comments on commit c065fa9

Please sign in to comment.