Merge pull request #9715 from mvdbeek/fix_import_into_wrong_history

[20.01] Make collection import from library import into correct history
This commit is contained in:
Dannon
2020-05-05 12:24:42 -04:00
committed by GitHub
3 changed files with 27 additions and 17 deletions
+22 -15
View File
@@ -89,7 +89,7 @@ class DatasetCollectionManager(object):
def create(self, trans, parent, name, collection_type, element_identifiers=None,
elements=None, implicit_collection_info=None, trusted_identifiers=None,
hide_source_items=False, tags=None, copy_elements=False):
hide_source_items=False, tags=None, copy_elements=False, history=None):
"""
PRECONDITION: security checks on ability to add to parent
occurred during load.
@@ -108,6 +108,7 @@ class DatasetCollectionManager(object):
elements=elements,
hide_source_items=hide_source_items,
copy_elements=copy_elements,
history=history,
)
implicit_inputs = []
@@ -162,7 +163,7 @@ class DatasetCollectionManager(object):
return self.__persist(dataset_collection_instance, flush=flush)
def create_dataset_collection(self, trans, collection_type, element_identifiers=None, elements=None,
hide_source_items=None, copy_elements=False):
hide_source_items=None, copy_elements=False, history=None):
# Make sure at least one of these is None.
assert element_identifiers is None or elements is None
@@ -180,11 +181,12 @@ class DatasetCollectionManager(object):
collection_type_description=collection_type_description,
element_identifiers=element_identifiers,
hide_source_items=hide_source_items,
copy_elements=copy_elements)
copy_elements=copy_elements,
history=history)
else:
if has_subcollections:
# Nested collection - recursively create collections as needed.
self.__recursively_create_collections_for_elements(trans, elements, hide_source_items, copy_elements=copy_elements)
self.__recursively_create_collections_for_elements(trans, elements, hide_source_items, copy_elements=copy_elements, history=history)
# else if elements is set, it better be an ordered dict!
if elements is not self.ELEMENTS_UNINITIALIZED:
@@ -200,7 +202,8 @@ class DatasetCollectionManager(object):
collection_type_description,
element_identifiers,
hide_source_items=False,
copy_elements=False):
copy_elements=False,
history=None):
if collection_type_description.has_subcollections():
# Nested collection - recursively create collections and update identifiers.
self.__recursively_create_collections_for_identifiers(trans, element_identifiers, hide_source_items, copy_elements)
@@ -211,12 +214,14 @@ class DatasetCollectionManager(object):
elements = self.__load_elements(trans=trans,
element_identifiers=element_identifier['element_identifiers'],
hide_source_items=hide_source_items,
copy_elements=copy_elements)
copy_elements=copy_elements,
history=history)
if not new_collection:
elements = self.__load_elements(trans=trans,
element_identifiers=element_identifiers,
hide_source_items=hide_source_items,
copy_elements=copy_elements)
copy_elements=copy_elements,
history=history)
return elements
def _append_tags(self, dataset_collection_instance, implicit_inputs=None, tags=None):
@@ -356,7 +361,7 @@ class DatasetCollectionManager(object):
return element_identifiers
def __recursively_create_collections_for_elements(self, trans, elements, hide_source_items, copy_elements):
def __recursively_create_collections_for_elements(self, trans, elements, hide_source_items, copy_elements, history=None):
if elements is self.ELEMENTS_UNINITIALIZED:
return
@@ -377,21 +382,23 @@ class DatasetCollectionManager(object):
collection_type=collection_type,
elements=sub_elements,
hide_source_items=hide_source_items,
copy_elements=copy_elements
copy_elements=copy_elements,
history=history,
)
new_elements[key] = collection
elements.update(new_elements)
def __load_elements(self, trans, element_identifiers, hide_source_items=False, copy_elements=False):
def __load_elements(self, trans, element_identifiers, hide_source_items=False, copy_elements=False, history=None):
elements = OrderedDict()
for element_identifier in element_identifiers:
elements[element_identifier["name"]] = self.__load_element(trans,
element_identifier=element_identifier,
hide_source_items=hide_source_items,
copy_elements=copy_elements)
copy_elements=copy_elements,
history=history)
return elements
def __load_element(self, trans, element_identifier, hide_source_items, copy_elements):
def __load_element(self, trans, element_identifier, hide_source_items, copy_elements, history=None):
# if not isinstance( element_identifier, dict ):
# # Is allowing this to just be the id of an hda too clever? Somewhat
# # consistent with other API methods though.
@@ -426,15 +433,15 @@ class DatasetCollectionManager(object):
decoded_id = int(trans.app.security.decode_id(encoded_id))
hda = self.hda_manager.get_accessible(decoded_id, trans.user)
if copy_elements:
element = self.hda_manager.copy(hda, history=trans.history, hide_copy=True)
element = self.hda_manager.copy(hda, history=history or trans.history, hide_copy=True)
else:
element = hda
if hide_source_items and self.hda_manager.get_owned(hda.id, user=trans.user, current_history=trans.history):
if hide_source_items and self.hda_manager.get_owned(hda.id, user=trans.user, current_history=history or trans.history):
hda.visible = False
self.tag_handler.apply_item_tags(user=trans.user, item=element, tags_str=tag_str)
elif src_type == 'ldda':
element = self.ldda_manager.get(trans, encoded_id, check_accessible=True)
element = element.to_history_dataset_association(trans.history, add_to_history=True, visible=not hide_source_items)
element = element.to_history_dataset_association(history or trans.history, add_to_history=True, visible=not hide_source_items)
self.tag_handler.apply_item_tags(user=trans.user, item=element, tags_str=tag_str)
elif src_type == 'hdca':
# TODO: Option to copy? Force copy? Copy or allow if not owned?
@@ -522,6 +522,7 @@ class HistoryContentsController(BaseAPIController, UsesLibraryMixin, UsesLibrary
dataset_collection_instance = service.create(
trans,
parent=history,
history=history,
**create_params
)
elif source == "hdca":
+4 -2
View File
@@ -473,14 +473,15 @@ class HistoryContentsApiTestCase(ApiTestCase, TestsDatasets):
ld = self.library_populator.new_library_dataset("el1")
ldda_id = ld["ldda_id"]
element_identifiers = [{"name": "el1", "src": "ldda", "id": ldda_id}]
history_id = self._new_history()
create_data = dict(
history_id=self.history_id,
history_id=history_id,
type="dataset_collection",
name="Test From Library",
element_identifiers=json.dumps(element_identifiers),
collection_type="list",
)
create_response = self._post("histories/%s/contents/dataset_collections" % self.history_id, create_data)
create_response = self._post("histories/%s/contents/dataset_collections" % history_id, create_data)
hdca = self.__check_create_collection_response(create_response)
elements = hdca["elements"]
assert len(elements) == 1
@@ -488,6 +489,7 @@ class HistoryContentsApiTestCase(ApiTestCase, TestsDatasets):
assert hda["hda_ldda"] == "hda"
assert hda["history_content_type"] == "dataset"
assert hda["copied_from_ldda_id"] == ldda_id
assert hda['history_id'] == history_id
def test_hdca_from_inaccessible_library_datasets(self):
library, library_dataset = self.library_populator.new_library_dataset_in_private_library("HDCACreateInaccesibleLibrary")