192 lines
8.3 KiB
Python
192 lines
8.3 KiB
Python
import datetime, json, requests, time
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query, Response, status
|
|
from fastapi.responses import FileResponse
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from sqlalchemy import text
|
|
|
|
from app.lib_general import log, logging, common_route_params, Common_Route_Params
|
|
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, run_sql_select
|
|
|
|
from app.ae_obj_types_def import *
|
|
from app.models.core_object_models import Core_Std_Obj_Base, Core_Object_Base
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Updated 2024-07-31
|
|
@router.get('/{obj_type}/convert')
|
|
def convert_obj_type(
|
|
obj_type: str = Path(min_length=2, max_length=50),
|
|
|
|
# for_obj_type: Optional[str] = Query(None, max_length=50),
|
|
# for_obj_id: Optional[str] = Query(None, max_length=22),
|
|
|
|
# tbl_alt: Optional[str] = Query('default', max_length=50), # This is used as a lookup for the real SQL database table or view name to use.
|
|
# mdl_alt: Optional[str] = Query('default', max_length=50), # This is used as a lookup for the real Python Pydantic model name to use.
|
|
# exp_alt: Optional[str] = Query('default', max_length=50), # This is used as a lookup for a list of column names to use.
|
|
|
|
# Get the "json" param from the query string. This is a JSON formatted string of the data to be inserted.
|
|
# jp: Optional[Union[str, None]] = None,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Get the field map for the object type
|
|
# # Check the URL to read the JSON formatted mapping data.
|
|
# url = 'https://dev-api.oneskyit.com/static/ae/obj_type_maps.json'
|
|
# # url = f'https://dev-api.oneskyit.com/static/ae/obj_type_map__{obj_type}.json'
|
|
# resp = requests.get(url=url, headers={'Content-Type': 'application/json', 'Cache-Control': 'no-cache'})
|
|
# log.debug(f'Status Code: {resp.status_code}')
|
|
# log.debug(f'Headers: {resp.headers}')
|
|
|
|
# obj_type_maps = resp.json()
|
|
|
|
# log.debug(obj_type_maps)
|
|
|
|
# if obj_type not in obj_type_maps:
|
|
|
|
obj_type_maps = {}
|
|
url = f'https://dev-api.oneskyit.com/static/ae/obj_type_map__{obj_type}.json'
|
|
resp = requests.get(url=url, headers={'Content-Type': 'application/json', 'Cache-Control': 'no-cache'})
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
if resp.status_code == 200:
|
|
obj_type_maps[obj_type] = resp.json()
|
|
else:
|
|
log.warning(f"Object type '{obj_type}' not found.")
|
|
raise HTTPException(status_code=404, detail=f"Object type '{obj_type}' not found.")
|
|
|
|
field_map = obj_type_maps[obj_type]
|
|
log.debug(field_map)
|
|
|
|
# ### SECTION ### Call generic function to convert the object type to the new model
|
|
result = handle_convert_obj_type(
|
|
obj_type=obj_type,
|
|
field_map=field_map,
|
|
)
|
|
|
|
data = {
|
|
'result_count': result.rowcount,
|
|
'result_id': result.lastrowid,
|
|
'obj_type': obj_type,
|
|
}
|
|
|
|
return mk_resp(data=data, response=commons.response)
|
|
|
|
|
|
# This will run the generated SQL statement based on the object type and field map provided.
|
|
# Updated 2024-07-31
|
|
def handle_convert_obj_type(
|
|
obj_type: str,
|
|
field_map: Dict[str, str]
|
|
) -> Dict[str, Union[str, int]]:
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Create the SQL statement to convert the object type to the new model
|
|
sql = text(f"""
|
|
INSERT INTO obj
|
|
(
|
|
`obj_id`, `obj_ext_uid`, `obj_ext_id`,
|
|
`obj_code`,
|
|
`obj_type`, `obj_type_ver_id`,
|
|
`obj_account_id`,
|
|
`obj_parent_type`, `obj_parent_id`,
|
|
`obj_passcode`,
|
|
`obj_name`, `obj_description`,
|
|
`obj_alert`, `obj_alert_msg`,
|
|
`obj_status`, `obj_approve`, `obj_approved_on`,
|
|
`obj_enable`, `obj_enable_on`, `obj_archive_on`,
|
|
`obj_restricted`, `obj_hide`, `obj_priority`, `obj_sort`, `obj_group`,
|
|
`obj_ver`, `obj_staff_notes`,
|
|
`obj_data_json`, `obj_cfg_json`, `obj_meta_json`, `obj_other_json`,
|
|
`obj_notes`, `obj_created_on`, `obj_updated_on`
|
|
)
|
|
SELECT
|
|
{field_map['obj_id']} AS `obj_id`,
|
|
{field_map['obj_ext_uid']} AS `obj_ext_uid`,
|
|
{field_map['obj_ext_id']} AS `obj_ext_id`,
|
|
{field_map['obj_code']} AS `obj_code`,
|
|
'{obj_type}' AS `obj_type`,
|
|
{field_map['obj_type_ver_id']} AS `obj_type_ver_id`,
|
|
{field_map['obj_account_id']} AS `obj_account_id`,
|
|
{field_map['obj_parent_type']} AS `obj_parent_type`,
|
|
{field_map['obj_parent_id']} AS `obj_parent_id`,
|
|
{field_map['obj_passcode']} AS `obj_passcode`,
|
|
{field_map['obj_name']} AS `obj_name`,
|
|
{field_map['obj_description']} AS `obj_description`,
|
|
{field_map['obj_alert']} AS `obj_alert`,
|
|
{field_map['obj_alert_msg']} AS `obj_alert_msg`,
|
|
{field_map['obj_status']} AS `obj_status`,
|
|
{field_map['obj_approve']} AS `obj_approve`,
|
|
{field_map['obj_approved_on']} AS `obj_approved_on`,
|
|
{field_map['obj_enable']} AS `obj_enable`,
|
|
{field_map['obj_enable_on']} AS `obj_enable_on`,
|
|
{field_map['obj_archive_on']} AS `obj_archive_on`,
|
|
{field_map['obj_restricted']} AS `obj_restricted`,
|
|
{field_map['obj_hide']} AS `obj_hide`,
|
|
{field_map['obj_priority']} AS `obj_priority`,
|
|
{field_map['obj_sort']} AS `obj_sort`,
|
|
{field_map['obj_group']} AS `obj_group`,
|
|
{field_map['obj_ver']} AS `obj_ver`,
|
|
{field_map['obj_staff_notes']} AS `obj_staff_notes`,
|
|
{field_map['obj_data_json']} AS `obj_data_json`,
|
|
{field_map['obj_cfg_json']} AS `obj_cfg_json`,
|
|
{field_map['obj_meta_json']} AS `obj_meta_json`,
|
|
{field_map['obj_other_json']} AS `obj_other_json`,
|
|
{field_map['obj_notes']} AS `obj_notes`,
|
|
{field_map['obj_created_on']} AS `obj_created_on`,
|
|
{field_map['obj_updated_on']} AS `obj_updated_on`
|
|
FROM
|
|
`{field_map['tbl_name']}`
|
|
ON DUPLICATE KEY UPDATE
|
|
`obj_id` = VALUES(`obj_id`),
|
|
`obj_ext_uid` = VALUES(`obj_ext_uid`),
|
|
`obj_ext_id` = VALUES(`obj_ext_id`),
|
|
`obj_code` = VALUES(`obj_code`),
|
|
`obj_type` = VALUES(`obj_type`),
|
|
`obj_type_ver_id` = VALUES(`obj_type_ver_id`),
|
|
`obj_account_id` = VALUES(`obj_account_id`),
|
|
`obj_parent_type` = VALUES(`obj_parent_type`),
|
|
`obj_parent_id` = VALUES(`obj_parent_id`),
|
|
`obj_passcode` = VALUES(`obj_passcode`),
|
|
`obj_name` = VALUES(`obj_name`),
|
|
`obj_description` = VALUES(`obj_description`),
|
|
`obj_alert` = VALUES(`obj_alert`),
|
|
`obj_alert_msg` = VALUES(`obj_alert_msg`),
|
|
`obj_status` = VALUES(`obj_status`),
|
|
`obj_approve` = VALUES(`obj_approve`),
|
|
`obj_approved_on` = VALUES(`obj_approved_on`),
|
|
`obj_enable` = VALUES(`obj_enable`),
|
|
`obj_enable_on` = VALUES(`obj_enable_on`),
|
|
`obj_archive_on` = VALUES(`obj_archive_on`),
|
|
`obj_restricted` = VALUES(`obj_restricted`),
|
|
`obj_hide` = VALUES(`obj_hide`),
|
|
`obj_priority` = VALUES(`obj_priority`),
|
|
`obj_sort` = VALUES(`obj_sort`),
|
|
`obj_group` = VALUES(`obj_group`),
|
|
`obj_ver` = VALUES(`obj_ver`),
|
|
`obj_staff_notes` = VALUES(`obj_staff_notes`),
|
|
`obj_data_json` = VALUES(`obj_data_json`),
|
|
`obj_cfg_json` = VALUES(`obj_cfg_json`),
|
|
`obj_meta_json` = VALUES(`obj_meta_json`),
|
|
`obj_other_json` = VALUES(`obj_other_json`),
|
|
`obj_notes` = VALUES(`obj_notes`),
|
|
`obj_created_on` = VALUES(`obj_created_on`),
|
|
`obj_updated_on` = VALUES(`obj_updated_on`)
|
|
;
|
|
""")
|
|
log.debug(sql)
|
|
# return False
|
|
|
|
# ### SECTION ### Execute the SQL statement
|
|
result = run_sql_select(sql, commit=True, log_lvl=logging.INFO)
|
|
log.info(result.rowcount)
|
|
log.debug(result.lastrowid)
|
|
|
|
return result
|