Skip to content

Commit

Permalink
#136: Resolve ICAT error where related entity were trying to be set …
Browse files Browse the repository at this point in the history
…to null

- The code to convert request body values into dates has been tested and is fully working.
  • Loading branch information
MRichards99 committed Jul 22, 2020
1 parent f072c1d commit 4b15a05
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions common/python_icat_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def refresh_client_session(client):
client.refresh()


def construct_icat_query(client, entity_name, conditions=None, aggregate=None):
def construct_icat_query(client, entity_name, conditions=None, aggregate=None, includes=None):
"""
Create a Query object within Python ICAT
Expand All @@ -78,11 +78,13 @@ def construct_icat_query(client, entity_name, conditions=None, aggregate=None):
:param aggregate: Name of the aggregate function to apply. Operations such as counting the
number of records. See `icat.query.setAggregate for valid values.
:type aggregate: :class:`str`
:param includes: TODO
:type includes: iterable of :class:`str` or :class:`str`
:return: Query object from Python ICAT
"""

try:
query = Query(client, entity_name, conditions=conditions, aggregate=aggregate)
query = Query(client, entity_name, conditions=conditions, aggregate=aggregate, includes=includes)
except ValueError:
# TODO - Add appropriate action
pass
Expand Down Expand Up @@ -239,9 +241,13 @@ def get_entity_by_id(client, table_name, id, return_json_formattable_data):

selected_entity_name = get_python_icat_entity_name(client, table_name)

id_query = construct_icat_query(client, selected_entity_name, id_condition)
id_query = construct_icat_query(client, selected_entity_name, conditions=id_condition, includes="1")
entity_by_id_data = execute_icat_query(client, id_query, return_json_formattable_data)

if entity_by_id_data == []:
# Cannot find any data matching the given ID
raise MissingRecordError("No result found")

return entity_by_id_data


Expand Down Expand Up @@ -273,13 +279,14 @@ def update_entity_by_id(client, table_name, id, new_data):
:type id: :class:`int`
:param new_data: JSON from request body providing new data to update the record with the
specified ID
:return: The updated record of the specified ID from the given entity
"""

entity_id_data = get_entity_by_id(client, table_name, id, False)
# There will only ever be one record associated with a single ID
entity_id_data = entity_id_data[0]

update_attributes(entity_id_data, new_data)
# There will only ever be one record associated with a single ID - if a record with the
# specified ID cannot be found, it'll be picked up by the MissingRecordError in
# get_entity_by_id()
update_attributes(entity_id_data[0], new_data)

# The record is re-obtained from Python ICAT (rather than using entity_id_data) to show to the
# user whether the change has actually been applied
Expand Down

0 comments on commit 4b15a05

Please sign in to comment.