Browse Source

Publish prev version fix

master
venkataakhil 1 year ago
parent
commit
79be2cf402
  1. 324
      smart_service/apis/publish_api.py

324
smart_service/apis/publish_api.py

@ -552,286 +552,70 @@ def set_publish_flag(publish_type,vehicle,language):
except Exception as e:
frappe.log_error(str(e))
@frappe.whitelist()
def old_merge_json_files(old_json_path, new_json_path, out_file_path):
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)
data_consolidated = {"JSON": []}
variant_name = set()
sub_system_name = set()
variant_added = set()
for variant in data_old["JSON"]:
variant_name.add(variant["Variant"]["name"])
for variant in data_new["JSON"]:
variant_name.add(variant["Variant"]["name"])
for variant_new in data_new["JSON"]:
if variant_new["Variant"]["name"] in variant_name and variant_new["Variant"]["name"] not in variant_added:
system_name = set()
system_id={}
system_added = set()
system_list = []
variant_asset_name = set()
variant_asset_added = set()
variant_asset_list = []
try:
# variant_new["Variant"]["Assets"]
for variant_asset in variant_new["Variant"]["Assets"]:
variant_asset_name.add(variant_asset["file"])
if variant_asset["file"] not in variant_asset_added and variant_asset[
"file"] in variant_asset_name:
variant_asset_list.append(variant_asset)
variant_asset_added.add(variant_asset["file"])
except:
pass
try:
# variant_new["Variant"]["Systems"]
for system in variant_new["Variant"]["Systems"]:
system_asset_name = set()
system_asset_added = set()
system_asset_list = []
system_name.add(system["system_name"])
# system_name.add(system[""])
if system["system_name"] not in system_added and system["system_name"] in system_name:
sub_system_name = set()
sub_system_added = set()
sub_system_list = []
for subsystem in system["Subsystems"]:
sub_system_name.add(subsystem["sub_systems"])
if subsystem["sub_systems"] not in sub_system_added and subsystem["sub_systems"] in sub_system_name:
conf_km_name = set()
conf_km_added = set()
conf_km_list = []
for ck in subsystem["Config Kilometer"]:
conf_km_name.add(ck["kilometer_name"])
if ck["kilometer_name"] not in conf_km_added and ck["kilometer_name"] in conf_km_name:
conf_km_list.append({
"kilometer_name": ck["kilometer_name"],
"kilometer_IDX": ck["kilometer_IDX"],
"kilometers_applicable": ck["kilometers_applicable"]
})
conf_km_added.add(
ck["kilometer_name"])
for old_variants in data_old["JSON"]:
if old_variants["Variant"]["name"] == variant_new["Variant"]["name"]:
for old_system in old_variants["Variant"]["Systems"]:
if old_system["system_name"] == system["system_name"]:
for old_sub_system in old_system["Subsystems"]:
if old_sub_system["sub_systems"] == subsystem["sub_systems"]:
for old_ck in old_sub_system["Config Kilometer"]:
conf_km_name.add(
old_ck["kilometer_name"])
if old_ck["kilometer_name"] not in conf_km_added and \
old_ck["kilometer_name"] in conf_km_name:
# sub_system_list.append(
# old_ck)
conf_km_list.append(
old_ck)
conf_km_added.add(
old_ck["kilometer_name"])
proc_det_name = set()
proc_det_added = set()
proc_det_list = []
for pd in subsystem["Procedure_details"]:
proc_det_name.add(pd["procedure_name"])
if pd["procedure_name"] not in proc_det_added and pd[
"procedure_name"] in proc_det_name:
proc_det_list.append({
"procedure_name": pd["procedure_name"],
"steps": pd["steps"],
})
proc_det_added.add(
pd["procedure_name"])
for old_variants in data_old["JSON"]:
if old_variants["Variant"]["name"] == variant_new["Variant"]["name"]:
for old_system in old_variants["Variant"]["Systems"]:
if old_system["system_name"] == system["system_name"]:
for old_sub_system in old_system["Subsystems"]:
if old_sub_system["sub_systems"] == subsystem["sub_systems"]:
for old_pd in old_sub_system["Procedure_details"]:
# proc_det_name.add(
# old_pd["kilometer_name"])
if old_pd["procedure_name"] not in proc_det_added and \
old_pd["procedure_name"] in proc_det_name:
sub_system_list.append(
old_pd)
proc_det_added.add(
old_pd["kilometer_name"])
sub_system_list.append({"subSystemdisplayorder": subsystem["subSystemdisplayorder"],
"systems": subsystem["systems"],
"sub_systems": subsystem["sub_systems"],
"symptom": subsystem["symptom"],
"component": subsystem["component"],
"estimated_time": subsystem["estimated_time"],
"rts": subsystem["rts"],
"mat": subsystem["mat"],
"cover_image": subsystem["cover_image"],
"myid": subsystem["myid"],
"active_status": subsystem["active_status"],
"Config Kilometer": conf_km_list,
"procedure_status": subsystem["procedure_status"],
"procedure_link": subsystem["procedure_link"],
"Procedure_details": proc_det_list,
})
sub_system_added.add(
subsystem["sub_systems"])
for old_variants in data_old["JSON"]:
if old_variants["Variant"]["name"] == variant_new["Variant"]["name"]:
for old_system in old_variants["Variant"]["Systems"]:
if old_system["system_name"] == system["system_name"]:
for old_sub_system in old_system["Subsystems"]:
sub_system_name.add(
old_sub_system["sub_systems"])
if old_sub_system["sub_systems"] not in sub_system_added and \
old_sub_system["sub_systems"] in sub_system_name:
sub_system_list.append(
old_sub_system)
sub_system_added.add(
old_sub_system["sub_systems"])
for asset in system["Assets"]:
system_asset_name.add(asset["system_asset"])
if asset["system_asset"] not in system_asset_added and asset[
"system_asset"] in system_asset_name:
system_asset_list.append({"system_asset": asset["system_asset"],
"systemdisplayorder": asset["systemdisplayorder"],
"active_status": asset["active_status"],
})
system_asset_added.add(
asset["system_asset"])
for old_variants in data_old["JSON"]:
if old_variants["Variant"]["name"] == variant_new["Variant"]["name"]:
for old_system in old_variants["Variant"]["Systems"]:
if old_system["system_name"] == system["system_name"]:
for old_system_asset in old_system["Assets"]:
system_asset_name.add(
old_system_asset["system_asset"])
if old_system_asset["system_asset"] not in system_asset_added and \
old_system_asset["system_asset"] in system_asset_name:
system_asset_list.append(
old_system_asset)
system_asset_added.add(
old_system_asset["system_asset"])
system_list.append({"systemdisplayorder": system["systemdisplayorder"],
"system_name": system["system_name"],
"icon_file": system["icon_file"],
"myid": system["myid"],
"active_status": system["active_status"],
"Assets": system_asset_list,
"Subsystems": sub_system_list,
# "Config Kilometer": conf_km_list,
})
system_added.add(system["system_name"])
except:
pass
for old_variants in data_old["JSON"]:
if old_variants["Variant"]["name"] == variant_new["Variant"]["name"]:
try:
old_variants["Variant"]["Systems"]
for system in old_variants["Variant"]["Systems"]:
system_name.add(system["system_name"])
if system["system_name"] not in system_added and system["system_name"] in system_name:
system_list.append(system)
system_added.add(system["system_name"])
except:
pass
data_consolidated["JSON"].append({"Variant": {"name": variant_new["Variant"]["name"],
"variant": variant_new["Variant"]["variant"],
"vehicle": variant_new["Variant"]["vehicle"],
"family_code": variant_new["Variant"]["family_code"],
"vehicle_segment": variant_new["Variant"][
"vehicle_segment"],
"fuel": variant_new["Variant"]["fuel"],
"transmission": variant_new["Variant"]["transmission"],
"drive": variant_new["Variant"]["drive"],
"active_status": variant_new["Variant"]["active_status"],
"kms_mapping_active_status": variant_new["Variant"][
"kms_mapping_active_status"],
"Assets": variant_asset_list,
"Systems": system_list
}})
variant_added.add(variant_new["Variant"]["name"])
for variant_old in data_old["JSON"]:
if variant_old["Variant"]["name"] in variant_name and variant_old["Variant"]["name"] not in variant_added:
data_consolidated["JSON"].append({"Variant": variant_old["Variant"],
})
with open(out_file_path, "w") as outfile:
outfile.write(json.dumps(data_consolidated, indent=4))
return True, out_file_path
except Exception as e:
return False, frappe.get_traceback()
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)
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)
var_dict = {}
final_data = []
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)
# Clear data to check
data_old = []
json_object = []
data_new = []
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):

Loading…
Cancel
Save