@ -6,15 +6,13 @@ import frappe
from frappe import _
from frappe import _
from datetime import datetime
from datetime import datetime
from frappe . utils import cstr
from frappe . utils import cstr
from frappe . utils import logger
frappe . utils . logger . set_log_level ( " DEBUG " )
logger = frappe . logger ( " login_api " , allow_site = True , file_count = 100 )
site_name = cstr ( frappe . local . site )
site_name = cstr ( frappe . local . site )
current_db_name = frappe . conf . get ( " db_name " )
current_db_name = frappe . conf . get ( " db_name " )
custom_lang = frappe . db . sql (
custom_lang = frappe . db . sql (
''' SELECT lang_code,lang_name FROM {0} .`tabCustom Languages` where lang_code <> ' en ' ; ''' . format (
''' SELECT lang_code,lang_name FROM {0} .`tabCustom Languages` where lang_code <> ' en ' ; ''' . format (
current_db_name ) , as_dict = True )
current_db_name ) , as_dict = True )
activeStatusQ uerycase = " case when active_status = ' Active ' then ' A ' else ' IA ' end as active_status "
active_status_q uerycase = " case when active_status = ' Active ' then ' A ' else ' IA ' end as active_status "
def execute ( filters = None ) :
def execute ( filters = None ) :
@ -47,97 +45,111 @@ def get_data(filters):
""" .format(current_db_name, fil), as_dict=True)
""" .format(current_db_name, fil), as_dict=True)
filter_data = filters . variant_mapping
filter_data = filters . variant_mapping
if filter_data != None and len ( data ) > 0 :
if filter_data != None and len ( data ) > 0 :
data = modify_data ( data , filter_data )
validation_flag , data = modify_data ( data , filter_data )
if validation_flag :
return data
else :
return [ ]
return data
return data
def status_length_append ( dicV al , parameter ) :
def status_length_append ( dic_v al , parameter ) :
if len ( dicV al ) == 1 :
if len ( dic_v al ) == 1 :
system_size = str ( len ( dicV al ) ) + " " + parameter
system_size = str ( len ( dic_v al ) ) + " " + parameter
else :
else :
system_size = str ( len ( dicV al ) ) + " " + parameter + " s "
system_size = str ( len ( dic_v al ) ) + " " + parameter + " s "
return system_size
return system_size
def modify_data ( dataInput , filterParent ) :
def modify_data ( data_input , filter_parent ) :
list1 = [ ]
try :
input_data = modify_child_data ( dataInput , indent = 5 , header = 0 )
list1 = [ ]
parent_append = add_parent ( filterParent , indent = 0 , header = 1 )
input_data = modify_child_data ( data_input , indent = 5 , header = 0 )
s_map , sSize = get_parent_map ( input_data , ' systems ' )
parent_append = add_parent ( filter_parent , indent = 0 , header = 1 )
system_size = status_length_append ( sSize , ' System ' )
s_map , s_size = get_parent_map ( input_data , ' systems ' )
parent_append [ 0 ] [ ' active_status ' ] = system_size
system_size = status_length_append ( s_size , ' System ' )
if parent_append :
for pa in parent_append :
parent_append [ 0 ] [ ' active_status ' ] = system_size
list1 . append ( pa )
for pa in parent_append :
for i in s_map :
list1 . append ( pa )
s_map1 , sSize1 = get_parent_map ( s_map [ i ] , ' systems ' )
s_map_append = append_to_dic1 ( s_map1 , indent = 1 , header = 0 )
for i in s_map :
ssMap , ssSize = get_parent_map ( s_map [ i ] , ' sub_systems ' )
s_map1 , s_size1 = get_parent_map ( s_map [ i ] , ' systems ' )
ssMapAppend = append_to_dic2 ( ssMap , indent = 2 , header = 1 )
s_map_append = append_to_dic1 ( s_map1 , indent = 1 , header = 0 )
ss_map , ss_size = get_parent_map ( s_map [ i ] , ' sub_systems ' )
s_map_append [ 0 ] [ ' active_status ' ] = status_length_append (
ss_map_append = append_to_dic2 ( ss_map , indent = 2 , header = 1 )
ssSize , ' Sub System ' )
list1 . append ( s_map_append [ 0 ] )
if s_map_append :
s_map_append [ 0 ] [ ' active_status ' ] = status_length_append (
sys_assets = frappe . db . sql ( ''' SELECT system_asset as description, {0}
ss_size , ' Sub System ' )
FROM { 1 } . ` tabSystem Mapping_System Assets `
list1 . append ( s_map_append [ 0 ] )
where parent like ' {2} ' and systems = ' {3} ' and language = ' en ' order by idx ; ''' .format(activeStatusQuerycase, current_db_name, list1[0][ ' procedure_name ' ]+ " % " ,
s_map_append [ 0 ] [ ' procedure_name ' ] ) , as_dict = True )
sys_assets = frappe . db . sql ( ''' SELECT system_asset as description, {0} FROM {1} .`tabSystem Mapping_System Assets`
where parent like ' {2} ' and systems = ' {3} ' and language = ' en ' order by idx ; ''' .format(
for sa in sys_assets :
active_status_querycase , current_db_name , list1 [ 0 ] [ ' procedure_name ' ] + " % " ,
list1 . append ( sa . update (
s_map_append [ 0 ] [ ' procedure_name ' ] ) , as_dict = True )
{ ' procedure_name ' : " System Asset " , ' indent ' : 2 , ' header ' : ' ' , ' type ' : ' Asset ' } ) )
for sa in sys_assets :
for j in ssMapAppend :
list1 . append ( sa . update (
val = j [ ' procedure_name ' ]
{ ' procedure_name ' : " System Asset " , ' indent ' : 2 , ' header ' : ' ' , ' type ' : ' Asset ' } ) )
proc = j [ ' procedure ' ]
procMap , procSize = get_parent_map ( ssMap [ val ] , ' procedure_name ' )
for j in ss_map_append :
procValuesDic = append_to_dic3 (
val = j [ ' procedure_name ' ]
procMap , procSize , j , indent = 3 , header = 0 , procedure = proc )
proc = j [ ' procedure ' ]
proc_map , proc_size = get_parent_map (
procLength = status_length_append ( procValuesDic , ' Procedure ' )
ss_map [ val ] , ' procedure_name ' )
j [ ' active_status ' ] = j [ ' active_status ' ] + ' / ' + procLength
proc_values_dic = append_to_dic3 (
list1 . append ( j )
proc_map , proc_size , j , indent = 3 , header = 0 , procedure = proc )
for pm in procMap :
proc_length = status_length_append (
step_list , steps_size = get_parent_map (
proc_values_dic , ' Procedure ' )
procMap [ pm ] , ' step_name ' )
j [ ' active_status ' ] = j [ ' active_status ' ] + ' / ' + proc_length
step_length = status_length_append ( steps_size , ' Step ' )
list1 . append ( j )
list1 . append ( { ' procedure_name ' : pm , ' indent ' : 3 , ' header ' : 0 ,
' active_status ' : step_length , ' modified ' : ' ' } )
for pm in proc_map :
for sl in step_list :
step_list , steps_size = get_parent_map (
list1 . append ( { ' procedure_name ' : sl , ' indent ' : 4 ,
proc_map [ pm ] , ' step_name ' )
' header ' : 0 , ' active_status ' : ' ' , ' modified ' : ' ' } )
step_length = status_length_append ( steps_size , ' Step ' )
for ss in step_list [ sl ] :
list1 . append ( { ' procedure_name ' : pm , ' indent ' : 3 , ' header ' : 0 ,
ss . update ( { ' procedure_name ' : ss [ ' content_type ' ] } )
' active_status ' : step_length , ' modified ' : ' ' } )
list1 . append ( ss )
for sl in step_list :
list1 . append ( { ' procedure_name ' : sl , ' indent ' : 4 ,
appendData = add_duplicate_data ( list1 )
' header ' : 0 , ' active_status ' : ' ' , ' modified ' : ' ' } )
appendData = remove_duplicate_and_header ( appendData )
for ss in step_list [ sl ] :
return appendData
ss . update ( { ' procedure_name ' : ss [ ' content_type ' ] } )
list1 . append ( ss )
def append_to_dic1 ( mapData , indent , header ) :
append_data = add_duplicate_data ( list1 )
append_data = remove_duplicate_and_header ( append_data )
# frappe.msgprint(str(append_data))
return True , append_data
except Exception as e :
frappe . log_error ( ' Audit report ' , frappe . get_traceback ( ) )
False , None
def append_to_dic1 ( map_data , indent , header ) :
map_data_append = [ ]
map_data_append = [ ]
for d in mapData :
for d in map_data :
keyDict = {
if d :
' procedure_name ' : d ,
key_dict = {
' active_status ' : mapData [ d ] [ 0 ] [ ' active_status ' ] ,
' procedure_name ' : d ,
' indent ' : indent ,
' active_status ' : map_data [ d ] [ 0 ] [ ' active_status ' ] ,
' header ' : header ,
' indent ' : indent ,
' type ' : ' System '
' header ' : header ,
}
' type ' : ' System '
map_data_append . append ( keyDict )
}
map_data_append . append ( key_dict )
return map_data_append
return map_data_append
def append_to_dic2 ( mapD ata , indent , header ) :
def append_to_dic2 ( map_d ata , indent , header ) :
map_data_append = [ ]
map_data_append = [ ]
for d in mapD ata :
for d in map_d ata :
da = mapD ata [ d ] [ 0 ]
da = map_d ata [ d ] [ 0 ]
keyD ict = {
key_d ict = {
' systems ' : da [ ' systems ' ] ,
' systems ' : da [ ' systems ' ] ,
' sub_systems ' : da [ ' sub_systems ' ] ,
' sub_systems ' : da [ ' sub_systems ' ] ,
' parent ' : da [ ' parent ' ] ,
' parent ' : da [ ' parent ' ] ,
@ -156,7 +168,7 @@ def append_to_dic2(mapData, indent, header):
other_proc = other_proc [ : - 2 ] + c [ ' lang_code ' ]
other_proc = other_proc [ : - 2 ] + c [ ' lang_code ' ]
others = frappe . db . sql ( ''' select case when `tabProcedure`.active_status = ' Active ' then ' A ' WHEN `tabProcedure`.active_status = ' InActive ' then ' IA ' end as active_status,count(distinct(`tabProcedure_Details`.procedure_name)) as syscount,`tabProcedure`.workflow_state
others = frappe . db . sql ( ''' select case when `tabProcedure`.active_status = ' Active ' then ' A ' WHEN `tabProcedure`.active_status = ' InActive ' then ' IA ' end as active_status,count(distinct(`tabProcedure_Details`.procedure_name)) as syscount,`tabProcedure`.workflow_state
from ( { 0 } . ` tabProcedure_Details ` inner join { 0 } . ` tabProcedure ` on ` tabProcedure_Details ` . ` parent ` = ` tabProcedure ` . ` name ` )
from ( { 0 } . ` tabProcedure_Details ` inner join { 0 } . ` tabProcedure ` on ` tabProcedure_Details ` . ` parent ` = ` tabProcedure ` . ` name ` )
where ` tabProcedure ` . ` name ` = ' {1} ' ; ''' .format(current_db_name, other_proc, activeStatusQ uerycase), as_dict=True)
where ` tabProcedure ` . ` name ` = ' {1} ' ; ''' .format(current_db_name, other_proc, active_status_q uerycase), as_dict=True)
if len ( others ) > 0 :
if len ( others ) > 0 :
others = others [ 0 ]
others = others [ 0 ]
@ -169,16 +181,15 @@ def append_to_dic2(mapData, indent, header):
c [ ' lang_code ' ] ) ] = others [ ' active_status ' ] + ' / ' + str ( others [ ' syscount ' ] ) + ' Procedures '
c [ ' lang_code ' ] ) ] = others [ ' active_status ' ] + ' / ' + str ( others [ ' syscount ' ] ) + ' Procedures '
dic1 [ ' {} _workflow_state ' . format (
dic1 [ ' {} _workflow_state ' . format (
c [ ' lang_code ' ] ) ] = others [ ' workflow_state ' ]
c [ ' lang_code ' ] ) ] = others [ ' workflow_state ' ]
keyD ict . update ( dic1 )
key_d ict . update ( dic1 )
map_data_append . append ( keyD ict )
map_data_append . append ( key_d ict )
return map_data_append
return map_data_append
def append_to_dic3 ( mapData , procSize , j , indent , header , procedure ) :
def append_to_dic3 ( map_data , proc_size , j , indent , header , procedure ) :
# logger = frappe.logger("login_api", allow_site=True, file_count=100)
map_data_append = [ ]
map_data_append = [ ]
i = 0
i = 0
dic1 = { }
dic1 = { }
@ -195,21 +206,21 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure):
list2 . append ( l )
list2 . append ( l )
list1 . append ( list2 )
list1 . append ( list2 )
for u in mapD ata :
for u in map_d ata :
date_list = [ ]
date_list = [ ]
for k in mapD ata [ u ] :
for k in map_d ata [ u ] :
date_list . append ( str ( k [ ' modified ' ] ) )
date_list . append ( str ( k [ ' modified ' ] ) )
date_list . sort ( key = lambda date : datetime . strptime (
date_list . sort ( key = lambda date : datetime . strptime (
date , " % Y- % m- %d % H: % M: % S. %f " ) )
date , " % Y- % m- %d % H: % M: % S. %f " ) )
if len ( mapD ata [ u ] ) == 1 :
if len ( map_d ata [ u ] ) == 1 :
step_length = str ( len ( mapD ata [ u ] ) ) + " Step "
step_length = str ( len ( map_d ata [ u ] ) ) + " Step "
else :
else :
step_length = str ( len ( mapD ata [ u ] ) ) + " Steps "
step_length = str ( len ( map_d ata [ u ] ) ) + " Steps "
proc_dict = {
proc_dict = {
' systems ' : mapD ata [ u ] [ 0 ] [ ' systems ' ] ,
' systems ' : map_d ata [ u ] [ 0 ] [ ' systems ' ] ,
' sub_systems ' : mapD ata [ u ] [ 0 ] [ ' sub_systems ' ] ,
' sub_systems ' : map_d ata [ u ] [ 0 ] [ ' sub_systems ' ] ,
' parent ' : j [ " parent " ] ,
' parent ' : j [ " parent " ] ,
' procedure_name ' : u ,
' procedure_name ' : u ,
' indent ' : indent ,
' indent ' : indent ,
@ -223,43 +234,46 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure):
for cus in range ( len ( custom_lang ) ) :
for cus in range ( len ( custom_lang ) ) :
if len ( list1 [ cus ] ) > 0 :
if len ( list1 [ cus ] ) > 0 :
if i < len ( list1 [ cus ] ) :
# frappe.msgprint(str(list1[cus]))
if list1 [ cus ] [ i ] [ 0 ] == 1 :
dic1 [ ' {} _active_status ' . format ( custom_lang [ cus ] [ ' lang_code ' ] ) ] = str (
list1 [ cus ] [ i ] [ 0 ] ) + " Step "
else :
dic1 [ ' {} _active_status ' . format ( custom_lang [ cus ] [ ' lang_code ' ] ) ] = str (
list1 [ cus ] [ i ] [ 0 ] ) + " Steps "
if list1 [ cus ] [ i ] [ 0 ] == 1 :
# Append modified date
dic1 [ ' {} _active_status ' . format ( custom_lang [ cus ] [ ' lang_code ' ] ) ] = str (
dic1 [ ' {} _modified ' . format (
list1 [ cus ] [ i ] [ 0 ] ) + " Step "
custom_lang [ cus ] [ ' lang_code ' ] ) ] = list1 [ cus ] [ i ] [ 1 ]
else :
dic1 [ ' {} _active_status ' . format ( custom_lang [ cus ] [ ' lang_code ' ] ) ] = str (
list1 [ cus ] [ i ] [ 0 ] ) + " Steps "
# Append modified date
dic1 [ ' {} _modified ' . format (
custom_lang [ cus ] [ ' lang_code ' ] ) ] = list1 [ cus ] [ i ] [ 1 ]
dic2 . update ( dic1 )
dic2 . update ( dic1 )
proc_dict . update ( dic1 )
proc_dict . update ( dic1 )
i + = 1
i + = 1
map_data_append . append ( proc_dict )
map_data_append . append ( proc_dict )
return map_data_append
return map_data_append
def modify_child_data ( childD ata , indent , header ) :
def modify_child_data ( child_d ata , indent , header ) :
for a in childD ata :
for a in child_d ata :
a . update ( { ' indent ' : indent } )
a . update ( { ' indent ' : indent } )
a . update ( { ' header ' : header } )
a . update ( { ' header ' : header } )
a . update ( { ' type ' : " Step " } )
a . update ( { ' type ' : " Step " } )
return childD ata
return child_d ata
def add_parent ( variantP arent , indent , header ) :
def add_parent ( variant_p arent , indent , header ) :
list1 = [ ]
list1 = [ ]
varParD ict = {
var_par_d ict = {
' procedure_name ' : variantP arent ,
' procedure_name ' : variant_p arent ,
' indent ' : indent ,
' indent ' : indent ,
' header ' : header ,
' header ' : header ,
}
}
list1 . append ( varParDict )
list1 . append ( var_par_dict )
variant_assests = frappe . db . sql ( '''
variant_assests = frappe . db . sql ( ''' SELECT category as procedure_name,attach_file as description, {2}
SELECT category as procedure_name , attach_file as description , { 2 } FROM { 0 } . ` tabVariant Mapping_Assets ` where parent = ' {1} ' ; ''' .format(current_db_name, variantParent, activeStatusQuerycase), as_dict=True)
FROM { 0 } . ` tabVariant Mapping_Assets `
where parent = ' {1} ' ; ''' .format(current_db_name, variant_parent,
active_status_querycase ) , as_dict = True )
for va in variant_assests :
for va in variant_assests :
list1 . append (
list1 . append (
@ -268,24 +282,25 @@ def add_parent(variantParent, indent, header):
def get_parent_map ( input_list , parameter ) :
def get_parent_map ( input_list , parameter ) :
sizeList = [ ]
size_list = [ ]
parentChildMap = { }
parent_child_map = { }
for b in input_list :
for b in input_list :
parentChildM ap . setdefault ( b [ parameter ] or None , [ ] ) . append ( b )
parent_child_m ap . setdefault ( b [ parameter ] or None , [ ] ) . append ( b )
for c in parentChildM ap :
for c in parent_child_m ap :
sizeL ist . append ( len ( parentChildM ap [ c ] ) )
size_l ist . append ( len ( parent_child_m ap [ c ] ) )
return parentChildMap , sizeL ist
return parent_child_map , size_l ist
def add_duplicate_data ( data1 ) :
def add_duplicate_data ( data1 ) :
prevV alue = " "
prev_v alue = " "
for i in data1 :
for i in data1 :
if i [ ' procedure_name ' ] == prevV alue :
if i [ ' procedure_name ' ] == prev_v alue :
dup = { " duplicate " : " yes " }
dup = { " duplicate " : " yes " }
else :
else :
dup = { " duplicate " : " no " }
dup = { " duplicate " : " no " }
i . update ( dup )
i . update ( dup )
prevV alue = i [ ' procedure_name ' ]
prev_v alue = i [ ' procedure_name ' ]
return data1
return data1