Browse Source

Publish API full update file save

master
venkataakhil 11 months ago
parent
commit
a00ff92570
  1. 197
      smart_service/apis/publish_api.py

197
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"])
@ -292,7 +294,7 @@ def json_grouping(args, language):
variant_out = {} variant_out = {}
vari = frappe.db.sql("""SELECT name,variant, vehicle,family_code,vehicle_segment,fuel,transmission,drive, active_status vari = frappe.db.sql("""SELECT name,variant, vehicle,family_code,vehicle_segment,fuel,transmission,drive, active_status
FROM %s.`tabVariant Mapping` WHERE name ="%s" ; """ % (current_db_name, d), FROM %s.`tabVariant Mapping` WHERE name ="%s" ; """ % (current_db_name, d),
as_dict=True) as_dict=True)
var_asset = frappe.db.sql( var_asset = frappe.db.sql(
"""SELECT name,category, attach_file as file, %s FROM %s.`tabVariant Mapping_Assets` where category<>"Technical Manual" """SELECT name,category, attach_file as file, %s FROM %s.`tabVariant Mapping_Assets` where category<>"Technical Manual"
@ -315,8 +317,8 @@ 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
@ -349,7 +351,7 @@ def json_grouping(args, language):
sys["Assets"] = sysassets sys["Assets"] = sysassets
subsystem_out = [] subsystem_out = []
for j in subsystem[d][i]: for j in subsystem[d][i]:
subsys = frappe.db.sql("""select tmss.idx as subSystemdisplayorder, subsys = frappe.db.sql("""select tmss.idx as subSystemdisplayorder,
concat("{0}","-",systems) as sys_id, concat("{0}","-",systems) as sys_id,
concat("{0}","-",systems,"-",sub_systems) as subsys_id, concat("{0}","-",systems,"-",sub_systems) as subsys_id,
@ -358,7 +360,7 @@ def json_grouping(args, language):
from {4}.`tabSystem Mapping_Sub System` tmss inner join {4}.`tabSub Systems` ts on tmss.sub_systems = ts.name from {4}.`tabSystem Mapping_Sub System` tmss inner join {4}.`tabSub Systems` ts on tmss.sub_systems = ts.name
where tmss.parent like "{0}-{3}%" and systems="{1}" and sub_systems="{2}";""".format( where tmss.parent like "{0}-{3}%" and systems="{1}" and sub_systems="{2}";""".format(
d, i, j, lang["language"], current_db_name), as_dict=1) d, i, j, lang["language"], current_db_name), as_dict=1)
subsys = subsys[0] subsys = subsys[0]
if subsys["active_status"] == "Active": if subsys["active_status"] == "Active":
@ -404,12 +406,14 @@ 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))
""" New Publish """ """ New Publish """
@ -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,37 +484,114 @@ 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,
language, prev_update_ver) language, prev_update_ver)
file_flag, final_update_file = merge_json_files( file_flag, final_update_file = merge_json_files(
prev_full_update_file, file_name, full_update_file_name) prev_full_update_file, file_name, full_update_file_name)
get_step_total_count(final_update_file) get_step_total_count(final_update_file)
set_publish_flag(publish_type,vehicle,language) set_publish_flag(publish_type, vehicle, language)
if file_flag: if file_flag:
return True, file_name.split("public")[1] return True, file_name.split("public")[1]
# return True, file_name # return True, file_name
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)
file_flag = True
except Exception as e:
file_flag = False
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"
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
except Exception as e: except Exception as e:
# return "Failed to save file" # return "Failed to save file"
frappe.log_error("Publish",frappe.get_traceback()) frappe.log_error("Publish", frappe.get_traceback())
return False, str(frappe.get_traceback()) return False, str(frappe.get_traceback())
def set_publish_flag(publish_type,vehicle,language): def set_publish_flag(publish_type, vehicle, language):
try: try:
if publish_type == 'Internal': if publish_type == 'Internal':
# Update vehicle master field # Update vehicle master field
@ -510,7 +599,7 @@ def set_publish_flag(publish_type,vehicle,language):
SELECT * FROM tabVehicle where vehicle = '{vehicle}' and internal_publish=1; SELECT * FROM tabVehicle where vehicle = '{vehicle}' and internal_publish=1;
''') ''')
if not vehicle_data: if not vehicle_data:
frappe.db.sql(f''' frappe.db.sql(f'''
update tabVehicle set internal_publish = 1 where vehicle = '{vehicle}'; update tabVehicle set internal_publish = 1 where vehicle = '{vehicle}';
''') ''')
@ -521,7 +610,7 @@ def set_publish_flag(publish_type,vehicle,language):
SELECT * FROM `tabCustom Languages` where lang_code = '{language}' and internal_publish=1; SELECT * FROM `tabCustom Languages` where lang_code = '{language}' and internal_publish=1;
''') ''')
if not lang_data: if not lang_data:
frappe.db.sql(f''' frappe.db.sql(f'''
update `tabCustom Languages` set internal_publish = 1 where lang_code = '{language}'; update `tabCustom Languages` set internal_publish = 1 where lang_code = '{language}';
''') ''')
@ -542,15 +631,15 @@ def set_publish_flag(publish_type,vehicle,language):
lang_data = frappe.db.sql(f''' lang_data = frappe.db.sql(f'''
SELECT * FROM `tabCustom Languages` where lang_code = '{language}' and global_publish=1; SELECT * FROM `tabCustom Languages` where lang_code = '{language}' and global_publish=1;
''') ''')
if not lang_data: if not lang_data:
frappe.db.sql(f''' frappe.db.sql(f'''
update `tabCustom Languages` set global_publish = 1 where lang_code = '{language}'; update `tabCustom Languages` set global_publish = 1 where lang_code = '{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"]
@ -588,11 +679,12 @@ def merge_json_files(old_json_path, new_json_path, out_file_path):
old_sub_key = check_key(s["sub_systems"], old_sub_key = check_key(s["sub_systems"],
var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"], var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"],
"Sub System") "Sub System")
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)
@ -611,10 +703,10 @@ def merge_json_files(old_json_path, new_json_path, out_file_path):
data_old = [] data_old = []
json_object = [] json_object = []
data_new = [] data_new = []
return True, out_file_path return True, out_file_path
except: except:
return False, frappe.log_error("Merge JSON:",frappe.get_traceback()) return False, frappe.log_error("Merge JSON:", frappe.get_traceback())
def check_key(key_name, old_data, type=None): def check_key(key_name, old_data, type=None):
@ -626,22 +718,23 @@ def check_key(key_name, old_data, type=None):
if old_data[d]["Variant"]["name"] == key_name: if old_data[d]["Variant"]["name"] == key_name:
return d return d
except: except:
frappe.log_error("Check key System:",frappe.get_traceback()) frappe.log_error("Check key System:", frappe.get_traceback())
if type == "System": if type == "System":
try: try:
if old_data[d]["sys_id"] == key_name: if old_data[d]["sys_id"] == key_name:
return d return d
except: except:
frappe.log_error("Check key System:",frappe.get_traceback()) frappe.log_error("Check key System:", frappe.get_traceback())
if type == "Sub System": if type == "Sub System":
try: try:
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)
def new_update(vehicle_list=None): def new_update(vehicle_list=None):
@ -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