From d7dd6812564718642e2c0232c76778c19ef8e0de Mon Sep 17 00:00:00 2001 From: venkataakhil Date: Wed, 21 Jun 2023 19:17:57 +0530 Subject: [PATCH] Publish: Display order fix --- smart_service/apis/publish_api.py | 455 +++++++++--------- .../doctype/procedure/procedure.py | 2 +- .../transactions/doctype/publish/publish.py | 2 +- .../doctype/system_mapping/system_mapping.py | 4 +- 4 files changed, 244 insertions(+), 219 deletions(-) diff --git a/smart_service/apis/publish_api.py b/smart_service/apis/publish_api.py index e191b05..eb71ccf 100644 --- a/smart_service/apis/publish_api.py +++ b/smart_service/apis/publish_api.py @@ -88,7 +88,7 @@ def api_procedure(args, publish_type, vehicle, language, version): return "Failed to save file" -def json_grouping(args, language): +def json_grouping1(args, language): dicfinal = [] docs_child = frappe.db.get_list("Publish_Docs", filters={"parent": args}, fields=["variant_mapping", "system", "sub_system", "procedure_status", @@ -172,7 +172,8 @@ def json_grouping(args, language): sys["active_status"] = False sysassets = frappe.db.sql( - """SELECT idx as systemdisplayorder, system_asset,active_status FROM {3}.`tabSystem Mapping_System Assets` where parent like "{0}%" and language="{1}" and systems="{2}";""".format( + """SELECT idx as systemdisplayorder, system_asset,active_status FROM {3}.`tabSystem Mapping_System Assets` + where parent like "{0}%" and language="{1}" and systems="{2}";""".format( d, lang["language"], i, current_db_name), as_dict=True) total_count += len(sysassets) @@ -246,14 +247,172 @@ def json_grouping(args, language): return dicfinal +def json_grouping(args, language): + try: + dicfinal = [] + docs_child = frappe.db.get_list("Publish_Docs", filters={"parent": args}, + fields=["variant_mapping", "system", "sub_system", "procedure_status", + "procedure_link"]) + + lang = {"language": language} + variant = set() + system = {} + subsystem = {} + + for i in docs_child: + variant.add(i["variant_mapping"]) + + for i in variant: + temp = set() + for j in docs_child: + if j["variant_mapping"] == i: + temp.add(j["system"]) + system[i] = temp + + for i in variant: + temp_set = {} + for j in system[i]: + temp = set() + for k in docs_child: + if i == k["variant_mapping"] and j == k["system"]: + temp.add(k["sub_system"]) + temp_set[j] = temp + subsystem[i] = temp_set + + final = [] + count = 0 + active_status_case = """CASE WHEN active_status = "Active" THEN "True" ELSE "False" END AS active_status""" + + for d in variant: + total_count = 0 + 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) + + var_asset = frappe.db.sql( + """SELECT name,category, attach_file as file, %s FROM %s.`tabVariant Mapping_Assets` where category<>"Technical Manual" + and parent="%s" and language="%s";""" % (active_status_case, current_db_name, d, lang["language"]), as_dict=True) + + vari = vari[0] + + if vari["active_status"] == "Active": + vari["active_status"] = True + else: + vari["active_status"] = False + kms_mapping_status = None + kms_mapping_status = frappe.db.sql( + """SELECT active_status FROM {0}.`tabKilometer Mapping` where name LIKE "{1}" and substring(name,-5,2)= "{2}";""" + .format(current_db_name, d + "%", language)) + + if kms_mapping_status: + kms_mapping_status = kms_mapping_status[0][0] + if kms_mapping_status == "Active": + kms_active_status = True + else: + kms_active_status = False + + vari.update({"kms_mapping_active_status": kms_active_status}) + vari["Assets"] = var_asset + variant_out["Variant"] = vari + + total_count += len(var_asset) + system_out = [] + for i in system[d]: + if i != None: + sys = frappe.db.sql(""" SELECT min(tmss.idx) as systemdisplayorder,concat(tmss.parent,"-",ts.system_name) as sys_id, + ts.system_name,ts.icon_file,ts.myid,ts.active_status FROM + {0}.`tabSystem Mapping_Sub System` as tmss inner join {0}.`tabSystems` as ts on tmss.systems = ts.system_name + where tmss.parent LIKE "{1}" and substring(tmss.parent,-5,2) = "{2}" and tmss.systems ="{3}";""" + .format(current_db_name, str(vari["name"] + "%"), language, i), as_dict=True) + sys = sys[0] + if sys["active_status"] == "Active": + sys["active_status"] = True + else: + sys["active_status"] = False + + sysassets = frappe.db.sql( + """SELECT idx as systemdisplayorder, system_asset,active_status + FROM {3}.`tabSystem Mapping_System Assets` where parent like "{0}%" and language="{1}" and systems="{2}";""".format( + d, lang["language"], i, current_db_name), as_dict=True) + total_count += len(sysassets) + + for sysasset in sysassets: + if sysasset["active_status"] == "Active": + sysasset["active_status"] = True + else: + sysasset["active_status"] = False + sys["Assets"] = sysassets + subsystem_out = [] + + 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, + systems,sub_systems, symptom, component,estimate_time as estimated_time, rts, mat, cover_image, ts.myid, + tmss.active_status,tmss.cover_image + 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": + subsys["active_status"] = True + else: + subsys["active_status"] = False + varKMS = vari["name"] + "-" + lang["language"] + "%" + + kms = frappe.db.sql( + """SELECT kilometer as kilometer_name, idx as kilometer_IDX, applicable as kilometers_applicable + FROM %s.`tabKilometer Mapping_Items` where sub_systems="%s" and parent LIKE "%s" order by kilometer_IDX;""" % ( + current_db_name, j, varKMS), as_dict=True) + total_count += len(kms) + subsys["Config Kilometer"] = kms + for k in docs_child: + if k["variant_mapping"] == d and k["system"] == i and k["sub_system"] == j: + proc_details = frappe.db.sql("""select procedure_name, step_name, content_type, + case when content_type="Link" Then system_id else content end as content, + file,idx as display_order,idx from {}.tabProcedure_Details + where parent ="{}" order by idx asc; """.format(current_db_name, k["procedure_link"]), as_dict=True) + + proc_data = get_parent_map( + proc_details, "procedure_name") + total_count += len(proc_data) + k["Procedure_details"] = proc_data + subsys["procedure_status"] = k["procedure_status"] + subsys["procedure_link"] = k["procedure_link"] + subsys["Procedure_details"] = k["Procedure_details"] + + stepcount = frappe.db.sql( + """SELECT count(*) as Stepcount FROM {1}.tabProcedure_Details where parent="{0}";""".format( + k["procedure_link"], current_db_name), as_dict=True) + stepcount = stepcount[0] + count = count + int(stepcount["Stepcount"]) + + subsystem_out.append(subsys) + + sys["Subsystems"] = subsystem_out + system_out.append(sys) + variant_out["StepCount"] = count + variant_out["TotalCount"] = total_count + count + variant_out["Variant"]["Systems"] = system_out + + 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.commit() + return dicfinal + except Exception as e: + frappe.log_error('JSON Grouping',str(e)) + + """ New Publish """ @frappe.whitelist() def new_publish(args, publish_type, vehicle, language, version): - # rate_res = custom_rate_limit(limit=5, seconds=15) - # if rate_res != 1: - # return rate_res val = input_validation(args=args, publish_type=publish_type, vehicle=vehicle, language=language, version=version) if val != "": @@ -270,7 +429,7 @@ def new_publish(args, publish_type, vehicle, language, version): os.makedirs(full_update_path + vehicle + "/") except Exception as e: - return {STATUS: 0, ERROR: "Failed to create Folders "} + return False, "Failed to create Folders" """ JSON generation """ json_file = json_grouping(args, language) @@ -305,42 +464,39 @@ def new_publish(args, publish_type, vehicle, language, version): full_update_file_name = full_update_path + vehicle + "/" + \ "%s-%s-full_v%s.json" % (vehicle, language, version) - # new_full_update_file_name = full_update_path + vehicle + "/" + \ - # "%s-%s-full_update.json" % (vehicle, language) - """ Since full update is not available for Global version 1 """ - if str(version) == "1.00": with open(full_update_file_name, "w") as outfile: outfile.write(json.dumps(json_file)) - # with open(new_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,publish_type FROM tabPublish where vehicle = "{}" - and publish_type = "Global" AND `language` = "{}" order by version desc limit 2 ;""".format(vehicle, language), as_dict=True) + and publish_type = "Global" AND `language` = "{}" order by version 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 + 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) if file_flag: - return True, file_name.split("public")[1] + # return True, file_name.split("public")[1] + return True, file_name else: return False, "File save issue" - return True, file_name.split("public")[1] + # return True, file_name.split("public")[1] + return True, file_name except Exception as e: # return "Failed to save file" - return False, str(frappe.get_traceback()) + # frappe.log_error("Publish",frappe.get_traceback()) + return False, str(frappe.get_traceback(e)) @frappe.whitelist() -def merge_json_files(old_json_path, new_json_path, out_file_path): +def old_merge_json_files(old_json_path, new_json_path, out_file_path): try: with open(old_json_path, "r") as json_file: data_old = json.load(json_file) @@ -383,7 +539,7 @@ def merge_json_files(old_json_path, new_json_path, out_file_path): system_asset_added = set() system_asset_list = [] system_name.add(system["system_name"]) - # system_name.add(system['']) + # 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() @@ -557,214 +713,83 @@ def merge_json_files(old_json_path, new_json_path, out_file_path): return False, frappe.get_traceback() -@frappe.whitelist() -def new_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": []} + 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["system_name"], 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) - 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"]) + data_old["JSON"][old_var_key] = var_dict + final_data["JSON"].append(var_dict) - 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_added = set() - system_list = [] - variant_asset_name = set() - variant_asset_added = set() - variant_asset_list = [] - try: - 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: - for system in variant_new["Variant"]["Systems"]: - system_asset_name = set() - system_asset_added = set() - system_asset_list = [] - system_name.add(system["system_name"]) - 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_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"]: - 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 - }) - 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 + else: + data_old["JSON"].append(k) - 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"], - }) + json_object = json.dumps(data_old, indent=4) with open(out_file_path, "w") as outfile: - outfile.write(json.dumps(data_consolidated, indent=4)) + outfile.write(json_object) + + except Exception as es: + frappe.log_error("Merge JSON:",frappe.get_traceback()) - return True, out_file_path - except Exception as e: - return False, 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": + if old_data[d]["Variant"]["name"] == key_name: + return d + + if type == "System": + try: + if old_data[d]["system_name"] == key_name: + return d + except Exception as e: + 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 Exception as e2: + frappe.log_error("Check key subsystem:",frappe.get_traceback()) + @frappe.whitelist(allow_guest=True) def new_update(vehicle_list=None): diff --git a/smart_service/transactions/doctype/procedure/procedure.py b/smart_service/transactions/doctype/procedure/procedure.py index 1bee137..3403977 100644 --- a/smart_service/transactions/doctype/procedure/procedure.py +++ b/smart_service/transactions/doctype/procedure/procedure.py @@ -37,7 +37,7 @@ class Procedure(Document): if not self.is_new(): frappe.db.sql( - f"""UPDATE `tabSystem Mapping_Sub System` set procedure_is_published=0 where `procedure`='{self.name}';""") + f"""UPDATE `tabSystem Mapping_Sub System` set procedure_is_published=0,procedure_status='{self.workflow_state}',active_status='{self.active_status}' where `procedure`='{self.name}';""") frappe.db.commit() diff --git a/smart_service/transactions/doctype/publish/publish.py b/smart_service/transactions/doctype/publish/publish.py index 379220d..8a9771d 100644 --- a/smart_service/transactions/doctype/publish/publish.py +++ b/smart_service/transactions/doctype/publish/publish.py @@ -304,7 +304,7 @@ def get_system(doc, name,lang): for s in system_map: doc = frappe.get_doc("System Mapping", s.name) system = frappe.db.sql("""select systems,sub_systems,active_status,symptom,component,estimate_time,rts,component,mat, - procedure_status,`procedure` from {0}.`tabSystem Mapping_Sub System` where parent = "{1}" and + procedure_status,`procedure` from {0}.`tabSystem Mapping_Sub System` where parent = "{1}" and procedure_status != "Draft" and procedure_is_published = "0";""".format(current_db_name, s.name), as_dict=1) return system diff --git a/smart_service/transactions/doctype/system_mapping/system_mapping.py b/smart_service/transactions/doctype/system_mapping/system_mapping.py index 18087f1..11dc656 100644 --- a/smart_service/transactions/doctype/system_mapping/system_mapping.py +++ b/smart_service/transactions/doctype/system_mapping/system_mapping.py @@ -17,8 +17,8 @@ class SystemMapping(Document): procedure = frappe.db.sql("""select name,workflow_state from {0}.`tabProcedure` where name = "{1}";""".format( current_db_name,x.procedure),as_dict=1) procedure = frappe.db.get_value("Procedure", x.procedure, "workflow_state") - frappe.db.sql("""update `tabSystem Mapping_Sub System` set procedure_status = "{0}" where parent = "{1}" - """.format(str(procedure),self.name)) + frappe.db.sql("""update `tabSystem Mapping_Sub System` set procedure_status = %s where parent = %s and `procedure` = %s ; """, ( + str(procedure), self.name, x.procedure)) frappe.db.commit() # x.procedure_status = str(procedure) # frappe.flags.mute_messages = True