Skip to content

Commit

Permalink
Merge pull request #151 from ral-facilities/feature/python-icat-entit…
Browse files Browse the repository at this point in the history
…y-id-endpoints-#136

Entity by ID Methods
  • Loading branch information
MRichards99 authored Aug 17, 2020
2 parents 412cf1b + 6c1dd4c commit 0600a0a
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 64 deletions.
11 changes: 6 additions & 5 deletions common/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,33 @@ def count_with_filters(self, session_id, entity_type, filters):
pass

@abstractmethod
def get_with_id(self, session_id, entity_type, id):
def get_with_id(self, session_id, entity_type, id_):
"""
Gets the entity matching the given ID for the given entity type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param id: the id of the record to find
:param id_: the id of the record to find
:return: the entity retrieved
"""
pass

@abstractmethod
def delete_with_id(self, session_id, entity_type, id):
def delete_with_id(self, session_id, entity_type, id_):
"""
Deletes the row matching the given ID for the given entity type
:param session_id: The session id of the requesting user
:param table: the table to be searched
:param id: the id of the record to delete
:param id_: the id of the record to delete
"""
pass

@abstractmethod
def update_with_id(self, session_id, entity_type, id, data):
def update_with_id(self, session_id, entity_type, id_, data):
"""
Updates the row matching the given ID for the given entity type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param id_: the id of the record to update
:param data: The dictionary that the entity should be updated with
:return: The updated entity.
"""
Expand Down
1 change: 1 addition & 0 deletions common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

class Constants:
DATABASE_URL = config.get_db_url()
ACCEPTED_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
12 changes: 6 additions & 6 deletions common/database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ def count_with_filters(self, session_id, table, filters):

@requires_session_id
@queries_records
def get_with_id(self, session_id, table, id):
return get_row_by_id(table, id).to_dict()
def get_with_id(self, session_id, table, id_):
return get_row_by_id(table, id_).to_dict()

@requires_session_id
@queries_records
def delete_with_id(self, session_id, table, id):
return delete_row_by_id(table, id)
def delete_with_id(self, session_id, table, id_):
return delete_row_by_id(table, id_)

@requires_session_id
@queries_records
def update_with_id(self, session_id, table, id, data):
return update_row_from_id(table, id, data)
def update_with_id(self, session_id, table, id_, data):
return update_row_from_id(table, id_, data)

@requires_session_id
@queries_records
Expand Down
34 changes: 17 additions & 17 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def commit_changes(self):
"""
Commits all changes to the database and closes the session
"""
log.info(f" Commiting changes to {self.table}")
log.info(" Committing changes to %s", self.table)
self.session.commit()


Expand Down Expand Up @@ -146,7 +146,7 @@ def __init__(self, table, row, new_values):
self.new_values = new_values

def execute_query(self):
log.info(f" Updating row in {self.table}")
log.info(" Updating row in %s", self.table)
self.row.update_from_dict(self.new_values)
self.session.add(self.row)
self.commit_changes()
Expand All @@ -159,7 +159,7 @@ def __init__(self, table, row):
self.row = row

def execute_query(self):
log.info(f" Deleting row {self.row} from {self.table.__tablename__}")
log.info(" Deleting row %s from %s", self.row, self.table.__tablename__)
self.session.delete(self.row)
self.commit_changes()

Expand Down Expand Up @@ -394,40 +394,40 @@ def create_rows_from_json(table, data):
return create_row_from_json(table, data)


def get_row_by_id(table, id):
def get_row_by_id(table, id_):
"""
Gets the row matching the given ID from the given table, raises MissingRecordError if it can not be found
:param table: the table to be searched
:param id: the id of the record to find
:param id_: the id of the record to find
:return: the record retrieved
"""
with ReadQuery(table) as read_query:
log.info(f" Querying {table.__tablename__} for record with ID: {id}")
where_filter = WhereFilter("ID", id, "eq")
log.info(" Querying %s for record with ID: %d", table.__tablename__, id_)
where_filter = WhereFilter("ID", id_, "eq")
where_filter.apply_filter(read_query)
return read_query.get_single_result()


