Browse Source

Publish API testing,Update validation coding standard improvements

master
venkataakhil 2 years ago
parent
commit
38c18ad5c3
  1. 100
      smart_service/apis/publish_api.py
  2. 545
      smart_service/apis/update_validation.py
  3. 1
      smart_service/transactions/doctype/publish/publish.js

100
smart_service/apis/publish_api.py

@ -304,7 +304,7 @@ def new_publish(args, publish_type, vehicle, language, version):
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)
# Since full update is not available for Global version 1 # Since full update is not available for Global version 1
if version == '1.00': if version == 1.00:
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))
@ -331,54 +331,6 @@ def new_publish(args, publish_type, vehicle, language, version):
return False, str(frappe.get_traceback()) return False, str(frappe.get_traceback())
@frappe.whitelist(allow_guest=True)
def new_update(vehicle_list=None):
rate_res = custom_rate_limit(limit=5, seconds=15)
if rate_res != 1:
return rate_res
val = input_validation(vehicle_list=vehicle_list)
if val != '':
return {'status': 0, 'error': "Parameter missing :"+val}
req_list = json.loads(vehicle_list)
vehicle = req_list['Vehicle']
iid = req_list['InstallationId']
vehicle_data = req_list['VehicleReqList']
list_val = []
json_val = []
for v in vehicle_data:
lang = v['LanguageID']
cur_ver = v['CurrentVersion']
# publish_list = frappe.db.sql(''' SELECT vehicle,`language`,version,publish_type FROM tabPublish where vehicle = '{}'
# and publish_type='Global' AND `language` = '{}';'''.format(vehicle,lang),as_dict=True)
# list_val.append(publish_list)
for f in os.listdir(internal_path + vehicle):
try:
if (f.split('-')[1]).split('_')[0] == lang:
json_data = json.load(
open(internal_path + vehicle + '/'+f))
for d in json_data:
json_val.append(d)
except:
pass
for i in os.listdir(global_path + vehicle):
try:
if (i.split('-')[1]).split('_')[0] == lang:
json_data = json.load(open(global_path + vehicle + '/'+f))
for d in json_data:
json_val.append(d)
except:
pass
cumulative_update = global_path + vehicle + '/' + 'global.json'
with open(cumulative_update, 'w') as outfile:
outfile.write(json.dumps(json_val))
return json_val
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
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):
# rate_res = custom_rate_limit(limit=5, seconds=15) # rate_res = custom_rate_limit(limit=5, seconds=15)
@ -594,14 +546,64 @@ def merge_json_files(old_json_path, new_json_path, out_file_path):
data_consolidated['JSON'].append({'Variant': variant_old['Variant'], data_consolidated['JSON'].append({'Variant': variant_old['Variant'],
}) })
json_object = json.dumps(data_consolidated) json_object = json.dumps(data_consolidated)
# return True, json_object
with open(out_file_path, "w") as outfile: with open(out_file_path, "w") as outfile:
outfile.write(json_object) outfile.write(json_object)
return True, out_file_path return True, out_file_path
except Exception as e: except Exception as e:
return False, frappe.get_traceback() return False, frappe.get_traceback()
# return "Exception in publish" # return "Exception in publish"
@frappe.whitelist(allow_guest=True)
def new_update(vehicle_list=None):
rate_res = custom_rate_limit(limit=5, seconds=15)
if rate_res != 1:
return rate_res
val = input_validation(vehicle_list=vehicle_list)
if val != '':
return {'status': 0, 'error': "Parameter missing :"+val}
req_list = json.loads(vehicle_list)
vehicle = req_list['Vehicle']
iid = req_list['InstallationId']
vehicle_data = req_list['VehicleReqList']
list_val = []
json_val = []
for v in vehicle_data:
lang = v['LanguageID']
cur_ver = v['CurrentVersion']
# publish_list = frappe.db.sql(''' SELECT vehicle,`language`,version,publish_type FROM tabPublish where vehicle = '{}'
# and publish_type='Global' AND `language` = '{}';'''.format(vehicle,lang),as_dict=True)
# list_val.append(publish_list)
for f in os.listdir(internal_path + vehicle):
try:
if (f.split('-')[1]).split('_')[0] == lang:
json_data = json.load(
open(internal_path + vehicle + '/'+f))
for d in json_data:
json_val.append(d)
except:
pass
for i in os.listdir(global_path + vehicle):
try:
if (i.split('-')[1]).split('_')[0] == lang:
json_data = json.load(open(global_path + vehicle + '/'+f))
for d in json_data:
json_val.append(d)
except:
pass
cumulative_update = global_path + vehicle + '/' + 'global.json'
with open(cumulative_update, 'w') as outfile:
outfile.write(json.dumps(json_val))
return json_val
def get_step_total_count(json_path): def get_step_total_count(json_path):
with open(json_path) as json_file: with open(json_path) as json_file:
data = json.load(json_file) data = json.load(json_file)

545
smart_service/apis/update_validation.py

