diff --git a/smart_service/apis/publish_api.py b/smart_service/apis/publish_api.py index 4512307..0a153ac 100644 --- a/smart_service/apis/publish_api.py +++ b/smart_service/apis/publish_api.py @@ -20,6 +20,7 @@ base_url = os.path.expanduser( internal_path = base_url + "/files/json_files/internal/" global_path = base_url + "/files/json_files/global/" full_update_path = base_url + "/files/json_files/full_update/" +full_update_path_internal = base_url + "/files/json_files/full_update/internal/" STATUS = "status" ERROR = "error" PARAM_MISSING = "Parameter missing :" @@ -245,8 +246,8 @@ def old_json_grouping(args, language): final.append(variant_out) dicfinal = {"JSON": final} - frappe.db.sql(""" UPDATE %s.tabPublish SET publish_status = "%s" where name = "%s" """ % ( - current_db_name, "Published", args)) + frappe.db.sql(""" UPDATE `tabPublish` SET publish_status = "%s" where name = "%s" """ % ( + "Published", args)) frappe.db.commit() return dicfinal @@ -254,6 +255,7 @@ def old_json_grouping(args, language): def json_grouping(args, language): try: dicfinal = [] + # frappe.log_error('args' + str(args)) docs_child = frappe.db.get_list("Publish_Docs", filters={"parent": args}, fields=["variant_mapping", "system", "sub_system", "procedure_status", "procedure_link"]) @@ -292,7 +294,7 @@ def json_grouping(args, language): variant_out = {} 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), - as_dict=True) + as_dict=True) var_asset = frappe.db.sql( """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 else: kms_active_status = False - - vari.update({"kms_mapping_active_status": kms_active_status}) + if kms_mapping_status: + vari.update({"kms_mapping_active_status": kms_active_status}) vari["Assets"] = var_asset variant_out["Variant"] = vari @@ -349,7 +351,7 @@ def json_grouping(args, language): sys["Assets"] = sysassets subsystem_out = [] - for j in subsystem[d][i]: + for j in subsystem[d][i]: subsys = frappe.db.sql("""select tmss.idx as subSystemdisplayorder, concat("{0}","-",systems) as sys_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 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) - + subsys = subsys[0] if subsys["active_status"] == "Active": @@ -404,12 +406,14 @@ def json_grouping(args, language): final.append(variant_out) dicfinal = {"JSON": final} - frappe.db.sql(""" UPDATE %s.tabPublish SET publish_status = "%s" where name = "%s" """ % ( - current_db_name, "Published", args)) + # frappe.log_error('dicfinal' + str(dicfinal)) + frappe.db.sql(""" UPDATE `tabPublish` SET publish_status = "%s" where name = "%s" """ % ( + "Published", args)) frappe.db.commit() + return dicfinal except Exception as e: - frappe.log_error('JSON Grouping',str(e)) + frappe.log_error('JSON Grouping', str(e)) """ New Publish """ @@ -431,12 +435,15 @@ def new_publish(args, publish_type, vehicle, language, version): os.makedirs(global_path + vehicle + "/") if not os.path.exists(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: return False, "Failed to create Folders" """ JSON generation """ json_file = json_grouping(args, language) + # frappe.log_error('json grouping' + str(json_file)) file_name = "" 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 else: return {STATUS: 0, ERROR: "Publish type not available"} + # frappe.log_error('created file name' + str(file_name)) """ Save file (Internal/Global) """ with open(file_name, "w") as outfile: outfile.write(json.dumps(json_file)) get_step_total_count(file_name) - """ Remove previous internal publish file """ - if publish_type.lower() == "internal": - for fil in os.listdir(internal_path + vehicle): - file_val = internal_path + vehicle + "/" + fil - if file_val != file_name: - if (fil.split("-")[1]).split("_")[0] == language: - os.remove(file_val) + # """ Remove previous internal publish file """ + # if publish_type.lower() == "internal": + # for fil in os.listdir(internal_path + vehicle): + # file_val = internal_path + vehicle + "/" + fil + # if file_val != file_name: + # if (fil.split("-")[1]).split("_")[0] == language: + # os.remove(file_val) """ Generate full update file """ pub_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": + # frappe.log_error(str("global")) full_update_file_name = full_update_path + vehicle + "/" + \ "%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: outfile.write(json.dumps(json_file)) get_step_total_count(full_update_file_name) + file_name1 = file_name + return True, file_name1.split("public")[1] else: - 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) + 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 work 2 ;""".format(vehicle, language), as_dict=1) if pub_ver: + prev_update_ver = pub_ver[1]["version"] prev_full_update_file = full_update_path + vehicle + "/" + \ "%s-%s-full_v%s.json" % (vehicle, - language, prev_update_ver) + 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) + 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" + + 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 except Exception as e: # 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()) -def set_publish_flag(publish_type,vehicle,language): +def set_publish_flag(publish_type, vehicle, language): try: if publish_type == 'Internal': # 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; ''') - if not vehicle_data: + if not vehicle_data: frappe.db.sql(f''' 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; ''') - if not lang_data: + if not lang_data: frappe.db.sql(f''' 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''' SELECT * FROM `tabCustom Languages` where lang_code = '{language}' and global_publish=1; ''') - - if not lang_data: + + if not lang_data: frappe.db.sql(f''' update `tabCustom Languages` set global_publish = 1 where lang_code = '{language}'; ''') frappe.db.commit() - + 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): @@ -563,7 +652,8 @@ def merge_json_files(old_json_path, new_json_path, out_file_path): data_new = json.load(json_file1) 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: var_dict = {} 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"] 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: 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"], 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) + var_dict["Variant"]["Systems"][old_sys_key]["Subsystems"].append( + s) else: 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 = [] json_object = [] data_new = [] - return True, out_file_path - + return True, out_file_path + 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): @@ -626,22 +718,23 @@ def check_key(key_name, old_data, type=None): if old_data[d]["Variant"]["name"] == key_name: return d except: - frappe.log_error("Check key System:",frappe.get_traceback()) - + frappe.log_error("Check key System:", frappe.get_traceback()) + if type == "System": try: if old_data[d]["sys_id"] == key_name: return d except: - frappe.log_error("Check key System:",frappe.get_traceback()) + frappe.log_error("Check key System:", frappe.get_traceback()) if type == "Sub System": try: if old_data[d]["sub_systems"] == key_name: return d except: - frappe.log_error("Check key subsystem:",frappe.get_traceback()) - + frappe.log_error("Check key subsystem:", + frappe.get_traceback()) + @frappe.whitelist(allow_guest=True) def new_update(vehicle_list=None): @@ -735,18 +828,18 @@ def calculate_total(data): return data -@frappe.whitelist() -def upv(): - # frappe.db.sql( - # f"""update `tabPublish` set version = 1.00 where name = "THAR-en -2023-05-21 16:57:54.119826";""") - # frappe.db.commit() - return 1 +# @frappe.whitelist() +# def upv(): +# # frappe.db.sql( +# # f"""update `tabPublish` set version = 1.00 where name = "THAR-en -2023-05-21 16:57:54.119826";""") +# # frappe.db.commit() +# return 1 """ Generate publish """ -@frappe.whitelist() -def run_publish(): - new_publish("THAR-en -2023-05-21 16:57:54.119826", - "global", "THAR", "en", "1.00") +# @frappe.whitelist() +# def run_publish(): +# new_publish("KUV test 1-en -2023-11-07 15:29:39.023917", +# "internal", "KUV test 1", "en", "1.010")