def get(self): """!@brief Get data in left for Step 4 when click [新规登陆] Get the tree for left in step 4 @return data: data for left in step 4 """ try: if self.id is not '': policy_tree = Policy_tree(self.id) # get policy tree policy_tree_dict = policy_tree.get_policy_tree() data = { 'data': { 'data': policy_tree_dict, }, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS, }, } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def delete(self): """!@brief delete the rule @param @pre @post @note @return data rule tree and block rule tree @author Gin Chen @date 2017/12/25 """ try: rule_id = views_helper.get_request_value(self.request, key='rule_id', method_type='GET') policy_tree_id = views_helper.get_request_value(self.request, key='coll_policy_id', method_type='GET') CollPolicyCliRule.objects.get(ruleid=rule_id).delete() p = Policy_tree(policy_tree_id) rule_tree_tuple = p.get_rules_tree() data = { 'data': { "block_rule_tree_json": rule_tree_tuple[0], "data_rule_tree_json": rule_tree_tuple[1] }, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get(self): """!@brief init policy tree edit page @note @return policy tree info,data rule rule,block rule @author Gin Chen @date 2017/12/18 """ try: self.coll_policy_id = views_helper.get_request_get(self.request, 'coll_policy_id') cp = CollPolicy.objects.get(coll_policy_id=self.coll_policy_id) cli_command_result = cp.cli_command_result policy_name = cp.name policy_tree = Policy_tree(self.coll_policy_id) # get policy tree policy_tree_dict = policy_tree.get_policy_tree() # get rules of the policy tree rule_tree_tuple = policy_tree.get_rules_tree() if rule_tree_tuple == 'Error': data = { 'data': '', 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.LOAD_RULE_TYPE_ERROR } } else: block_rule_tree_dict = rule_tree_tuple[0] data_rule_tree_dict = rule_tree_tuple[1] data = { 'data': { "coll_policy_name": policy_name, "cli_command_result": cli_command_result, "policy_tree_json": policy_tree_dict, "block_rule_tree_json": block_rule_tree_dict, "data_rule_tree_json": data_rule_tree_dict, }, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def post(self): """!@brief save policy tree @note @return response @author Gin Chen @date 2017/12/18 """ self.coll_policy_id = views_helper.get_request_value(self.request, 'coll_policy_id', 'BODY') self.tree = views_helper.get_request_value(self.request, 'tree', 'BODY') self.raw_data = views_helper.get_request_value(self.request, 'raw_data', 'BODY') try: with transaction.atomic(): tree_is_exits = CollPolicyRuleTree.objects.filter(coll_policy=self.coll_policy_id) if tree_is_exits: # check the policy is exits in the item tables items_list = Items.objects.filter(coll_policy=self.coll_policy_id) if len(items_list) > 0: data = { 'data': '', 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.POLICY_IS_APPLIED } } return api_return(data=data) else: # del the old policy tree CollPolicyRuleTree.objects.filter(coll_policy=self.coll_policy_id).delete() # check the leaf that is not block rule if not self.__check_the_node_is_leaf(self.tree): data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.LEAF_IS_BLOCK_RULE } } return api_return(data=data) # save the new policy # update cli_command_result into coll_policy table coll_policy = CollPolicy.objects.get(pk=self.coll_policy_id) if self.raw_data: coll_policy.cli_command_result = self.raw_data coll_policy.save() # select all nodes of the policy tree policy_tree = Policy_tree(self.coll_policy_id) policy_tree.get_all_nodes(self.tree) obj = policy_tree.all_nodes data = self.__save_the_policy_tree(nodes_dict=obj) return api_return(data=data) except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) data = { 'data': '', 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.DB_EXCEPTION } } return api_return(data=data)
def post(self): """!@brief save the rule information into db. two points need to check before save the rule information 1 the rule name is not existing in the cp 2 the identifier(識別子)name is not existing in the cp @param @pre @post @note @return data rule tree and block rule tree and verify error message @author Gin Chen @date 2017/12/25 """ try: insert_info = views_helper.get_request_value(self.request, key='rule_info', method_type='BODY') coll_policy_id = str(insert_info['coll_policy']) name = insert_info['name'] query_set = CollPolicyCliRule.objects.filter(name=name, coll_policy=coll_policy_id) error_msg_list = {} if len(query_set) > 0: error_msg_list.update({'rule_name': False}) else: error_msg_list.update({'rule_name': True}) identifier_is_existing = self.__judge_identifier_name_exist(insert_info['key_str'], coll_policy_id) if identifier_is_existing: error_msg_list.update({'key_str_name': False}) else: error_msg_list.update({'key_str_name': True}) if len(query_set) > 0 or identifier_is_existing: data = { 'data': '', 'verify_error_msg': error_msg_list, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.RULE_NAME_IS_EXISTENCE } } else: cli_command = CollPolicy.objects.get(coll_policy_id=coll_policy_id).cli_command rule_data_dict = self.__set_input_rule_data(insert_info) rule_data_dict['command'] = cli_command serializer = CollPolicyCliRuleSerializer(data=rule_data_dict) if serializer.is_valid(): serializer.save() policy_tree = Policy_tree(coll_policy_id) rule_tree_tuple = policy_tree.get_rules_tree() data = { 'data': { "block_rule_tree_json": rule_tree_tuple[0], "data_rule_tree_json": rule_tree_tuple[1] }, 'verify_error_msg': None, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } else: data = { 'data': serializer.errors, 'verify_error_msg': None, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.RULE_DATA_VALID_ERROR } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def put(self): """!@brief update the rule information. two points need to check before update the rule information 1 the rule name is not existing in the cp 2 the identifier(識別子)name is not existing in the cp @param @pre @post @note @return data rule tree and block rule tree and verify error message @author Gin Chen @date 2017/12/25 """ try: rule_id = int(views_helper.get_request_value(self.request, 'rule_id', 'GET')) insert_info = views_helper.get_request_value(self.request, 'rule_info', 'BODY') policy_id = insert_info['coll_policy'] name = insert_info['name'] query_set_len = len(CollPolicyCliRule.objects.filter(Q(name=name) & Q(coll_policy=policy_id) & ~Q(ruleid=rule_id))) error_msg_list = {} if query_set_len > 0: error_msg_list.update({'rule_name': False}) else: error_msg_list.update({'rule_name': True}) identifier_is_existing = self.__judge_identifier_name_exist(insert_info['key_str'], policy_id, rule_id=rule_id) if identifier_is_existing: error_msg_list.update({'key_str_name': False}) else: error_msg_list.update({'key_str_name': True}) if query_set_len > 0 or identifier_is_existing: data = { 'data': '', 'new_token': self.new_token, 'verify_error_msg': error_msg_list, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.FAILED } } else: old_rule_obj = CollPolicyCliRule.objects.get(ruleid=rule_id) rule_data_dict = self.__set_input_rule_data(insert_info) serializer = CollPolicyCliRuleSerializer(instance=old_rule_obj, data=rule_data_dict) if serializer.is_valid(Exception): serializer.save() new_name = serializer.data['name'] policy_tree = Policy_tree(policy_id) rule_tree_tuple = policy_tree.get_rules_tree() data = { 'data': { "new_name": new_name, "block_rule_tree_json": rule_tree_tuple[0], "data_rule_tree_json": rule_tree_tuple[1] }, 'verify_error_msg': None, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } else: data = { 'data': '', 'new_token': self.new_token, 'verify_error_msg': None, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.RULE_DATA_VALID_ERROR } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get(self): """!@brief When click Column button in 新规页面, return all table or return history data and policy tree data according to the table id @post return all data or history data and policy tree data @return data: all data or history data and policy tree data """ try: if self.table_id is not '': tvs = TableViewsSet(request=self.request) data_history = tvs.get_info_by_table_id( self.table_id, history_need_flag=False) # get tree information pt = Policy_tree(self.policy_id) pt_data = pt.get_policy_tree() data = { 'data': { 'table_name': DataTable.objects.get(table_id=self.table_id).name, 'table_id': DataTable.objects.get(table_id=self.table_id).table_id, 'data_history': data_history, 'policy_tree': pt_data, }, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS }, } return api_return(data=data) dt_query = DataTable.objects.all() dt = dt_query.values('table_id', 'name', 'coll_policy__name', 'descr', 'groups', 'tree', 'coll_policy') data_all = [] for per_dt in dt: result = { 'name': per_dt['name'], 'table_id': per_dt['table_id'], 'group_id': per_dt['groups'], 'tree_id': per_dt['tree'], 'policy_name': per_dt['coll_policy__name'], 'policy_id': per_dt['coll_policy'], 'desc': per_dt['descr'] } data_all.append(result) paginator = Paginator(data_all, int(self.max_size_per_page)) contacts = paginator.page(int(self.page_from)) data = { 'data': { 'data': contacts.object_list, }, 'new_token': self.new_token, 'num_page': paginator.num_pages, 'page_range': list(paginator.page_range), 'page_has_next': contacts.has_next(), 'total_num': len(contacts.object_list), 'current_page_num': contacts.number, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS }, } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)