Skip to content

Commit

Permalink
Merge branch 'feature/python-icat-get-with-filters-#137' into feature…
Browse files Browse the repository at this point in the history
…/python-icat-where-filter-#142
  • Loading branch information
MRichards99 committed Aug 13, 2020
2 parents 3290f08 + 80b89e9 commit 26b8033
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 95 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 @@ -66,18 +66,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 @@ -83,7 +83,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 @@ -161,7 +161,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 @@ -174,7 +174,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 @@ -250,40 +250,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 @@ -352,7 +352,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 @@ -364,7 +364,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 @@ -379,7 +379,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
12 changes: 6 additions & 6 deletions common/icat/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,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):
return get_entity_by_id(self.client, table.__name__, id, True)
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):
return delete_entity_by_id(self.client, table.__name__, id)
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):
return update_entity_by_id(self.client, table.__name__, id, data)
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 26b8033

Please sign in to comment.