From 3cc1301ee6eaa6897344d87705ce6ba1669f8900 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Tue, 14 Jul 2020 15:39:43 +0000 Subject: [PATCH] #136: Add functionality to get a user by their ID - The 'User' entity is currently hardcoded into get_entity_by_id, due to facing an issue with case sensitivity on Python ICAT's side. In the future, any entity should be able to use this code --- common/python_icat_backend.py | 5 +++-- common/python_icat_helpers.py | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/common/python_icat_backend.py b/common/python_icat_backend.py index d4b087b5..59a965c5 100644 --- a/common/python_icat_backend.py +++ b/common/python_icat_backend.py @@ -5,7 +5,8 @@ from common.backend import Backend from common.helpers import queries_records -from common.python_icat_helpers import requires_session_id, get_session_details_helper, logout_icat_client, refresh_client_session +from common.python_icat_helpers import requires_session_id, get_session_details_helper, logout_icat_client, \ + refresh_client_session, get_entity_by_id from common.config import config from common.exceptions import AuthenticationError from common.models.db_models import SESSION @@ -79,7 +80,7 @@ def count_with_filters(self, session_id, table, filters): @requires_session_id @queries_records def get_with_id(self, session_id, table, id): - pass + return get_entity_by_id(self.client, table, id) @requires_session_id @queries_records diff --git a/common/python_icat_helpers.py b/common/python_icat_helpers.py index 6631b8c2..166cf403 100644 --- a/common/python_icat_helpers.py +++ b/common/python_icat_helpers.py @@ -1,7 +1,7 @@ from functools import wraps import logging from datetime import datetime, timedelta - +from icat.query import Query from icat.exception import ICATSessionError from common.exceptions import AuthenticationError @@ -63,3 +63,29 @@ def logout_icat_client(client): def refresh_client_session(client): client.refresh() + + +def construct_icat_query(client, entity_name, conditions=None): + return Query(client, entity_name, conditions=conditions) + + +def execute_icat_query(client, query): + client.search(query) + + +def get_entity_by_id(client, table, id): + id_condition = {'id': f'= {id}'} + + # TODO - Sort out entities + id_query = construct_icat_query(client, "User", id_condition) + + # TODO - Should all query executions be converted to strings? + query_result = client.search(id_query) + for result in query_result: + final_result = result.as_dict() + + for key, value in final_result.items(): + # Convert everything to strings so it can be converted into JSON + final_result[key] = str(final_result[key]) + + return final_result