Skip to content

Commit

Permalink
#145: Add basic implementation of updating multiple entities in one …
Browse files Browse the repository at this point in the history
…request

- This works some of the time, but not all as of this commit
  • Loading branch information
MRichards99 committed Oct 1, 2020
1 parent d3a0f0b commit 4f549bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
4 changes: 3 additions & 1 deletion common/icat/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get_entity_with_filters,
get_count_with_filters,
get_first_result_with_filters,
update_entities,
)

from common.config import config
Expand Down Expand Up @@ -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
Expand Down
33 changes: 29 additions & 4 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)


Expand Down Expand Up @@ -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

0 comments on commit 4f549bc

Please sign in to comment.