From 8a4eed70e19d9c169c41b925b9fe06fec2e7ee24 Mon Sep 17 00:00:00 2001 From: venkataakhil Date: Thu, 13 Jul 2023 18:56:52 +0530 Subject: [PATCH] Publish Api Fix --- smart_service/apis/publish_api.py | 119 ++++++++++++++++-------------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/smart_service/apis/publish_api.py b/smart_service/apis/publish_api.py index 860e66c..b6a863f 100644 --- a/smart_service/apis/publish_api.py +++ b/smart_service/apis/publish_api.py @@ -8,6 +8,9 @@ import os from frappe.utils import cstr from smart_service.apis.app_user_login import input_validation import time +# from frappe.utils import logger +# frappe.utils.logger.set_log_level("DEBUG") + current_db_name = frappe.conf.get("db_name") site_name = cstr(frappe.local.site) @@ -766,71 +769,79 @@ def old_merge_json_files(old_json_path, new_json_path, out_file_path): except Exception as e: return False, frappe.get_traceback() -@frappe.whitelist(allow_guest=1) -def merge_json_files(old_json_path, new_json_path, out_file_path): - try: - final_data = {"JSON": []} - with open(old_json_path, "r") as json_file: - data_old = json.load(json_file) - with open(new_json_path, "r") as json_file1: - data_new = json.load(json_file1) - for k in data_new["JSON"]: - old_var_key = check_key(k["Variant"]["name"], data_old["JSON"], "Variant") - - if old_var_key is not None and old_var_key >= 0: - var_dict = {} - var_dict["Variant"] = k["Variant"] - variant_systems = k["Variant"]["Systems"] - var_dict["Variant"]["Systems"] = data_old["JSON"][old_var_key]["Variant"]["Systems"] - - if not k["Variant"]["Assets"]: - var_dict["Variant"]["Assets"] = data_old["JSON"][old_var_key]["Variant"]["Assets"] - - for i in variant_systems: - old_sys_key = check_key(i["sys_id"], var_dict["Variant"]["Systems"], "System") - - if old_sys_key is not None and old_sys_key >= 0: - var_dict["Variant"]["Systems"][old_sys_key]["systemdisplayorder"] = i["systemdisplayorder"] - var_dict["Variant"]["Systems"][old_sys_key]["icon_file"] = i["icon_file"] - var_dict["Variant"]["Systems"][old_sys_key]["active_status"] = i["active_status"] - - if i["Assets"]: - var_dict["Variant"]["Systems"][old_sys_key]["Assets"] = i["Assets"] - - for s in i["Subsystems"]: - old_sub_key = check_key(s["sub_systems"], - var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"], - "Sub System") - - if old_sub_key is not None and old_sub_key >= 0: - var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"][old_sub_key] = s - else: - var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"].append(s) - else: - var_dict["Variant"]["Systems"].append(i) +def merge_json_files(old_json_path, new_json_path, out_file_path): + # try: + final_data = {"JSON": []} + with open(old_json_path, "r") as json_file: + data_old = json.load(json_file) + # logger.debug('Old Json Path',str(old_json_path)) + + with open(new_json_path, "r") as json_file1: + data_new = json.load(json_file1) + + # logger.debug('New Json Path',str(new_json_path)) + + for k in data_new["JSON"]: + old_var_key = check_key(k["Variant"]["name"], data_old["JSON"], "Variant") + # logger.debug('Old Variant',str(old_var_key)) + + if old_var_key is not None and old_var_key >= 0: + var_dict = {} + var_dict["Variant"] = k["Variant"] + # logger.debug("variant dict",str(var_dict["Variant"])) + variant_systems = k["Variant"]["Systems"] + var_dict["Variant"]["Systems"] = data_old["JSON"][old_var_key]["Variant"]["Systems"] + + if not k["Variant"]["Assets"]: + var_dict["Variant"]["Assets"] = data_old["JSON"][old_var_key]["Variant"]["Assets"] + + for i in variant_systems: + old_sys_key = check_key(i["sys_id"], var_dict["Variant"]["Systems"], "System") + + if old_sys_key is not None and old_sys_key >= 0: + var_dict["Variant"]["Systems"][old_sys_key]["systemdisplayorder"] = i["systemdisplayorder"] + var_dict["Variant"]["Systems"][old_sys_key]["icon_file"] = i["icon_file"] + var_dict["Variant"]["Systems"][old_sys_key]["active_status"] = i["active_status"] + + if i["Assets"]: + var_dict["Variant"]["Systems"][old_sys_key]["Assets"] = i["Assets"] + + for s in i["Subsystems"]: + old_sub_key = check_key(s["sub_systems"], + var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"], + "Sub System") + + if old_sub_key is not None and old_sub_key >= 0: + var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"][old_sub_key] = s + else: + var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"].append(s) + else: + var_dict["Variant"]["Systems"].append(i) - time.sleep(0.100) - data_old["JSON"][old_var_key] = var_dict - final_data["JSON"].append(var_dict) + data_old["JSON"][old_var_key] = var_dict + final_data["JSON"].append(var_dict) - else: - data_old["JSON"].append(k) + else: + data_old["JSON"].append(k) - json_object = json.dumps(data_old, indent=4) - with open(out_file_path, "w") as outfile: - outfile.write(json_object) - - return True, out_file_path - except: - return False, frappe.log_error("Merge JSON:",frappe.get_traceback()) + json_object = json.dumps(data_old, indent=4) + with open(out_file_path, "w") as outfile: + outfile.write(json_object) + + return True, out_file_path + # except: + # return False, frappe.log_error("Merge JSON:",frappe.get_traceback()) def check_key(key_name, old_data, type=None): old_file_length = len(old_data) for d in range(0, old_file_length): + if type == "Variant": try: + # logger.debug('Variant check',str(old_data[d]["Variant"]["name"])) + # logger.debug('Variant key check',str(key_name)) if old_data[d]["Variant"]["name"] == key_name: return d except: