From 4f549bce1c8977831abb883c85b0cd80791794d8 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Thu, 1 Oct 2020 16:39:59 +0000 Subject: [PATCH] #145: Add basic implementation of updating multiple entities in one request - This works some of the time, but not all as of this commit --- common/icat/backend.py | 4 +++- common/icat/helpers.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/common/icat/backend.py b/common/icat/backend.py index 122f7d9f..f9153d19 100644 --- a/common/icat/backend.py +++ b/common/icat/backend.py @@ -16,6 +16,7 @@ get_entity_with_filters, get_count_with_filters, get_first_result_with_filters, + update_entities, ) from common.config import config @@ -87,7 +88,8 @@ def create(self, session_id, table, data): @requires_session_id @queries_records def update(self, session_id, table, data): - pass + self.client.sessionId = session_id + return update_entities(self.client, table.__name__, data) @requires_session_id @queries_records diff --git a/common/icat/helpers.py b/common/icat/helpers.py index 44037b77..f6ad810c 100644 --- a/common/icat/helpers.py +++ b/common/icat/helpers.py @@ -4,7 +4,7 @@ from icat.entity import Entity, EntityList from icat.query import Query -from icat.exception import ICATSessionError, ICATValidationError +from icat.exception import ICATSessionError, ICATValidationError, ICATInternalError from common.exceptions import ( AuthenticationError, BadRequestError, @@ -163,7 +163,7 @@ def execute_query( try: log.debug("Executing ICAT query") query_result = client.search(self.query) - except ICATValidationError as e: + except (ICATValidationError, ICATInternalError) as e: raise PythonICATError(e) flat_query_includes = self.flatten_query_included_fields(self.query.includes) @@ -538,7 +538,7 @@ def update_attributes(old_entity, new_entity): except AttributeError: raise BadRequestError( f"Bad request made, cannot find attribute '{key}' within the" - f"{old_entity.BeanName} entity" + f" {old_entity.BeanName} entity" ) try: @@ -551,7 +551,7 @@ def update_attributes(old_entity, new_entity): try: old_entity.update() - except ICATValidationError as e: + except (ICATValidationError, ICATInternalError) as e: raise PythonICATError(e) @@ -774,3 +774,28 @@ def get_first_result_with_filters(client, table_name, filters): raise MissingRecordError("No results found") else: return entity_data + + +def update_entities(client, table_name, data_to_update): + """ + TODO - Add docstring + """ + + updated_data = [] + + if not isinstance(data_to_update, list): + data_to_update = [data_to_update] + + for entity in data_to_update: + try: + updated_result = update_entity_by_id( + client, table_name, entity["id"], entity + ) + updated_data.append(updated_result) + except KeyError: + raise BadRequestError( + "The new data in the request body must contain the ID (using the key:" + " 'id') of the entity you wish to update" + ) + + return updated_data