Skip to content

Commit

Permalink
#136: Add functionality to get a user by their ID
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
MRichards99 committed Jul 14, 2020
1 parent ddca280 commit 3cc1301
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions common/python_icat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion common/python_icat_helpers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3cc1301

Please sign in to comment.