@ -96,6 +96,7 @@ def json_grouping(args, language):
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),
@ -104,7 +105,7 @@ def json_grouping(args, language):
var_asset = frappe . db . sql (
''' SELECT 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 ' :
@ -125,8 +126,10 @@ def json_grouping(args, language):
vari . update ( { ' kms_mapping_active_status ' : kms_active_status } )
vari [ ' Assets ' ] = var_asset
variant_out [ ' Variant ' ] = vari
total_count + = len ( var_asset )
# total_count += len(variant[d]) - 1
system_out = [ ]
for i in system [ d ] :
if i != None :
@ -143,7 +146,9 @@ def json_grouping(args, language):
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 )
# total_count += len(system[d][i])
for sysasset in sysassets :
if sysasset [ ' active_status ' ] == ' Active ' :
sysasset [ ' active_status ' ] = True
@ -169,7 +174,7 @@ def json_grouping(args, 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 :
@ -178,7 +183,7 @@ def json_grouping(args, language):
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 ' ]
@ -195,6 +200,7 @@ def json_grouping(args, language):
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 )
@ -211,73 +217,69 @@ def print(sts):
@frappe . whitelist ( allow_guest = True )
def new_publish ( args , publish_type , vehicle , language , version ) :
# Create Directory for internal,global and full_update publish
try :
if not os . path . exists ( internal_path + vehicle + ' / ' ) :
os . makedirs ( internal_path + vehicle + ' / ' )
if not os . path . exists ( global_path + vehicle + ' / ' ) :
os . makedirs ( global_path + vehicle + ' / ' )
if not os . path . exists ( full_update_path + vehicle + ' / ' ) :
os . makedirs ( full_update_path + vehicle + ' / ' )
except Exception as e :
return { " status " : 0 , " error " : " Failed to create Folders " }
#JSON generation
json_file = json_grouping ( args , language )
file_name = ' %s - %s _v %s .json ' % ( vehicle , language , version )
# File name generation
if publish_type . lower ( ) == ' internal ' :
file_name = internal_path + vehicle + ' / ' + file_name
elif publish_type . lower ( ) == ' global ' :
file_name = global_path + vehicle + ' / ' + file_name
else :
return { " status " : 0 , ' error ' : ' Publish type not available ' }
# Save file (Internal/Global)
with open ( file_name , ' w ' ) as outfile :
outfile . write ( json . dumps ( json_file ) )
# 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
if publish_type . lower ( ) == ' global ' :
pub_ver = frappe . db . sql ( ''' SELECT vehicle,`language`,version,publish_type FROM tabPublish where vehicle = ' {} '
and publish_type = ' Global ' AND ` language ` = ' {} ' order by version desc limit 1 ; ''' .format(vehicle,language),as_dict=True)
if pub_ver :
prev_update_ver = pub_ver [ 0 ] [ ' version ' ]
full_update_file_name = full_update_path + vehicle + ' / ' + ' %s - %s -full_v %s .json ' % ( vehicle , language , version )
# Since full update is not available for Global version 1
if version == ' 1.00 ' :
with open ( full_update_file_name , ' w ' ) as outfile :
outfile . write ( json . dumps ( json_file ) )
return full_update_file_name
list1 = [ ]
try :
try :
if not os . path . exists ( internal_path + vehicle + ' / ' ) :
os . makedirs ( internal_path + vehicle + ' / ' )
if not os . path . exists ( global_path + vehicle + ' / ' ) :
os . makedirs ( global_path + vehicle + ' / ' )
if not os . path . exists ( full_update_path + vehicle + ' / ' ) :
os . makedirs ( full_update_path + vehicle + ' / ' )
except Exception as e :
return { " status " : 0 , " error " : " Failed to create Folders " }
#JSON generation
json_file = json_grouping ( args , language )
file_name = ' %s - %s _v %s .json ' % ( vehicle , language , version )
# File name generation
if publish_type . lower ( ) == ' internal ' :
file_name = internal_path + vehicle + ' / ' + file_name
elif publish_type . lower ( ) == ' global ' :
file_name = global_path + vehicle + ' / ' + file_name
else :
prev_full_update_file = full_update_path + vehicle + ' / ' + ' %s - %s -full_v %s .json ' % ( vehicle , language , prev_update_ver )
final_update_file = merge_json_files ( prev_full_update_file , file_name , full_update_file_name )
# Need to remove other full update files
# for fil in os.listdir(full_update_path + vehicle):
# file_val = full_update_path + vehicle + '/' + fil
# if file_val != final_update_file:
# if (fil.split('-')[1]).split('_')[0] == language:
# Remove prev full update files
# os.remove(file_val)
return final_update_file
return { " status " : 0 , ' error ' : ' Publish type not available ' }
# Save file (Internal/Global)
with open ( file_name , ' w ' ) as outfile :
outfile . write ( json . dumps ( json_file ) )
# 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
if publish_type . lower ( ) == ' global ' :
pub_ver = frappe . db . sql ( ''' SELECT vehicle,`language`,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)
if pub_ver :
prev_update_ver = pub_ver [ 1 ] [ ' version ' ]
full_update_file_name = full_update_path + vehicle + ' / ' + ' %s - %s -full_v %s .json ' % ( vehicle , language , version )
# Since full update is not available for Global version 1
if version == ' 1.00 ' :
with open ( full_update_file_name , ' w ' ) as outfile :
outfile . write ( json . dumps ( json_file ) )
# return full_update_file_name
else :
prev_full_update_file = full_update_path + vehicle + ' / ' + ' %s - %s -full_v %s .json ' % ( vehicle , language , prev_update_ver )
list1 . append ( prev_full_update_file )
list1 . append ( file_name )
list1 . append ( full_update_file_name )
final_update_file = merge_json_files ( prev_full_update_file , file_name , full_update_file_name )
return file_name . split ( ' public ' ) [ 1 ] , list1
except :
return " Failed to save file "
@frappe . whitelist ( allow_guest = True )
def new_update ( vehicle_list = None ) :
@ -392,8 +394,8 @@ def merge_json_files(old_json_path,new_json_path,out_file_path):
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 :
conf_km_name . add ( old_ck [ ' kilometer_name ' ] )
sub_system_list . append ( old_ck )
conf_km_added . add ( old_ck [ ' kilometer_name ' ] )
proc_det_name = set ( )
@ -414,8 +416,8 @@ def merge_json_files(old_json_path,new_json_path,out_file_path):
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 ' ] :
proc_det_name . add ( old_pd [ ' kilometer_name ' ] )
if old_pd [ ' procedure_name ' ] not in proc_det_added and old_pd [ ' procedure_name ' ] in proc_det_name :
proc_det_name . add ( old_pd [ ' kilometer_name ' ] )
sub_system_list . append ( old_pd )
proc_det_added . add ( old_pd [ ' kilometer_name ' ] )
sub_system_list . append ( { " subSystemdisplayorder " : subsystem [ ' subSystemdisplayorder ' ] ,
@ -440,27 +442,27 @@ def merge_json_files(old_json_path,new_json_path,out_file_path):
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_name . add ( old_sub_system [ ' sub_systems ' ] )
sub_system_list . append ( old_sub_system )
sub_system_added . add ( old_sub_system [ ' sub_systems ' ] )
for asset in system [ ' Assets ' ] :
sub_s ystem_name . add ( asset [ ' system_asset ' ] )
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 ' ] ,
} )
sub_s ystem_added . add ( asset [ ' system_asset ' ] )
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 :
sub_system_name . add ( old_system_asset [ ' system_asset ' ] )
sub_system_list . append ( old_system_asset )
sub_system_added . add ( old_system_asset [ ' system_asset ' ] )
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 ' ] ,
@ -468,7 +470,7 @@ def merge_json_files(old_json_path,new_json_path,out_file_path):
" active_status " : system [ ' active_status ' ] ,
" Assets " : system_asset_list ,
" Subsystems " : sub_system_list ,
" conf_km_list " : conf_km_list ,
" Config Kilometer " : conf_km_list ,
} )
system_added . add ( system [ ' system_name ' ] )
except :
@ -508,8 +510,8 @@ def merge_json_files(old_json_path,new_json_path,out_file_path):
with open ( out_file_path , " w " ) as outfile :
outfile . write ( json_object )
return out_file_path
except :
return ' Failed to m erge json '
except Exception as e :
return e
# def get_data(args):
# try: