def get_template_by_id(database, template_id): """Returns the template by id. """ collection = get_template_collection(database) template_id = db_utils.ensure_objectid(template_id) arguments = [] arguments.append(db_utils.make_single_field_argument('_id', template_id)) argument = utils.merge_list_of_dicts(arguments) cursor = db_utils.mongo_find_records(collection, argument=argument) template_list = db_utils.unload_cursor(cursor) try: return template_list[0] except IndexError: return None
def update_template(database, template, changes): """Updates the given template by adding changes for the given changed parameters to the current record. """ collection = get_template_collection(database) template_id = db_utils.ensure_objectid(template['_id']) argument = db_utils.make_single_field_argument('_id', template_id) updates = [] for change in changes: if '.' in change: nested_changes = change.split('.') nested_value_string = 'template' for nested_change in nested_changes: nested_value_string += '["'"{0}"'"]'.format(nested_change) updates.append(db_utils.make_update_argument(change, eval(nested_value_string))) else: updates.append(db_utils.make_update_argument(change, template[change])) update = db_utils.merge_update_args(updates) cursor = db_utils.mongo_update_one(collection, argument, update) if cursor.matched_count == 1: return get_template_by_id(database, template_id) return None