@@ -972,6 +972,111 @@ def get_pptx_from_report(self, report_guid, directory_name=None, query=None):
972972 else :
973973 raise Exception (f"The server returned an error code { resp .status_code } " )
974974
975+ def get_templates_as_json (self , root_guid ):
976+ """
977+ Convert report templates rooted as root_guid to JSON
978+ Return a python dictionary.
979+ """
980+ templates_data = {}
981+ templates = self .get_objects (objtype = report_objects .TemplateREST )
982+ template_guid_id_map = {root_guid : 0 }
983+ _build_template_data (root_guid , templates_data , templates , template_guid_id_map )
984+ return templates_data
985+
986+ def _populate_template (self , attr , parent_template ):
987+ template = self .create_template (
988+ name = attr ["name" ], parent = parent_template , report_type = attr ["report_type" ]
989+ )
990+ template .set_params (attr ["params" ])
991+ if attr ["sort_selection" ] != "" :
992+ template .set_sort_selection (value = attr ["sort_selection" ])
993+ template .set_tags (attr ["tags" ])
994+ template .set_filter (filter_str = attr ["item_filter" ])
995+
996+ return template
997+
998+ def _update_changes (self , id_str , id_template_map , templates_json ):
999+ children_id_strs = templates_json [id_str ]["children" ]
1000+ if not children_id_strs :
1001+ return
1002+
1003+ for child_id_str in children_id_strs :
1004+ self ._update_changes (child_id_str , id_template_map , templates_json )
1005+ self .put_objects (id_template_map [id_str ])
1006+
1007+ def _build_templates_from_parent (self , id_str , id_template_map , templates_json ):
1008+ children_id_strs = templates_json [id_str ]["children" ]
1009+ if not children_id_strs :
1010+ return
1011+
1012+ child_templates = []
1013+ for child_id_str in children_id_strs :
1014+ child_attr = templates_json [child_id_str ]
1015+ child_template = self ._populate_template (child_attr , id_template_map [id_str ])
1016+ child_templates .append (child_template )
1017+ id_template_map [child_id_str ] = child_template
1018+
1019+ self .put_objects (child_templates )
1020+
1021+ i = 0
1022+ for child_id_str in children_id_strs :
1023+ self ._build_templates_from_parent (child_id_str , id_template_map , templates_json )
1024+ i += 1
1025+
1026+ def load_templates (self , templates_json ):
1027+ """
1028+ Load templates given a json-format data
1029+ """
1030+ for template_id_str , template_attr in templates_json .items ():
1031+ if template_attr ["parent" ] is None :
1032+ root_id_str = template_id_str
1033+ break
1034+
1035+ root_attr = templates_json [root_id_str ]
1036+ root_template = self ._populate_template (root_attr , None )
1037+ self .put_objects (root_template )
1038+ id_template_map = {}
1039+ id_template_map [root_id_str ] = root_template
1040+ self ._build_templates_from_parent (root_id_str , id_template_map , templates_json )
1041+ self ._update_changes (root_id_str , id_template_map , templates_json )
1042+
1043+
1044+ def _build_template_data (guid , templates_data , templates , template_guid_id_map ):
1045+ curr_template = None
1046+ for template in templates :
1047+ if template .guid == guid :
1048+ curr_template = template
1049+
1050+ fields = ["name" , "report_type" , "date" , "tags" , "item_filter" ]
1051+ curr_template_key = f"Template_{ template_guid_id_map [curr_template .guid ]} "
1052+ templates_data [curr_template_key ] = {}
1053+ for field in fields :
1054+ value = getattr (curr_template , field , None )
1055+ if value is None :
1056+ continue
1057+ templates_data [curr_template_key ][field ] = value
1058+
1059+ templates_data [curr_template_key ]["params" ] = curr_template .get_params ()
1060+ templates_data [curr_template_key ]["sort_selection" ] = curr_template .get_sort_selection ()
1061+ if curr_template .parent is None :
1062+ templates_data [curr_template_key ]["parent" ] = None
1063+ else :
1064+ templates_data [curr_template_key ][
1065+ "parent"
1066+ ] = f"Template_{ template_guid_id_map [curr_template .parent ]} "
1067+
1068+ templates_data [curr_template_key ]["children" ] = []
1069+ children_guids = curr_template .children
1070+ for child_guid in children_guids :
1071+ curr_size = len (template_guid_id_map )
1072+ template_guid_id_map [child_guid ] = curr_size
1073+ templates_data [curr_template_key ]["children" ].append (f"Template_{ curr_size } " )
1074+
1075+ if not children_guids :
1076+ return
1077+ for child_guid in children_guids :
1078+ _build_template_data (child_guid , templates_data , templates , template_guid_id_map )
1079+
9751080
9761081def create_new_local_database (
9771082 parent ,
0 commit comments