From 07d29d42f18eadae29483e9ae332dce1b4d78ce4 Mon Sep 17 00:00:00 2001 From: Keiran Price Date: Thu, 20 Jun 2019 08:28:55 +0100 Subject: [PATCH] #1: Allow single dictionary in patch function --- common/database_helpers.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/common/database_helpers.py b/common/database_helpers.py index e9d22653..3c7141ab 100644 --- a/common/database_helpers.py +++ b/common/database_helpers.py @@ -4,7 +4,7 @@ from sqlalchemy.orm import sessionmaker from common.constants import Constants -from common.exceptions import MissingRecordError, BadFilterError +from common.exceptions import MissingRecordError, BadFilterError, BadRequestError def get_record_by_id(table, id): @@ -179,15 +179,24 @@ def patch_entities(table, json_list): """ Update one or more rows in the given table, from the given list containing json. Each entity must contain its ID :param table: The table of the entities - :param json_list: the list of updated values + :param json_list: the list of updated values or a dictionary :return: The list of updated rows. """ results = [] - for entity in json_list: - for key in entity: + if type(json_list) == dict: + for key in json_list: if key.upper() == "ID": - update_row_from_id(table, entity[key], entity) - result = get_row_by_id(table, entity[key]) + update_row_from_id(table, json_list[key], json_list) + result = get_row_by_id(json_list[key]) results.append(result) + else: + for entity in json_list: + for key in entity: + if key.upper() == "ID": + update_row_from_id(table, entity[key], entity) + result = get_row_by_id(table, entity[key]) + results.append(result) + if len(results) == 0: + raise BadRequestError() return results