Browse Source

Publish API full update file save

master
venkataakhil 1 year ago
parent
commit
a00ff92570
  1. 151
      smart_service/apis/publish_api.py

151
smart_service/apis/publish_api.py

@ -20,6 +20,7 @@ base_url = os.path.expanduser(
internal_path = base_url + "/files/json_files/internal/" internal_path = base_url + "/files/json_files/internal/"
global_path = base_url + "/files/json_files/global/" global_path = base_url + "/files/json_files/global/"
full_update_path = base_url + "/files/json_files/full_update/" full_update_path = base_url + "/files/json_files/full_update/"
full_update_path_internal = base_url + "/files/json_files/full_update/internal/"
STATUS = "status" STATUS = "status"
ERROR = "error" ERROR = "error"
PARAM_MISSING = "Parameter missing :" PARAM_MISSING = "Parameter missing :"
@ -245,8 +246,8 @@ def old_json_grouping(args, language):
final.append(variant_out) final.append(variant_out)
dicfinal = {"JSON": final} dicfinal = {"JSON": final}
frappe.db.sql(""" UPDATE %s.tabPublish SET publish_status = "%s" where name = "%s" """ % ( frappe.db.sql(""" UPDATE `tabPublish` SET publish_status = "%s" where name = "%s" """ % (
current_db_name, "Published", args)) "Published", args))
frappe.db.commit() frappe.db.commit()
return dicfinal return dicfinal
@ -254,6 +255,7 @@ def old_json_grouping(args, language):
def json_grouping(args, language): def json_grouping(args, language):
try: try:
dicfinal = [] dicfinal = []
# frappe.log_error('args' + str(args))
docs_child = frappe.db.get_list("Publish_Docs", filters={"parent": args}, docs_child = frappe.db.get_list("Publish_Docs", filters={"parent": args},
fields=["variant_mapping", "system", "sub_system", "procedure_status", fields=["variant_mapping", "system", "sub_system", "procedure_status",
"procedure_link"]) "procedure_link"])
@ -315,7 +317,7 @@ def json_grouping(args, language):
kms_active_status = True kms_active_status = True
else: else:
kms_active_status = False kms_active_status = False
if kms_mapping_status:
vari.update({"kms_mapping_active_status": kms_active_status}) vari.update({"kms_mapping_active_status": kms_active_status})
vari["Assets"] = var_asset vari["Assets"] = var_asset
variant_out["Variant"] = vari variant_out["Variant"] = vari
@ -404,9 +406,11 @@ def json_grouping(args, language):
final.append(variant_out) final.append(variant_out)
dicfinal = {"JSON": final} dicfinal = {"JSON": final}
frappe.db.sql(""" UPDATE %s.tabPublish SET publish_status = "%s" where name = "%s" """ % ( # frappe.log_error('dicfinal' + str(dicfinal))
current_db_name, "Published", args)) frappe.db.sql(""" UPDATE `tabPublish` SET publish_status = "%s" where name = "%s" """ % (
"Published", args))
frappe.db.commit() frappe.db.commit()
return dicfinal return dicfinal
except Exception as e: except Exception as e:
frappe.log_error('JSON Grouping', str(e)) frappe.log_error('JSON Grouping', str(e))
@ -431,12 +435,15 @@ def new_publish(args, publish_type, vehicle, language, version):
os.makedirs(global_path + vehicle + "/") os.makedirs(global_path + vehicle + "/")
if not os.path.exists(full_update_path + vehicle + "/"): if not os.path.exists(full_update_path + vehicle + "/"):
os.makedirs(full_update_path + vehicle + "/") os.makedirs(full_update_path + vehicle + "/")
if not os.path.exists(full_update_path_internal + vehicle + "/"):
os.makedirs(full_update_path_internal + vehicle + "/")
except Exception as e: except Exception as e:
return False, "Failed to create Folders" return False, "Failed to create Folders"
""" JSON generation """ """ JSON generation """
json_file = json_grouping(args, language) json_file = json_grouping(args, language)
# frappe.log_error('json grouping' + str(json_file))
file_name = "" file_name = ""
base_file_name = "%s-%s_v%s.json" % (vehicle, language, version) base_file_name = "%s-%s_v%s.json" % (vehicle, language, version)
@ -447,24 +454,29 @@ def new_publish(args, publish_type, vehicle, language, version):
file_name = global_path + vehicle + "/" + base_file_name file_name = global_path + vehicle + "/" + base_file_name
else: else:
return {STATUS: 0, ERROR: "Publish type not available"} return {STATUS: 0, ERROR: "Publish type not available"}
# frappe.log_error('created file name' + str(file_name))
""" Save file (Internal/Global) """ """ Save file (Internal/Global) """
with open(file_name, "w") as outfile: with open(file_name, "w") as outfile:
outfile.write(json.dumps(json_file)) outfile.write(json.dumps(json_file))
get_step_total_count(file_name) get_step_total_count(file_name)
""" Remove previous internal publish file """ # """ Remove previous internal publish file """
if publish_type.lower() == "internal": # if publish_type.lower() == "internal":
for fil in os.listdir(internal_path + vehicle): # for fil in os.listdir(internal_path + vehicle):
file_val = internal_path + vehicle + "/" + fil # file_val = internal_path + vehicle + "/" + fil
if file_val != file_name: # if file_val != file_name:
if (fil.split("-")[1]).split("_")[0] == language: # if (fil.split("-")[1]).split("_")[0] == language:
os.remove(file_val) # os.remove(file_val)
""" Generate full update file """ """ Generate full update file """
pub_ver = None pub_ver = None
prev_update_ver = None prev_update_ver = None
check_if_global_exist = frappe.db.sql("""SELECT vehicle,language,format(version,2) as version,modified,publish_type FROM tabPublish where vehicle = "{}"
and publish_type = "Global" AND `language` = "{}" order by modified desc limit 1 ;""".format(vehicle, language), as_dict=1)
if publish_type.lower() == "global": if publish_type.lower() == "global":
# frappe.log_error(str("global"))
full_update_file_name = full_update_path + vehicle + "/" + \ full_update_file_name = full_update_path + vehicle + "/" + \
"%s-%s-full_v%s.json" % (vehicle, language, version) "%s-%s-full_v%s.json" % (vehicle, language, version)
@ -472,11 +484,14 @@ def new_publish(args, publish_type, vehicle, language, version):
with open(full_update_file_name, "w") as outfile: with open(full_update_file_name, "w") as outfile:
outfile.write(json.dumps(json_file)) outfile.write(json.dumps(json_file))
get_step_total_count(full_update_file_name) get_step_total_count(full_update_file_name)
file_name1 = file_name
return True, file_name1.split("public")[1]
else: else:
pub_ver = frappe.db.sql(""" SELECT vehicle,`language`,format(version,2) as version,modified,publish_type FROM tabPublish where vehicle = "{}" pub_ver = frappe.db.sql(""" SELECT vehicle,language,format(version,2) as version,modified,publish_type FROM tabPublish where vehicle = "{}"
and publish_type = "Global" AND `language` = "{}" order by modified desc limit 2 ;""".format(vehicle, language), as_dict=1) and publish_type = "Global" AND `language` = "{}" order by modified desc work 2 ;""".format(vehicle, language), as_dict=1)
if pub_ver: if pub_ver:
prev_update_ver = pub_ver[1]["version"] prev_update_ver = pub_ver[1]["version"]
prev_full_update_file = full_update_path + vehicle + "/" + \ prev_full_update_file = full_update_path + vehicle + "/" + \
"%s-%s-full_v%s.json" % (vehicle, "%s-%s-full_v%s.json" % (vehicle,
@ -492,7 +507,81 @@ def new_publish(args, publish_type, vehicle, language, version):
else: else:
return False, "File save issue" return False, "File save issue"
if publish_type.lower() == "internal" and len(check_if_global_exist) > 0:
# frappe.log_error(str('already global published'))
full_update_file_name = full_update_path_internal + vehicle + "/" + \
"%s-%s-full_v%s.json" % (vehicle, language, version)
# frappe.log_error('update file' + str(full_update_file_name))
pub_ver = frappe.db.sql(""" SELECT vehicle,language,format(version,2) as version,modified,publish_type FROM tabPublish where vehicle = "{}"
and `language` = "{}" order by modified desc limit 2 ;""".format(vehicle, language), as_dict=1)
if pub_ver:
# frappe.log_error('inside')
if pub_ver[1]['publish_type'] == 'Internal':
prev_update_ver_internal = float(pub_ver[1]["version"])
# frappe.log_error('version_internal' +
# str(prev_update_ver_internal))
prev_full_internal_update_file = full_update_path_internal + vehicle + "/" + \
"%s-%s-full_v%s.json" % (vehicle,
language, prev_update_ver_internal)
file_flag, final_update_file = merge_json_files(
prev_full_internal_update_file, file_name, full_update_file_name)
get_step_total_count(final_update_file)
set_publish_flag(publish_type, vehicle, language)
if file_flag:
return True, file_name.split("public")[1]
# return True, file_name
else:
return False, "File save issue"
elif pub_ver[1]['publish_type'] == 'Global':
try:
with open(full_update_file_name, "w") as outfile:
outfile.write(json.dumps(json_file))
get_step_total_count(full_update_file_name)
file_name1 = file_name
set_publish_flag(publish_type, vehicle, language) set_publish_flag(publish_type, vehicle, language)
file_flag = True
except Exception as e:
file_flag = False
if file_flag:
return True, file_name.split("public")[1]
# return True, file_name
else:
return False, "File save issue"
else:
frappe.log_error(str('first time ionternal'))
full_update_file_name = full_update_path_internal + vehicle + "/" + \
"%s-%s-full_v%s.json" % (vehicle, language, version)
if str(version) == "0.01":
frappe.log_error(str('first time ionternal file creation'))
with open(full_update_file_name, "w") as outfile:
outfile.write(json.dumps(json_file))
get_step_total_count(full_update_file_name)
else:
pub_ver = frappe.db.sql(""" SELECT vehicle,language,format(version,2) as version,modified,publish_type FROM tabPublish where vehicle = "{}"
and publish_type = "Internal" AND `language` = "{}" order by modified desc limit 2 ;""".format(vehicle, language), as_dict=1)
if pub_ver:
prev_update_ver = pub_ver[1]["version"]
prev_full_update_file = full_update_path_internal + vehicle + "/" + \
"%s-%s-full_v%s.json" % (vehicle,
language, prev_update_ver)
file_flag, final_update_file = merge_json_files(
prev_full_update_file, file_name, full_update_file_name)
get_step_total_count(final_update_file)
set_publish_flag(publish_type, vehicle, language)
if file_flag:
return True, file_name.split("public")[1]
# return True, file_name
else:
return False, "File save issue"
set_publish_flag(publish_type, vehicle, language)
frappe.log_error('file_name' + str(file_name))
return True, file_name.split("public")[1] return True, file_name.split("public")[1]
# return True, file_name # return True, file_name
@ -550,7 +639,7 @@ def set_publish_flag(publish_type,vehicle,language):
frappe.db.commit() frappe.db.commit()
except Exception as e: except Exception as e:
frappe.log_error(str(e)) frappe.log_error('set_publish_flag' + str(e))
def 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):
@ -563,7 +652,8 @@ def merge_json_files(old_json_path, new_json_path, out_file_path):
data_new = json.load(json_file1) data_new = json.load(json_file1)
for k in data_new["JSON"]: for k in data_new["JSON"]:
old_var_key = check_key(k["Variant"]["name"], data_old["JSON"], "Variant") old_var_key = check_key(
k["Variant"]["name"], data_old["JSON"], "Variant")
if old_var_key is not None and old_var_key >= 0: if old_var_key is not None and old_var_key >= 0:
var_dict = {} var_dict = {}
var_dict["Variant"] = k["Variant"] var_dict["Variant"] = k["Variant"]
@ -574,7 +664,8 @@ def merge_json_files(old_json_path, new_json_path, out_file_path):
var_dict["Variant"]["Assets"] = data_old["JSON"][old_var_key]["Variant"]["Assets"] var_dict["Variant"]["Assets"] = data_old["JSON"][old_var_key]["Variant"]["Assets"]
for i in variant_systems: for i in variant_systems:
old_sys_key = check_key(i["sys_id"], var_dict["Variant"]["Systems"], "System") 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: 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]["systemdisplayorder"] = i["systemdisplayorder"]
@ -592,7 +683,8 @@ def merge_json_files(old_json_path, new_json_path, out_file_path):
if old_sub_key is not None and old_sub_key >= 0: if old_sub_key is not None and old_sub_key >= 0:
var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"][old_sub_key] = s var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"][old_sub_key] = s
else: else:
var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"].append(s) var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"].append(
s)
else: else:
var_dict["Variant"]["Systems"].append(i) var_dict["Variant"]["Systems"].append(i)
@ -640,7 +732,8 @@ def check_key(key_name, old_data, type=None):
if old_data[d]["sub_systems"] == key_name: if old_data[d]["sub_systems"] == key_name:
return d return d
except: except:
frappe.log_error("Check key subsystem:",frappe.get_traceback()) frappe.log_error("Check key subsystem:",
frappe.get_traceback())
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
@ -735,18 +828,18 @@ def calculate_total(data):
return data return data
@frappe.whitelist() # @frappe.whitelist()
def upv(): # def upv():
# frappe.db.sql( # # frappe.db.sql(
# f"""update `tabPublish` set version = 1.00 where name = "THAR-en -2023-05-21 16:57:54.119826";""") # # f"""update `tabPublish` set version = 1.00 where name = "THAR-en -2023-05-21 16:57:54.119826";""")
# frappe.db.commit() # # frappe.db.commit()
return 1 # return 1
""" Generate publish """ """ Generate publish """
@frappe.whitelist() # @frappe.whitelist()
def run_publish(): # def run_publish():
new_publish("THAR-en -2023-05-21 16:57:54.119826", # new_publish("KUV test 1-en -2023-11-07 15:29:39.023917",
"global", "THAR", "en", "1.00") # "internal", "KUV test 1", "en", "1.010")

Loading…
Cancel
Save