diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bc58e9a0a7ea35bcd1c0d896ecb1e68b0aa0c75..6045d552cada022af5afe1d4969ec1fda3520185 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### +* `apiutils.apply_to_ids` -- a helper which applies a function to all ids which + are used by an entity (own entity, parents, properties, references etc.). + ### Changed ### ### Deprecated ### ### Fixed ### +* import bugs in apiutils + ## [0.2.4] ### Added diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index 3af9345e48941d5f61766cc1c9d49a4a7b9c4fcb..c4eb3c28cdd7afd8e49dd03d829cc141c673af7a 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -672,24 +672,33 @@ def describe_diff(olddiff, newdiff, name=None, as_update=True): return description -def id_query(ids): - """ executes a query that returns entities purely based on whether their id - is in the supplied list of ids """ - q = "FIND Entity with " + " OR ".join(["id={}".format(id) for id in ids]) +def apply_to_ids(entities, func): + """ Apply a function to all ids. - return execute_query(q) + All ids means the ids of the entities themselves but also to all parents, + properties and referenced entities. - -def retrieve_multiple_entities(ids, step=20): + Parameters + ---------- + entities : list of Entity + func : function with one parameter. """ - retrieve the entities with the ids in the supplied list. + for entity in entities: + _apply_to_ids_of_entity(entity, func) - Entities are retrieved via queries. Due to a limited length of the query, - the entities are retrieved in chunks of sive "step") - """ - collection = Container() +def _apply_to_ids_of_entity(entity, func): + entity.id = func(entity.id) - for i in range(len(ids)//step+1): - collection.extend(id_query(ids[i*step:(i+1)*step])) + for par in entity.parents: + par.id = func(par.id) - return collection + for prop in entity.properties: + prop.id = func(prop.id) + isref = is_reference(prop.datatype) + + if isref: + if isinstance(prop.value, list): + prop.value = [func(el) for el in prop.value] + else: + if prop.value is not None: + prop.value = func(prop.value) diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 7eb9b9a76c8a60f6a341ea9524d00d4c84b5d2b8..364ff99270294a6be714ddf1db759ac63746daaa 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -941,23 +941,6 @@ class Entity(object): return self - def apply_to_ids(self, func): - self.id = func(self.id) - - for par in self.parents: - par.id = func(par.id) - - for prop in self.properties: - prop.id = func(prop.id) - isref = is_reference(prop.datatype) - - if isref: - if isinstance(prop.value, list): - prop.value = [func(el) for el in prop.value] - else: - if prop.value is not None: - prop.value = func(prop.value) - def _parse_col_values(cdt, vals): matcher = re.compile(r"^(?P<col>[^<]+)<(?P<dt>[^>]+)>$")