#NEWLINE# def get_mapped_doc( from_doctype , from_docname , table_maps , target_doc = None , postprocess = None , ignore_permissions = False ) : #NEWLINE# #INDENT# source_doc = frappe . get_doc ( from_doctype , from_docname ) #NEWLINE# if ( not ignore_permissions ) : #NEWLINE# #INDENT# if ( not source_doc . has_permission ( 'read' ) ) : #NEWLINE# #INDENT# source_doc . raise_no_permission_to ( 'read' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not target_doc ) : #NEWLINE# #INDENT# target_doc = frappe . new_doc ( table_maps [ from_doctype ] [ 'doctype' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( target_doc , basestring ) : #NEWLINE# #INDENT# target_doc = frappe . get_doc ( json . loads ( target_doc ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( not ignore_permissions ) and ( not target_doc . has_permission ( 'create' ) ) ) : #NEWLINE# #INDENT# target_doc . raise_no_permission_to ( 'create' ) #NEWLINE# #UNINDENT# map_doc ( source_doc , target_doc , table_maps [ source_doc . doctype ] ) #NEWLINE# row_exists_for_parentfield = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for df in source_doc . meta . get_table_fields ( ) : #NEWLINE# #INDENT# source_child_doctype = df . options #NEWLINE# table_map = table_maps . get ( source_child_doctype ) #NEWLINE# if ( not table_map ) : #NEWLINE# #INDENT# target_df = target_doc . meta . get_field ( df . fieldname ) #NEWLINE# if target_df : #NEWLINE# #INDENT# target_child_doctype = target_df . options #NEWLINE# if ( target_df and ( target_child_doctype == source_child_doctype ) and ( not df . no_copy ) and ( not target_df . no_copy ) ) : #NEWLINE# #INDENT# table_map = { #NEWLINE# #INDENT# 'doctype' : target_child_doctype , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if table_map : #NEWLINE# #INDENT# for source_d in source_doc . get ( df . fieldname ) : #NEWLINE# #INDENT# if ( 'condition' in table_map ) : #NEWLINE# #INDENT# if ( not table_map [ 'condition' ] ( source_d ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# target_child_doctype = table_map [ 'doctype' ] #NEWLINE# target_parentfield = target_doc . get_parentfield_of_doctype ( target_child_doctype ) #NEWLINE# if ( target_parentfield not in row_exists_for_parentfield ) : #NEWLINE# #INDENT# row_exists_for_parentfield [ target_parentfield ] = ( True if target_doc . get ( target_parentfield ) else False ) #NEWLINE# #UNINDENT# if ( table_map . get ( 'add_if_empty' ) and row_exists_for_parentfield . get ( target_parentfield ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( table_map . get ( 'filter' ) and table_map . get ( 'filter' ) ( source_d ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# map_child_doc ( source_d , target_doc , table_map , source_doc ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if postprocess : #NEWLINE# #INDENT# postprocess ( source_doc , target_doc ) #NEWLINE# #UNINDENT# target_doc . set_onload ( 'load_after_mapping' , True ) #NEWLINE# return target_doc