Exemple #1
0
def update_tables(case_id, tenant_id, updates):
    """Update the values in the database"""
    db_config['tenant_id'] = tenant_id
    extraction_db = DB(
        'extraction',
        **db_config)  # only in ocr or process_queue we are updating
    queue_db = DB('queues',
                  **db_config)  # only in ocr or process_queue we are updating

    for table, colum_values in updates.items():
        if table == 'ocr':
            extraction_db.update(table,
                                 update=colum_values,
                                 where={'case_id': case_id})
        if table == 'process_queue':
            queue_db.update(table,
                            update=colum_values,
                            where={'case_id': case_id})
    return "UPDATED IN THE DATABASE SUCCESSFULLY"
def save_data(tenant_id,
              database,
              table,
              data,
              case_id,
              case_id_based=True,
              view='records'):
    """Util for saving the data into database
    
    Args:
        tenant_id (str) -> the tenant name for which we have to take the database from. ex.invesco.acelive.ai
        database (str) -> database name. ex.extraction
        table (str) -> table name. ex.ocr
        case_id_based (bool) -> says whether we have to bring in all the data or only the data for a case_id.
        case_id (str) -> case_id for which we have to bring the data from the table.
        data (dict) -> column_value map or a record in the database.
    Returns:
        result (dict)
    Example:
        data1 = {'ocr':{'comments':'testing','assessable_value':1000}}
        save_data(tenant_id='deloitte.acelive.ai', database='extraction', table='None', data=data1, case_id='DEL754C18D_test', case_id_based = True, view='records')"""
    logging.info(f"tenant_id got is : {tenant_id}")
    logging.info(f"database got is : {database}")
    logging.info(f"table name got is : {table}")
    logging.info(f"data got is : {data}")
    logging.info(f"case_id got is : {case_id}")
    result = {}

    if case_id_based:
        logging.info(f"data to save is case_id based data.")
        try:

            db_config['tenant_id'] = tenant_id
            if database == 'extraction':
                extraction_db = DB(
                    'extraction', **
                    db_config)  # only in ocr or process_queue we are updating
            elif database == 'queues':
                queue_db = DB(
                    'queues', **
                    db_config)  # only in ocr or process_queue we are updating

            for table, colum_values in data.items():
                if table == 'ocr':
                    extraction_db.update(table,
                                         update=colum_values,
                                         where={'case_id': case_id})
                if table == 'process_queue':
                    queue_db.update(table,
                                    update=colum_values,
                                    where={'case_id': case_id})
        except Exception as e:
            logging.error(f"Cannot update the database")
            logging.error(e)
            result["flag"] = False,
            result['data'] = {
                'reason': f'Cannot update the database',
                'error_msg': str(e)
            }
            return result
        result['flag'] = True
        result['data'] = data
        return result

    else:
        logging.info(f"data to save is master based data.")
        try:
            db_config['tenant_id'] = tenant_id
            extraction_db = DB(
                'extraction',
                **db_config)  # only in ocr or process_queue we are updating
            queue_db = DB(
                'queues',
                **db_config)  # only in ocr or process_queue we are updating

            for table, colum_values in data.items():
                logging.info(
                    '************** have to develop due to where clause condition not getting from data *******'
                )
        except Exception as e:
            logging.error(f"Cannot update the database")
            logging.error(e)
            result['flag'] = False
            result['data'] = {
                'reason': f'Cannot update the database',
                'error_msg': str(e)
            }

        result['flag'] = True
        result['data'] = data
        return result