@ -10,6 +10,15 @@ site_name = cstr(frappe.local.site)
base_url = os.path.expanduser( base_url = os.path.expanduser(
"~") + "/frappe-bench/sites/" + site_name + "/public" "~") + "/frappe-bench/sites/" + site_name + "/public"
# Constants
json_ext = ".json"
json_internal_path = "/files/json_files/internal/"
json_global_path = "/files/json_files/global/"
json_full_update_path = "/files/json_files/full_update/"
status = "status"
error = "error"
update_available = 'IsUpdateAvailable'
def custom_rate_limit(limit, seconds): def custom_rate_limit(limit, seconds):
ip_based = True ip_based = True
@ -30,16 +39,16 @@ def custom_rate_limit(limit, seconds):
def get_parent_map(input_list, parameter): def get_parent_map(input_list, parameter):
parentChildMap = {} parent_child_map = {}
for b in input_list: for b in input_list:
parentChildMap.setdefault(b[parameter] or None, []).append(b) parent_child_map.setdefault(b[parameter] or None, []).append(b)
list1 = [] list1 = []
steps_length = 0 steps_length = 0
for p in parentChildMap: for p in parent_child_map:
da = {"procedure_name": p, 'steps': parentChildMap[p]} da = {"procedure_name": p, 'steps': parent_child_map[p]}
list1.append(da) list1.append(da)
steps_length += len(parentChildMap[p]) steps_length += len(parent_child_map[p])
for i in parentChildMap[p]: for i in parent_child_map[p]:
del i['procedure_name'] del i['procedure_name']
del i['idx'] del i['idx']
@ -52,79 +61,77 @@ def check_all_vehicle_updates(vehicle_list=None):
if rate_res != 1: if rate_res != 1:
return rate_res return rate_res
# if vehicle_list == None: # if vehicle_list == None:
# return {"status": 0, "error": "Parameter missing: Vehicle List"} # return {status: 0, error: "Parameter missing: Vehicle List"}
val = input_validation(vehicle_list=vehicle_list) val = input_validation(vehicle_list=vehicle_list)
if val != '': if val != '':
return {"status": 0, "error": "Parameter missing:"+val} return {status: 0, error: "Parameter missing:"+val}
if vehicle_list: if vehicle_list:
try: try:
Vehicle_req_list = [] vehicle_req_list = []
response = {} response = {}
v_list = json.loads(vehicle_list) v_list = json.loads(vehicle_list)
lang = v_list['LanguageID'] lang = v_list['LanguageID']
iid = v_list['InstallationId'] iid = v_list['InstallationId']
vehicle_data = v_list['VehicleReqList'] vehicle_data = v_list['VehicleReqList']
publish_type = frappe.db.sql( publish_type = frappe.db.sql(
'''SELECT publish_type FROM {}.`tabApp Device` where name='{}';'''.format( '''SELECT publish_type FROM `tabApp Device` where name='{}';'''.format(iid), as_list=1)
current_db_name, iid),
as_list=True)
if publish_type[0][0] is not None: if publish_type[0][0] is not None:
if vehicle_data: if vehicle_data:
for v in vehicle_data: for v in vehicle_data:
v_id = v['Vehicle'] v_id = v['Vehicle']
current_version = float(v['CurrentVersion']) current_version = float(v['CurrentVersion'])
data1_global = frappe.db.sql('''SELECT name,max(version) as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data1_global = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}'
and publish_type='Global' and vehicle_status='Active' order by version ASC;'''.format( and publish_status='Published' and publish_type='Global' and vehicle_status='Active'
current_db_name, v_id, lang), as_dict=True) order by version ASC;'''.format(v_id, lang), as_dict=1)
data1 = frappe.db.sql('''SELECT name,max(version) as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data1 = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}'
and publish_type='{}' and vehicle_status='Active' order by version ASC;'''.format( and publish_status='Published' and publish_type='{}' and vehicle_status='Active'
current_db_name, v_id, lang, publish_type[0][0]), as_dict=True) order by version ASC;'''.format(v_id, lang, publish_type[0][0]), as_dict=1)
if data1[0]['version'] is not None: if data1[0]['version'] is not None:
if current_version == float(data1[0]['version']): if current_version == float(data1[0]['version']):
data1[0]['IsUpdateAvailable'] = "false" data1[0][update_available] = "false"
Vehicle_req_list.append(data1) vehicle_req_list.append(data1)
else: else:
if current_version < float(data1_global[0]['version']): if current_version < float(data1_global[0]['version']):
data = frappe.db.sql('''SELECT name,max(version) as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}'
and publish_type='Global' and vehicle_status='Active' and version > '{}' order by version ASC;'''.format( and publish_status='Published' and publish_type='Global' and vehicle_status='Active' and version > '{}'
current_db_name, v_id, lang, current_version), as_dict=True) order by version ASC;'''.format(v_id, lang, current_version), as_dict=1)
elif current_version == float(data1_global[0]['version']): elif current_version == float(data1_global[0]['version']):
if current_version < float(data1[0]['version']): if current_version < float(data1[0]['version']):
data = frappe.db.sql('''SELECT name,max(version) as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and
and publish_type='{}' and vehicle_status='Active' and version > '{}' order by version ASC;'''.format( language='{}' and publish_status='Published' and publish_type='{}' and vehicle_status='Active' and version > '{}'
current_db_name, v_id, lang, publish_type[0][0], current_version), as_dict=True) order by version ASC;'''.format(v_id, lang, publish_type[0][0], current_version), as_dict=1)
# return data # return data
data_append = [] data_append = []
try: try:
for d in data: for d in data:
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float( d['CurrentVersion'] = float(
current_version) current_version)
data_append.append(d) data_append.append(d)
Vehicle_req_list.append(data_append) vehicle_req_list.append(data_append)
except: except:
pass pass
response['LanguageID'] = lang response['LanguageID'] = lang
if len(Vehicle_req_list) != 0: if len(vehicle_req_list) != 0:
response['VehicleReqList'] = Vehicle_req_list[0] response['VehicleReqList'] = vehicle_req_list[0]
return response return response
else: else:
return {"status": 0, "error": "No Vehicles in criteria"} return {status: 0, error: "No Vehicles in criteria"}
else: else:
return {"status": 0, "error": "Invalid Publish Details"} return {status: 0, error: "Invalid Publish Details"}
else: else:
return {"status": 0, "error": "Invalid Publish Details"} return {status: 0, error: "Invalid Publish Details"}
except Exception as e: except Exception as e:
return {"status": 0, "error": "Failed to fetch updates"} return {status: 0, error: "Failed to fetch updates"}
else: else:
return {"status": 0, "error": "Check argument: vehicle list"} return {status: 0, error: "Check argument: vehicle list"}
@frappe.whitelist() @frappe.whitelist()
@ -133,10 +140,10 @@ def check_vehicle_update(vehicle_list=None):
if rate_res != 1: if rate_res != 1:
return rate_res return rate_res
# if vehicle_list == None: # if vehicle_list == None:
# return {"status": 0, "error": "Parameter missing: Vehicle List"} # return {status: 0, error: "Parameter missing: Vehicle List"}
val = input_validation(vehicle_list=vehicle_list) val = input_validation(vehicle_list=vehicle_list)
if val != '': if val != '':
return {"status": 0, "error": "Parameter missing:"+val} return {status: 0, error: "Parameter missing:"+val}
if vehicle_list: if vehicle_list:
try: try:
@ -151,40 +158,40 @@ def check_vehicle_update(vehicle_list=None):
if len(publish_type) > 0: if len(publish_type) > 0:
publish_type = publish_type[0]['publish_type'] publish_type = publish_type[0]['publish_type']
Vehicle_req_list = [] vehicle_req_list = []
# Iterate on language # Iterate on language
for v in vehicle_data: for v in vehicle_data:
l_id = v['LanguageID'] l_id = v['LanguageID']
current_version = float(v['CurrentVersion']) current_version = float(v['CurrentVersion'])
data1_global = frappe.db.sql('''SELECT name,max(version) as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data1_global = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}'
and publish_type='Global' and vehicle_status='Active' order by version ASC;'''.format( and publish_status='Published' and publish_type='Global' and vehicle_status='Active'
current_db_name, vehicle, l_id), as_dict=True) order by version ASC;'''.format(vehicle, l_id), as_dict=1)
data1 = frappe.db.sql('''SELECT name,max(version) as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data1 = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' and
and vehicle_status='Active' order by version ASC;'''.format( publish_status='Published' and vehicle_status='Active'
current_db_name, vehicle, l_id), as_dict=True) order by version ASC;'''.format(vehicle, l_id), as_dict=1)
if data1_global[0]['version']: if data1_global[0]['version']:
if data1[0]['version'] is not None: if data1[0]['version'] is not None:
if current_version == float(data1[0]['version']): if current_version == float(data1[0]['version']):
data1[0]['IsUpdateAvailable'] = "false" data1[0][update_available] = "false"
data = {} data = {}
data['Language'] = l_id data['Language'] = l_id
data['IsUpdateAvailable'] = 'false' data[update_available] = 'false'
data['CurrentVersion'] = float(current_version) data['CurrentVersion'] = float(current_version)
Vehicle_req_list.append(data) vehicle_req_list.append(data)
else: else:
if current_version < (float(data1_global[0]['version'])-1.00): if current_version < (float(data1_global[0]['version'])-1.00):
data = frappe.db.sql('''SELECT name as Name,max(version) as Version,vehicle_id as Vehicle,language as Language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish`
and publish_type='Global' and vehicle_status='Active' and Version > '{}' order by version ASC;'''.format( where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='Global' and
current_db_name, vehicle, l_id, current_version), as_dict=True) vehicle_status='Active' and Version > '{}' order by version ASC;'''.format(vehicle, l_id, current_version), as_dict=1)
try: try:
for d in data: for d in data:
try: try:
file_name = "/files/json_files/full_update/"+vehicle+"/"+vehicle + \ file_name = json_full_update_path+vehicle+"/"+vehicle + \
"-" + \ "-" + \
d['Language']+"-"+"full_v" + \ d['Language']+"-"+"full_v" + \
str(d['Version'])+".json" str(d['Version'])+json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
d['JsonURL'] = file_name d['JsonURL'] = file_name
@ -198,26 +205,26 @@ def check_vehicle_update(vehicle_list=None):
d['FileSize'] = file_size d['FileSize'] = file_size
d['Error'] = 'File failed to load' d['Error'] = 'File failed to load'
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float( d['CurrentVersion'] = float(
current_version) current_version)
Vehicle_req_list.append(d) vehicle_req_list.append(d)
except Exception as e: except Exception as e:
Vehicle_req_list.append( vehicle_req_list.append(
{"Error": "Update checking Failed"}) {error: "Update checking Failed"})
elif current_version == (float(data1_global[0]['version'])-1.00): elif current_version == (float(data1_global[0]['version'])-1.00):
data = frappe.db.sql('''SELECT name as Name,max(version) as Version,vehicle_id as Vehicle,language as Language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish`
and publish_type='Global' and vehicle_status='Active' and Version > '{}' order by version ASC;'''.format( where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='Global' and vehicle_status='Active' and Version > '{}'
current_db_name, vehicle, l_id, current_version), as_dict=True) order by version ASC;'''.format(vehicle, l_id, current_version), as_dict=1)
try: try:
for d in data: for d in data:
file_name = None file_name = None
try: try:
file_name = "/files/json_files/global/"+vehicle+"/" + \ file_name = json_global_path+vehicle+"/" + \
vehicle+"-" + \ vehicle+"-" + \
d['Language']+"_v" + \ d['Language']+"_v" + \
str(d['Version'])+".json" str(d['Version'])+json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
d['JsonURL'] = file_name d['JsonURL'] = file_name
@ -231,27 +238,27 @@ def check_vehicle_update(vehicle_list=None):
d['FileSize'] = file_size d['FileSize'] = file_size
d['Error'] = 'File failed to load' d['Error'] = 'File failed to load'
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float( d['CurrentVersion'] = float(
current_version) current_version)
Vehicle_req_list.append(d) vehicle_req_list.append(d)
except Exception as e1: except Exception as e1:
Vehicle_req_list.append( vehicle_req_list.append(
{"Error": "Update checking Failed"}) {error: "Update checking Failed"})
elif current_version == float(data1_global[0]['version']): elif current_version == float(data1_global[0]['version']):
if current_version < float(data1[0]['version']): if current_version < float(data1[0]['version']):
data = frappe.db.sql('''SELECT name as Name,max(version) as Version,vehicle_id as Vehicle,language as Language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language
and publish_type='{}' and vehicle_status='Active' and version > '{}' order by version ASC;'''.format( FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' and
current_db_name, vehicle, l_id, publish_type, current_version), as_dict=True) vehicle_status='Active' and version > '{}' order by version ASC;'''.format(vehicle, l_id, publish_type, current_version), as_dict=1)
try: try:
for d in data: for d in data:
try: try:
file_name = "/files/json_files/internal/"+vehicle+"/" + \ file_name = json_internal_path+vehicle+"/" + \
vehicle+"-" + \ vehicle+"-" + \
d['Language']+"_v" + \ d['Language']+"_v" + \
str(d['Version']) + \ str(d['Version']) + \
".json" json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
d['JsonURL'] = file_name d['JsonURL'] = file_name
@ -265,27 +272,27 @@ def check_vehicle_update(vehicle_list=None):
d['FileSize'] = file_size d['FileSize'] = file_size
d['Error'] = 'File failed to load' d['Error'] = 'File failed to load'
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float( d['CurrentVersion'] = float(
current_version) current_version)
Vehicle_req_list.append(d) vehicle_req_list.append(d)
except Exception as e2: except Exception as e2:
Vehicle_req_list.append( vehicle_req_list.append(
{"Error": "Update checking Failed"}) {error: "Update checking Failed"})
elif (float(data1_global[0]['version'])-1.00) < current_version < float(data1_global[0]['version']): elif (float(data1_global[0]['version'])-1.00) < current_version < float(data1_global[0]['version']):
if current_version < float(data1[0]['version']): if current_version < float(data1[0]['version']):
data = frappe.db.sql('''SELECT name as Name,max(version) as Version,vehicle_id as Vehicle,language as Language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish`
and publish_type='Global' and vehicle_status='Active' and version > '{}' order by version ASC;'''.format( where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='Global' and vehicle_status='Active'
current_db_name, vehicle, l_id, current_version), as_dict=True) and version > '{}' order by version ASC;'''.format(vehicle, l_id, current_version), as_dict=1)
try: try:
for d in data: for d in data:
try: try:
file_name = "/files/json_files/global/"+vehicle+"/" + \ file_name = json_global_path+vehicle+"/" + \
vehicle+"-" + \ vehicle+"-" + \
d['Language']+"_v" + \ d['Language']+"_v" + \
str(d['Version']) + \ str(d['Version']) + \
".json" json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
d['JsonURL'] = file_name d['JsonURL'] = file_name
@ -293,39 +300,39 @@ def check_vehicle_update(vehicle_list=None):
d['Error'] = None d['Error'] = None
except: except:
file_name = "/files/json_files/global/"+vehicle+"/" + \ file_name = json_global_path+vehicle+"/" + \
vehicle+"-" + \ vehicle+"-" + \
d['Language']+"_v" + \ d['Language']+"_v" + \
str(d['Version']) + \ str(d['Version']) + \
".json" json_ext
file_size = None file_size = None
d['JsonURL'] = file_name d['JsonURL'] = file_name
d['FileSize'] = file_size d['FileSize'] = file_size
d['Error'] = 'File failed to load' d['Error'] = 'File failed to load'
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float( d['CurrentVersion'] = float(
current_version) current_version)
Vehicle_req_list.append(d) vehicle_req_list.append(d)
except Exception as e2: except Exception as e2:
Vehicle_req_list.append( vehicle_req_list.append(
{"Error": "Update checking Failed"}) {error: "Update checking Failed"})
# else: # else:
# Vehicle_req_list.append({"error2":"Else"}) # vehicle_req_list.append({"error2":"Else"})
elif current_version > float(data1_global[0]['version']): elif current_version > float(data1_global[0]['version']):
if current_version < float(data1[0]['version']): if current_version < float(data1[0]['version']):
data = frappe.db.sql('''SELECT name as Name,max(version) as Version,vehicle_id as Vehicle,language as Language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish`
and publish_type='{}' and vehicle_status='Active' and version > '{}' order by version ASC;'''.format( where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' and vehicle_status='Active' and version > '{}'
current_db_name, vehicle, l_id, publish_type, current_version), as_dict=True) order by version ASC;'''.format(vehicle, l_id, publish_type, current_version), as_dict=1)
try: try:
for d in data: for d in data:
try: try:
file_name = "/files/json_files/internal/"+vehicle+"/" + \ file_name = json_internal_path+vehicle+"/" + \
vehicle+"-" + \ vehicle+"-" + \
d['Language']+"_v" + \ d['Language']+"_v" + \
str(d['Version']) + \ str(d['Version']) + \
".json" json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
d['JsonURL'] = file_name d['JsonURL'] = file_name
@ -333,39 +340,39 @@ def check_vehicle_update(vehicle_list=None):
d['Error'] = None d['Error'] = None
except: except:
file_name = "/files/json_files/internal/"+vehicle+"/" + \ file_name = json_internal_path+vehicle+"/" + \
vehicle+"-" + \ vehicle+"-" + \
d['Language']+"_v" + \ d['Language']+"_v" + \
str(d['Version']) + \ str(d['Version']) + \
".json" json_ext
file_size = None file_size = None
d['JsonURL'] = file_name d['JsonURL'] = file_name
d['FileSize'] = file_size d['FileSize'] = file_size
d['Error'] = 'File failed to load' d['Error'] = 'File failed to load'
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float( d['CurrentVersion'] = float(
current_version) current_version)
Vehicle_req_list.append(d) vehicle_req_list.append(d)
except Exception as e3: except Exception as e3:
Vehicle_req_list.append( vehicle_req_list.append(
{'Error': "Update checking Failed"}) {'Error': "Update checking Failed"})
# else: # else:
# Vehicle_req_list.append({'Error':"Else 3"}) # vehicle_req_list.append({'Error':"Else 3"})
else: else:
Vehicle_req_list.append( vehicle_req_list.append(
{'Language': l_id, 'IsUpdateAvailable': 'false', 'CurrentVersion': float(current_version)}) {'Language': l_id, update_available: 'false', 'CurrentVersion': float(current_version)})
else: else:
data = frappe.db.sql('''SELECT name as Name,max(version) as Version,vehicle_id as Vehicle,language as Language FROM {}.tabPublish where vehicle='{}' and language='{}' and publish_status='Published' data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish`
and publish_type='{}' and vehicle_status='Active' and version > '{}' order by version ASC;'''.format( where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' and vehicle_status='Active'
current_db_name, vehicle, l_id, publish_type, current_version), as_dict=True) and version > '{}' order by version ASC;'''.format(vehicle, l_id, publish_type, current_version), as_dict=1)
try: try:
for d in data: for d in data:
try: try:
file_name = "/files/json_files/internal/"+vehicle+"/" + \ file_name = json_internal_path+vehicle+"/" + \
vehicle+"-"+d['Language'] + \ vehicle+"-"+d['Language'] + \
"_v"+str(d['Version'])+".json" "_v"+str(d['Version'])+json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
d['JsonURL'] = file_name d['JsonURL'] = file_name
@ -379,24 +386,24 @@ def check_vehicle_update(vehicle_list=None):
d['FileSize'] = file_size d['FileSize'] = file_size
d['Error'] = 'File failed to load' d['Error'] = 'File failed to load'
d['IsUpdateAvailable'] = 'true' d[update_available] = 'true'
d['CurrentVersion'] = float(current_version) d['CurrentVersion'] = float(current_version)
Vehicle_req_list.append(d) vehicle_req_list.append(d)
except Exception as e2: except Exception as e2:
Vehicle_req_list.append( vehicle_req_list.append(
{"Error": "Update checking Failed"}) {error: "Update checking Failed"})
response['Vehicle'] = vehicle response['Vehicle'] = vehicle
response['VehicleReqList'] = Vehicle_req_list response['VehicleReqList'] = vehicle_req_list
return response return response
else: else:
response['JSON'] = {"status": 0, "Error": "Data Not available"} response['JSON'] = {status: 0, error: "Data Not available"}
return response return response
except Exception as e: except Exception as e:
response['JSON'] = {"status": 0, "error": "Update checking Failed"} response['JSON'] = {status: 0, error: "Update checking Failed"}
return response return response
else: else:
return {"status": 0, "error": "Check Parameter: vehicle list"} return {status: 0, error: "Check Parameter: vehicle list"}
def get_step_total_count(json_path): def get_step_total_count(json_path):
@ -461,10 +468,10 @@ def calculate_step(data):
def check_vehicle_update11(vehicle_list=None): def check_vehicle_update11(vehicle_list=None):
val = input_validation(vehicle_list=vehicle_list) val = input_validation(vehicle_list=vehicle_list)
if val != '': if val != '':
return {"status": 0, "error": "Parameter missing:"+val} return {status: 0, error: "Parameter missing:"+val}
# if vehicle_list == None: # if vehicle_list == None:
# return {"status": 0, "error": "Parameter missing: Vehicle List"} # return {status: 0, error: "Parameter missing: Vehicle List"}
Vehicle_req_list = [] vehicle_req_list = []
if vehicle_list: if vehicle_list:
response = {} response = {}
@ -481,9 +488,9 @@ def check_vehicle_update11(vehicle_list=None):
# Iterate over language # Iterate over language
if publish_type == 'Internal': if publish_type == 'Internal':
for v in vehicle_data: for v in vehicle_data:
global_ver = frappe.db.sql('''SELECT max(version) as version,publish_type as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' global_ver = frappe.db.sql('''SELECT format(max(version),2) as version,publish_type as version,vehicle_id,language FROM `tabPublish` where vehicle='{}'
and language='{}' and publish_status='Published' and vehicle_status='Active' and publish_type = 'Global' ;'''.format( and language='{}' and publish_status='Published' and vehicle_status='Active'
current_db_name, vehicle, l_id), as_dict=True) and publish_type = 'Global' ;'''.format(vehicle, l_id), as_dict=1)
if global_ver: if global_ver:
global_pub_ver = global_pub_ver['version'] global_pub_ver = global_pub_ver['version']
@ -491,7 +498,7 @@ def check_vehicle_update11(vehicle_list=None):
cur_ver = v['CurrentVersion'] cur_ver = v['CurrentVersion']
pub_ver = get_publish_ver(vehicle, l_id) pub_ver = get_publish_ver(vehicle, l_id)
if cur_ver == pub_ver['version']: if cur_ver == pub_ver['version']:
Vehicle_req_list.append({'Language': l_id, 'IsUpdateAvailable': 'false', vehicle_req_list.append({'Language': l_id, update_available: 'false',
'CurrentVersion': float(cur_ver)}) 'CurrentVersion': float(cur_ver)})
# Global File # Global File
@ -500,306 +507,44 @@ def check_vehicle_update11(vehicle_list=None):
l_id = v['LanguageID'] l_id = v['LanguageID']
cur_ver = v['CurrentVersion'] cur_ver = v['CurrentVersion']
global_pub = frappe.db.sql('''SELECT max(version) as version,publish_type, vehicle_id,language FROM {}.tabPublish where vehicle='{}' global_pub = frappe.db.sql('''SELECT format(max(version),2) as version,publish_type, vehicle_id,language FROM `tabPublish` where vehicle='{}'
and language='{}' and publish_status='Published' and vehicle_status='Active' and publish_type = 'Global' ;'''.format( and language='{}' and publish_status='Published' and vehicle_status='Active'
current_db_name, vehicle, l_id), as_dict=True) and publish_type = 'Global' ;'''.format(vehicle, l_id), as_dict=1)
if global_pub: if global_pub:
global_pub_ver1 = float(global_pub[0]['version']) global_pub_ver1 = float(global_pub[0]['version'])
if global_pub_ver1 == cur_ver: if global_pub_ver1 == cur_ver:
Vehicle_req_list.append({'Language': l_id, 'IsUpdateAvailable': 'false', vehicle_req_list.append({'Language': l_id, update_available: 'false',
'CurrentVersion': float(cur_ver)}) 'CurrentVersion': float(cur_ver)})
else: else:
if cur_ver < global_pub_ver1: if cur_ver < global_pub_ver1:
if (global_pub_ver1 - cur_ver) > 1: if (global_pub_ver1 - cur_ver) > 1:
file_name = "/files/json_files/full_update/"+vehicle+"/"+vehicle + \ file_name = json_full_update_path+vehicle+"/"+vehicle + \
"-" + l_id + "-" + "full_v" + \ "-" + l_id + "-" + "full_v" + \
str(global_pub[0]['version'])+".json" str(global_pub[0]['version'])+json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
Vehicle_req_list.append({'Version': global_pub_ver1, 'Language': l_id, 'IsUpdateAvailable': 'true', 'JsonURL': file_name, 'FileSize': file_size, vehicle_req_list.append({'Version': global_pub_ver1, 'Language': l_id, update_available: 'true', 'JsonURL': file_name, 'FileSize': file_size,
'Error': None, 'CurrentVersion': float(cur_ver)}) 'Error': None, 'CurrentVersion': float(cur_ver)})
else: else:
file_name = "/files/json_files/global/"+vehicle+"/"+vehicle + \ file_name = json_global_path+vehicle+"/"+vehicle + \
"-" + l_id + "_v" + \ "-" + l_id + "_v" + \
str(global_pub[0]['version'])+".json" str(global_pub[0]['version'])+json_ext
file_size = os.path.getsize( file_size = os.path.getsize(
base_url + file_name) base_url + file_name)
Vehicle_req_list.append({'Version': global_pub_ver1, 'Language': l_id, 'IsUpdateAvailable': 'true', 'JsonURL': file_name, 'FileSize': file_size, vehicle_req_list.append({'Version': global_pub_ver1, 'Language': l_id, update_available: 'true', 'JsonURL': file_name, 'FileSize': file_size,
'Error': None, 'CurrentVersion': float(cur_ver)}) 'Error': None, 'CurrentVersion': float(cur_ver)})
else: else:
Vehicle_req_list.append({'Language': l_id, 'IsUpdateAvailable': 'false', vehicle_req_list.append({'Language': l_id, update_available: 'false',
'CurrentVersion': float(cur_ver)}) 'CurrentVersion': float(cur_ver)})
return Vehicle_req_list return vehicle_req_list
def get_publish_ver(vehicle, l_id): def get_publish_ver(vehicle, l_id):
return frappe.db.sql('''SELECT max(version) as version,publish_type as version,vehicle_id,language FROM {}.tabPublish where vehicle='{}' and language='{}' and return frappe.db.sql('''SELECT format(max(version),2) as version,publish_type as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' and
publish_status='Published' and vehicle_status='Active' ;'''.format( publish_status='Published' and vehicle_status='Active' ;'''.format(vehicle, l_id), as_dict=1)
current_db_name, vehicle, l_id), as_dict=True)
# @frappe.whitelist()
# def check_vehicle_update(vehicle_list=None):
# if vehicle_list == None:
# return {"status": 0, "error": "Parameter missing: Vehicle List"}
# if vehicle_list:
# try:
# response = {}
# req_list = json.loads(vehicle_list)
# vehicle = req_list['Vehicle']
# iid = req_list['InstallationId']
# vehicle_data = req_list['VehicleReqList']
# publish_type = frappe.db.get_list('App Device', filters={'name': iid}, fields='publish_type')
# if len(publish_type) > 0:
# publish_type = publish_type[0]['publish_type']
# Vehicle_req_list = []
# # Iterate on language
# for v in vehicle_data:
# appendList = []
# appendDict = {}
# variant = set()
# system = {}
# subsystem = {}
# procedure = {}
# list1 = []
# list2 = []
# l_id = v['LanguageID']
# current_version = v['CurrentVersion']
# # Not necessary
# if publish_type == 'Internal':
# publish_data = frappe.db.sql('''select name,CAST(version AS DECIMAL(10,2)) as version from {}.tabPublish where vehicle= '{}'
# and language='{}' and publish_type = '{}' and version > {} and vehicle_status = 'Active';
# '''.format(current_db_name, vehicle, l_id, publish_type, current_version), as_dict=True)
# else:
# publish_data = frappe.db.sql('''select name,version,release_description as description from {}.tabPublish where vehicle= '{}'
# and language='{}' and publish_type = '{}' and version > {} and vehicle_status = 'Active'; '''.format(
# current_db_name, vehicle, l_id, publish_type,
# current_version), as_dict=True)
# if len(publish_data) > 0:
# update_version = publish_data[-1]['version']
# for d in publish_data:
# publish_docs = frappe.db.get_list('Publish_Docs', filters={'parent': d['name']},
# fields=['variant_mapping', 'system', 'sub_system',
# 'procedure_link', 'procedure_status'])
# if publish_docs is not None:
# list1.append(publish_docs)
# for e in list1:
# for f in e:
# list2.append(f)
# # Sort data according to variant, system and sub system
# for d in list2:
# variant.add(d['variant_mapping'])
# for d in variant:
# temp = set()
# for j in list2:
# if j['variant_mapping'] == d:
# temp.add(j['system'])
# system[d] = temp
# for d in variant:
# temp_set = {}
# for j in system[d]:
# temp = set()
# for k in list2:
# if d == k['variant_mapping'] and j == k['system']:
# temp.add(k['sub_system'])
# temp_set[j] = temp
# subsystem[d] = temp_set
# for d in variant:
# temp_set = {}
# for i in system[d]:
# temp_dict = {}
# for j in subsystem[d][i]:
# temp_sub = set()
# for k in list2:
# if k['variant_mapping'] == d and k['system'] == i and k['sub_system'] == j:
# temp_sub.add(k['procedure_link'])
# temp_dict[j] = temp_sub
# temp_set[i] = temp_dict
# procedure[d] = temp_set
# appendList.append(procedure)
# for g in appendList:
# for f in g.keys():
# appendDict[f] = (g[f])
# final = []
# active_status_case = "CASE WHEN active_status = 'Active' THEN 'True' ELSE 'False' END AS active_status"
# for d in appendDict:
# total_count = 0
# # count = 0
# step_count = 0
# variant_out = {}
# # Variant data
# vari = frappe.db.sql('''SELECT name,variant, vehicle,family_code,vehicle_segment,fuel,transmission,drive, active_status
# FROM {}.`tabVariant Mapping` WHERE name ='{}' ; '''.format(current_db_name, d),
# as_dict=True)[0]
# var_asset = frappe.db.sql('''SELECT category, attach_file as file, {},idx as variant_asset_idx FROM {}.`tabVariant Mapping_Assets`
# where category<>'Technical Manual' and parent='{}' and language='{}';'''.format(
# active_status_case, current_db_name, d, l_id), as_dict=True)
# 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, vari['name'] + "%", l_id))[0][0]
# if vari['active_status'] == 'Active':
# vari['active_status'] = True
# else:
# vari['active_status'] = False
# 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
# total_count += len(var_asset)
# # -1 Due to NONE value in parent mapping
# total_count += len(appendDict[d]) - 1
# variant_out['Variant'] = vari
# # System data
# system_out = []
# for i in appendDict[d]:
# if i is not None:
# sys = frappe.db.sql(''' SELECT min(tmss.idx) as systemdisplayorder,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'] + "%"), l_id, i),
# as_dict=True)[0]
# if sys['active_status'] == 'Active':
# sys['active_status'] = True
# else:
# sys['active_status'] = False
# # System Assets
# 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, l_id, i, current_db_name), as_dict=True)
# for sysasset in sysassets:
# if sysasset['active_status'] == 'Active':
# sysasset['active_status'] = True
# else:
# sysasset['active_status'] = False
# sys['Assets'] = sysassets
# total_count += len(sysassets)
# total_count += len(appendDict[d][i])
# # Sub system
# subsystem_out = []
# for j in appendDict[d][i]:
# subsys = frappe.db.sql('''select `tabSystem Mapping_Sub System`.idx as subSystemdisplayorder, systems,sub_systems, symptom,
# component, estimate_time as estimated_time, rts, mat, cover_image, `tabSub Systems`.myid,`tabSystem Mapping_Sub System`.active_status,
# `tabSystem Mapping_Sub System`.cover_image from {4}.`tabSystem Mapping_Sub System` inner join {4}.`tabSub Systems` on
# `tabSystem Mapping_Sub System`.sub_systems = `tabSub Systems`.name where `tabSystem Mapping_Sub System`.parent like '{0}-{3}%' and
# systems='{1}' and sub_systems='{2}' and `tabSystem Mapping_Sub System`.active_status = 'Active';'''.format(
# d, i, j, l_id, current_db_name), as_dict=True)
# if len(subsys) > 0:
# subsys = subsys[0]
# if subsys['active_status'] == 'Active':
# subsys['active_status'] = True
# else:
# subsys['active_status'] = False
# kms = frappe.db.sql('''SELECT kilometer as kilometer_name, idx as kilometer_IDX, applicable as kilometers_applicable
# FROM {}.`tabKilometer Mapping_Items` where sub_systems='{}' and parent Like '{}' and substring(parent,-5,2) = '{}'
# order by kilometer_IDX;'''.format(current_db_name, j, d + "%",
# l_id),
# as_dict=True)
# total_count += len(kms)
# subsys['Config Kilometer'] = kms
# for k in appendDict[d][i][j]:
# proc_details = frappe.db.sql('''select procedure_name, step_name, content_type,
# GROUP_CONCAT(case when content_type='Link' Then system_id else content end) as content,
# GROUP_CONCAT(file) as file,GROUP_CONCAT(DISTINCT idx order by idx) as display_order, idx from {}.tabProcedure_Details
# where parent ='{}' group by procedure_name,step_name,content_type,content order by idx asc; '''
# .format(current_db_name, k), as_dict=True)
# proc_data, step_length = get_parent_map(proc_details, 'procedure_name')
# procedure_status = frappe.db.sql(
# '''SELECT workflow_state FROM {1}.tabProcedure where name='{0}';'''.format(
# k, current_db_name))
# total_count += len(proc_data)
# subsys['procedure_link'] = k
# subsys['procedure_status'] = procedure_status[0][0]
# subsys['Procedure_details'] = proc_data
# step_count += step_length
# subsystem_out.append(subsys)
# sys['Subsystems'] = subsystem_out
# system_out.append(sys)
# variant_out['StepCount'] = step_count
# variant_out['Variant']['Systems'] = system_out
# variant_out['TotalCount'] = total_count + step_count
# final.append(variant_out)
# res = {'Name': '%s-%s_%s' % (iid, vehicle, l_id), 'Language': l_id,
# 'IsUpdateAvailable': 'true',
# 'CurrentVersion': float(current_version), 'Version': float(update_version)}
# try:
# dir1 = "/files/json_files/temp/"
# dir2 = base_url+ dir1
# if not os.path.exists(dir2):
# os.makedirs(dir2)
# file_name = dir1 + "%s-%s_%s.json" % (iid, vehicle, l_id)
# dicfinal = {'JSON': final}
# with open(base_url + file_name, 'w') as outfile:
# outfile.write(json.dumps(dicfinal))
# file_size = os.path.getsize(base_url + file_name)
# res['JsonURL'] = file_name
# res['FileSize'] = file_size
# res['Error'] = None
# except:
# res['JsonURL'] = None
# res['FileSize'] = None
# res['Error'] = 'File failed to save'
# Vehicle_req_list.append(res)
# else:
# Vehicle_req_list.append(
# {'Language': l_id, 'IsUpdateAvailable': 'false', 'CurrentVersion': float(current_version)})
# response['Vehicle'] = vehicle
# response['VehicleReqList'] = Vehicle_req_list
# return response
# else:
# response['JSON'] = {"status": 0, "Error": "Data Not available"}
# return response
# except Exception as e:
# response['JSON'] = {"status": 0, "error": e}
# return response
# else:
# return {"status": 0, "error": "Check Parameter: vehicle list"}
@frappe.whitelist() @frappe.whitelist()
@ -809,23 +554,21 @@ def update_completed(FileName=None):
return rate_res return rate_res
val = input_validation(FileName=FileName) val = input_validation(FileName=FileName)
if val != '': if val != '':
return {"status": 0, "error": "Parameter missing:"+val} return {status: 0, error: "Parameter missing:"+val}
# if FileName == None:
# return {'status': 0, 'error': "Parameter missing: FileName"}
if FileName: if FileName:
try: try:
os.remove(base_url + FileName) os.remove(base_url + FileName)
return {'isSuccessful': True} return {'isSuccessful': True}
except Exception as e: except Exception as e:
return {'isSuccessful': False, "status": 0, 'error': e} return {'isSuccessful': False, status: 0, 'error': e}
else: else:
return {"status": 0, "error": "Parameter: FileName is null"} return {status: 0, error: "Parameter: FileName is null"}
@frappe.whitelist() @frappe.whitelist()
def delete_complete(doc=None, method=None): def delete_complete(doc=None, method=None):
rate_res = rate_limit(limit=5, seconds=15) rate_res = custom_rate_limit(limit=5, seconds=15)
if rate_res != 1: if rate_res != 1:
return rate_res return rate_res
base_url1 = os.path.expanduser( base_url1 = os.path.expanduser(

1
smart_service/transactions/doctype/publish/publish.js

@ -156,7 +156,6 @@ frappe.ui.form.on('Publish', {
// } // }
// } // }
}); });
} }

Loading…
Cancel
Save