def delete_row_by_id(table, id):
def delete_row_by_id(table, id_):
"""
Deletes the row matching the given ID from the given table, raises MissingRecordError if it can not be found
:param table: the table to be searched
:param id: the id of the record to delete
:param id_: the id of the record to delete
"""
log.info(f" Deleting row from {table.__tablename__} with ID: {id}")
row = get_row_by_id(table, id)
log.info(" Deleting row from %s with ID: %d", table.__tablename__, id_)
row = get_row_by_id(table, id_)
with DeleteQuery(table, row) as delete_query:
delete_query.execute_query()


def update_row_from_id(table, id, new_values):
def update_row_from_id(table, id_, new_values):
"""
Updates a record in a table
:param table: The table the record is in
:param id: The id of the record
:param id_: The id of the record
:param new_values: A JSON string containing what columns are to be updated
"""
row = get_row_by_id(table, id)
row = get_row_by_id(table, id_)
with UpdateQuery(table, row, new_values) as update_query:
update_query.execute_query()

Expand Down Expand Up @@ -496,7 +496,7 @@ def get_first_filtered_row(table, filters):
:param filters: the filter to be applied to the query
:return: the first row matching the filter
"""
log.info(f" Getting first filtered row for {table.__tablename__}")
log.info(" Getting first filtered row for %s", table.__tablename__)
return get_rows_by_filter(table, filters)[0]


Expand All @@ -508,7 +508,7 @@ def get_filtered_row_count(table, filters):
:return: int: the count of the rows
"""

log.info(f" getting count for {table.__tablename__}")
log.info(" getting count for %s", table.__tablename__)
with CountQuery(table) as count_query:
filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
Expand All @@ -523,7 +523,7 @@ def patch_entities(table, json_list):
:param json_list: the list of updated values or a dictionary
:return: The list of updated rows.
"""
log.info(f" Patching entities in {table.__tablename__}")
log.info(" Patching entities in %s", table.__tablename__)
results = []
if type(json_list) is dict:
for key in json_list:
Expand Down
6 changes: 6 additions & 0 deletions common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ class DatabaseError(ApiError):
def __init__(self, msg='Database error', *args, **kwargs):
super().__init__(msg, *args, **kwargs)
self.status_code = 500


class PythonICATError(ApiError):
def __init__(self, msg='Python ICAT error', *args, **kwargs):
super().__init__(msg, *args, **kwargs)
self.status_code = 500
18 changes: 10 additions & 8 deletions common/python_icat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

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, update_entity_by_id, \
delete_entity_by_id, get_entity_with_filters
from common.config import config
from common.exceptions import AuthenticationError
from common.models.db_models import SESSION
Expand Down Expand Up @@ -54,7 +56,7 @@ def logout(self, session_id):
@requires_session_id
@queries_records
def get_with_filters(self, session_id, table, filters):
pass
return get_entity_with_filters(self.client, table.__name__, filters)

@requires_session_id
@queries_records
Expand All @@ -78,18 +80,18 @@ def count_with_filters(self, session_id, table, filters):

@requires_session_id
@queries_records
def get_with_id(self, session_id, table, id):
pass
def get_with_id(self, session_id, table, id_):
return get_entity_by_id(self.client, table.__name__, id_, True)

@requires_session_id
@queries_records
def delete_with_id(self, session_id, table, id):
pass
def delete_with_id(self, session_id, table, id_):
return delete_entity_by_id(self.client, table.__name__, id_)

@requires_session_id
@queries_records
def update_with_id(self, session_id, table, id, data):
pass
def update_with_id(self, session_id, table, id_, data):
return update_entity_by_id(self.client, table.__name__, id_, data)

@requires_session_id
@queries_records
Expand Down
Loading

0 comments on commit 0600a0a

Please sign in to comment.