def get(self): """!@brief Rest Api of GET, verify whether data table name was exist @return data: the status """ try: if self.name is not '': get_name_from_data_table = self.get_data_table( **{'name': self.name}) data = { constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } if len(get_name_from_data_table) > 0: data = { constants.STATUS: { constants.STATUS: constants.FALSE, constants.MSG_TYPE: 'NAME_DUPLICATE', constants.MESSAGE: constants.DATA_TABLE_NAME_DUPLICATE } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get_history(self, item_id, value_type, policy_type): """!@brief Get history data from history_%s_%s table @param item_id: item id @param value_type: value type(str, int, float, text) @param policy_type: policy type(cli, snmp) @note @return history: history data(type is dic) """ try: base_db_format = "history_%s_%s" table_name = base_db_format % (policy_type.lower(), value_type.lower()) where_condition = 'item_id = ' + str(item_id) with connection.cursor() as cursor: sql = "select * from %s where %s order by %s" % ( table_name, where_condition, '-clock') cursor.execute(sql) # cursor.execute("SELECT * FROM history_cli_str LIMIT 2") return self.dict_fetchall(cursor) # history = table.objects.filter(**kwargs).order_by("-clock") # if value_type not in trigger_numeric: # for h in history: # # h.value = "'" + h.value + "'" # h.value = str(h.value) # return history except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
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_execute_ing(self): """!@brief Get the status of whether is executing or not @pre call the api from Gin @post return the status @return execute_ing: status """ # { # "now_time": 1513312116, # "param": 2, # "param_type": 0 # 0: group 1: policy # } try: req_body = { 'now_time': time.time(), 'param': self.id, 'param_type': 0 } url = constants.VERIFY_WHETHER_EXECUTING_SERVER_URL % ( constants.VERIFY_WHETHER_EXECUTING_SERVER_IP, constants.VERIFY_WHETHER_EXECUTING_SERVER_PORT) headers = {'content-type': 'application/json'} resp = requests.post(url=url, data=json.dumps(req_body), headers=headers) if 200 <= resp.status_code <= 299: resp_body = json.loads(resp.text) item = resp_body.get('items') if item == constants.NUMBER_ZERO: self.execute_ing = False except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get(self): try: queryset = CollPolicy.objects.all().values('coll_policy_id', 'value_type', 'name', 'policy_type') if self.id != '': queryset = CollPolicy.objects.filter(ostype=self.id).values( 'coll_policy_id', 'value_type', 'name', 'policy_type') # serializer = CollPolicyNameSerializer(queryset, many=True) for per in queryset: if per['policy_type'] == 1: # SNMP per['name'] = '[SNMP]' + per['name'] else: # CLI per['name'] = '[CLI]' + per['name'] data = { 'data': list(queryset), '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 highlight function @param @pre @post @return the highlighted page @author Gin Chen @date 2017/12/18 """ request_dict = { 'data': self.raw_data, 'tree': self.tree, 'tree_id': self.tree_id } try: render = Render(**request_dict) html_data = render.render() data = { 'data': html_data, '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 Create policy group @return data: the status of whether create successful and the inserted data """ try: with transaction.atomic(): data = { 'name': self.name, 'desc': self.desc, 'ostypeid': self.ostype, } if self.name is not '': get_name_from_cpg = self.get_cp_group( **{'name': self.name}) if get_name_from_cpg is not False: data = { constants.STATUS: { constants.STATUS: constants.FALSE, constants.MSG_TYPE: 'NAME_DUPLICATE', constants.MESSAGE: constants. COLLECTION_POLICY_GROUP_NAME_DUPLICATE } } return api_return(data=data) cps = views_helper.get_request_value(self.request, 'cps', 'BODY') serializer = CollPolicyGroupSerializer(data=data) if serializer.is_valid(Exception): serializer.save() policy_group_data = [] for per_cp in cps: per_cp['policy_group'] = int( serializer.data.get('policy_group_id')) per_cp['history'] = time.time() per_cp['policy'] = per_cp['policy'] policy_group_data.append(per_cp) serializer_related = PolicyGroupSerializer( data=policy_group_data, many=True) if serializer_related.is_valid(Exception): serializer_related.save() data = { 'data': { 'data_coll_policy': serializer.data, 'data_policys_groups': serializer_related.data, }, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.POST_SUCCESSFUL } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def verify_column_bak(self, id, method='GET'): """!@brief Return the status of each column, decide whether should disable or enable in web page False: modify reject, True: modify or shown permit @param id: policy group id @param method: request method, default is GET @pre call when need to get the column status @post return column status @return verify_result: the status of each column """ try: self.get_execute_ing() if id is not '': queryset_pg = PolicysGroups.objects.filter( **{'policy_group_id': id}) verify_result = { 'desc': True, 'status': True, 'ostype': True, 'collection_policy_name': True, 'collection_policy_group_name': True, 'exec_interval': True, 'execute_ing': self.execute_ing, } if len(queryset_pg) > 0 or self.execute_ing: verify_result['ostype'] = False if self.execute_ing: verify_result['collection_policy_name'] = False verify_result['exec_interval'] = False return verify_result except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def delete(self): schedule_id = views_helper.get_request_value(self.request, 'id', 'GET') try: with transaction.atomic(): # close all function off thought update status =1 obj = Schedules.objects.get(schedule_id=schedule_id) all_function_status = obj.status # all function off is opened if all_function_status == 0 and obj.policy_group is None: device_group_id = obj.device_group Schedules.objects.filter( device_group=device_group_id).update(status=1) # delete items table # delete schedule table Items.objects.filter(schedule_id=schedule_id).delete() Schedules.objects.get(schedule_id=schedule_id).delete() data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } return api_return(data=data) except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get(self): """!@brief Rest Api of GET, get all the data for summary page or get the data according to table id @return data: the data for summary page """ try: if self.id is not '': data = self.get_info_by_table_id(self.id) return api_return(data=data) field_relation_ships = { 'table_id': 'id', 'name': 'name', 'descr': 'descr', } query_data = { 'name': self.name, 'desc': self.desc, } search_fields = ['name', 'desc'] sorts, search_conditions = views_helper.get_search_conditions( self.request, field_relation_ships, query_data, search_fields) total_num = len(DataTable.objects.all()) if search_conditions: queryset = DataTable.objects.filter( **search_conditions).values(*[ 'table_id', 'descr', 'name', 'coll_policy', 'groups', 'tree', 'coll_policy__policy_type' ]).order_by(*sorts) else: queryset = DataTable.objects.all().values(*[ 'table_id', 'descr', 'name', 'coll_policy', 'groups', 'tree', 'coll_policy__policy_type' ]).order_by(*sorts) # serializer = ActionPolicyDataTableSerializer(queryset, many=True) paginator = Paginator(list(queryset), 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': total_num, '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)
def post(self): """!@brief add one schedule into schedule table,and many items into item table @author Gin Chen @date 2018/1/5 """ try: with transaction.atomic(): param_dict = self.__set_request_param() opt = DataCollectionOptCls(**param_dict) # ready devices_list, coll_policy_list = opt.add_new_schedule_ready() # check whether the tree of cp is null or not for cp_dict in coll_policy_list: cp_id = cp_dict['coll_policy_id'] if cp_dict['item_type'] == constants.ITEM_TYPE_CLI: cp_leaf_nodes = CollPolicyRuleTree.objects.filter( coll_policy=cp_id, is_leaf=constants.LEAF_NODE_MARK) if len(cp_leaf_nodes) == 0: data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.NULL_TREE_IN_CP } } return api_return(data=data) # insert schedule res = opt.insert_new_schedule(devices_list, coll_policy_list) if res[0]: # create item by schedule opt.insert_new_items(devices_list, res[1], coll_policy_list) data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } else: data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: res[1] } } 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 load the rule information when open the rule edit page @param @pre @post @return rule information,verify result(rule_is_used,is_processing,is_locked) @author Gin Chen @date 2017/12/25 """ try: coll_policy_id = views_helper.get_request_value(self.request, key='coll_policy_id', method_type='GET') rule_id = views_helper.get_request_value(self.request, key='rule_id', method_type='GET') query_set = CollPolicyRuleTree.objects.filter(rule=rule_id, coll_policy=coll_policy_id) is_used = False is_processing = False is_locked = False if len(query_set) > 0: is_processing = self.__judge_rule_is_processing(coll_policy_id) is_locked = self.__judge_rule_is_locked(coll_policy_id) is_used = True rule_info = CollPolicyCliRule.objects.get(ruleid=rule_id) result_dict = CollPolicyCliRuleSerializer(rule_info).data split_char = result_dict['split_char'] if split_char: if split_char == '@space@': result_dict['split_char'] = 4 result_dict['other_char'] = None elif split_char == ',': result_dict['split_char'] = 1 result_dict['other_char'] = None elif split_char == '/': result_dict['split_char'] = 2 result_dict['other_char'] = None else: result_dict['split_char'] = 3 result_dict['other_char'] = split_char data = { 'rule_is_used': is_used, 'is_processing': is_processing, 'is_locked': is_locked, 'data': result_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 get(self): """!@brief When select column2 after choosing column1 in 新规页面, column1 and column2 should be same with below 3 points: 1.the same device group 2.the same schedule_id(data_table_item-->item-->schedule) 3.the same policy execute inteval(data_table_item-->item-->policys_groups) @post return verify result @return data: return verify result """ try: if self.table_id_A is not '' and self.table_id_B is not '': # table_a = DataTable.objects.filter(table_id=self.table_id_A).values('groups', 'coll_policy__value_type', # 'coll_policy__policy_type') table_a_str = '' table_b_str = '' table_a = DataTableItems.objects.filter( table_id=self.table_id_A).values('table__groups', 'item__schedule', 'item__policys_groups') if table_a: table_a_str = str(table_a[0]['table__groups']) + '_' + str( table_a[0]['item__schedule']) + '_' + str( table_a[0]['item__policys_groups']) # table_b = DataTable.objects.filter(table_id=self.table_id_B).values('groups', 'coll_policy__value_type', # 'coll_policy__policy_type') table_b = DataTableItems.objects.filter( table_id=self.table_id_B).values('table__groups', 'item__schedule', 'item__policys_groups') if table_b: table_b_str = str(table_b[0]['table__groups']) + '_' + str( table_b[0]['item__schedule']) + '_' + str( table_b[0]['item__policys_groups']) if table_a_str == table_b_str: data = { constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS }, } return api_return(data=data) data = { constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.COLUMN_A_COLUMN_B_VERIFY_FAILED }, } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def decorator(request, *args, **kwargs): try: # username = views_helper.get_request_value(request, 'username', 'GET') username = request.session.get('_username') if username is None: data = { constants.STATUS: { constants.CODE: constants.TOKEN_NOT_EXIST_FOR_CURRENT_USER_CODE, constants.MESSAGE: constants.NO_USERNAME_OR_PASSWORD_FONUD_ERROR, constants.STATUS: constants.FALSE } } return HttpResponse(json.dumps(data)) # token = request.session[username + '_token'] # this token is from header new_token = views_helper.get_request_value(request, "HTTP_AUTHORIZATION", 'META') # this token is from session, for jqgrid token = request.session.get(username + '_token') if token is None: data = { constants.STATUS: { constants.CODE: constants.TOKEN_NOT_EXIST_FOR_CURRENT_USER_CODE, constants.MESSAGE: constants.TOKEN_NOT_EXIST_FOR_CURRENT_USER_MSG } } return HttpResponse(json.dumps(data)) if new_token is not '': token = request.META.get("HTTP_AUTHORIZATION").split()[1] refresh_token = TokenRefresh(token).refresh_token() if refresh_token is False: data = { constants.STATUS: { constants.MESSAGE: constants.TOKEN_EXPIRED_MSG, constants.CODE: constants.TOKEN_ALREADY_EXPIRED_CODE, constants.STATUS: constants.FALSE } } return HttpResponse(json.dumps(data)) if refresh_token[constants.CODE] == constants.REFRESH_CODE: request.session[username] = refresh_token[constants.TOKEN] if refresh_token: request.META[constants.NEW_TOKEN] = refresh_token return view(request, *args, **kwargs) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get(self, action_name=None): """!@brief Rest Api of GET, get all the data for summary page or get the data according to action name @return data: the data for summary page """ try: data = self.prepare_data_for_memory_cache(action_name) return api_return(data=data) except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def put(self): """!@brief commit the collection policy update information @param @pre @post @note @return the update information @author Gin Chen @date 2018/1/18 """ coll_policy_id = views_helper.get_request_value(self.request, "coll_policy_id", "BODY") name = views_helper.get_request_value(self.request, "coll_policy_name", "BODY") command = views_helper.get_request_value(self.request, "command", "BODY") desc = views_helper.get_request_value(self.request, "desc", "BODY") ostype = views_helper.get_request_value(self.request, "ostype", "BODY") coll_policy_update_data = { 'name': name, 'cli_command': command, 'desc': desc, 'ostype': ostype } if len(CollPolicy.objects.filter(~Q(coll_policy_id=coll_policy_id), name=name)): data = { 'data': '', 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MSG_TYPE: 'NAME_DUPLICATE', constants.MESSAGE: constants.COLLECTION_POLICY_NAME_DUPLICATE } } return api_return(data=data) obj = CollPolicy.objects.get(coll_policy_id=coll_policy_id) serializer = CollPolicyEditSerializer(instance=obj, data=coll_policy_update_data) try: if serializer.is_valid(): serializer.save() data = { 'data': serializer.data, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } return api_return(data=data) except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def csv_export(self): try: if self.id is not '': data_result = self.get_info_by_table_id(self.id) check_item_name = constants.EXPORT_CSV_TITLE_COLUMN_4 csv_data = [] if len(data_result['data']['data']): check_item_name = data_result['data']['data'][0][ 'checkitem'] if data_result['data']['data'][0]['item_type'].upper( ) == 'SNMP': check_item_name = 'Value' constants.EXPORT_CSV_TITLE_COLUMN_3 = 'OID' for per in data_result['data']['data']: if per['item_type'].upper() == 'CLI': csv_data.append([ per['hostname'], per['date'], per['path'], per['value'] ]) else: csv_data.append([ per['hostname'], per['date'], per['oid'], per['value'] ]) title = [ constants.EXPORT_CSV_TITLE_COLUMN_1, constants.EXPORT_CSV_TITLE_COLUMN_2, constants.EXPORT_CSV_TITLE_COLUMN_3, check_item_name ] script_dir = os.path.split(os.path.realpath(__file__))[0] csv_path = os.path.join( os.path.dirname( os.path.dirname( os.path.dirname(os.path.dirname(script_dir)))), constants.CSV_PATH) csv_data.insert(0, title) # create csv result = csv_export.csv_export(csv_path, csv_data) # download csv if result is False: data = { constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.CSV_PATH_NOT_EXIST }, } return api_return(data=data) return result except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get_schedule(**kwargs): """!@brief Get the data of Schedules table @param kwargs: dictionary type of the query condition @pre call when need data of Schedules table @post return Schedules data @return: data of Schedules table """ try: return Schedules.objects.filter(**kwargs) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def delete(self): """!@brief Rest Api of DELETE, delete data according to table id @return data: the status of whether delete successful """ try: with transaction.atomic(): kwargs = {'table_id': self.id} data_in_dp = self.get_data_table(**kwargs) data_in_trigger = self.get_trigger(self.id) data_table_item_info = self.get_data_table_item_for_delete( **{'table_id': self.id}) if len(data_in_dp) <= 0: data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.DATA_TABLE_NOT_EXIST_IN_SYSTEM } } return api_return(data=data) if len(data_in_trigger) > 0: data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.FALSE, constants.MESSAGE: constants.DATA_TABLE_EXIST_IN_TRIGGER } } return api_return(data=data) data_table_item_info.delete() data_in_dp.delete() data = { 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.DELETE_SUCCESSFUL } } return api_return(data=data) except Exception, e: transaction.rollback() 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 get_policy_group(**kwargs): """!@brief Get the data of PolicysGroups table @param kwargs: dictionary type of the query condition @pre call when need data of PolicysGroups table @post return PolicysGroups data @return: data of PolicysGroups table """ try: pg = PolicysGroups.objects.filter(**kwargs) return pg except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get_data_table(**kwargs): """!@brief Get the data of DataTable table @param kwargs: dictionary type of the query condition @pre call when need data of DataTable table @post return DataTable data @return result: data of DataTable table """ try: dt = DataTable.objects.filter(**kwargs) return dt except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get_trigger(id): """!@brief Get the data of Triggers table @param id: table id @pre call when need data of Triggers table @post return Triggers data @return result: data of Triggers table """ try: t = Triggers.objects.filter(Q(columnA=id) | Q(columnB=id)) return t except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def del_policy_group(self, **kwargs): """!@brief Delete policys_groups and coll_policy_group @param kwargs: dictionary type of the query condition @pre call when need to delete policys_groups @return: status """ try: pgs = self.get_policy_group(**kwargs) # if len(pgs) > 0: pgs.delete() # return True except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get_device_ids(**kwargs): """!@brief Get the device ids as group_id of the DevicesGroups table @param kwargs: dictionary type of the query condition @pre call when need to select DevicesGroups table @post according to the need to deal with the DevicesGroups table @note @return result: queryset of DevicesGroups table """ try: result = DevicesGroups.objects.filter(**kwargs).values('device') return result except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def run_request_method(resource_object): try: request_method = resource_object.request.method.lower() if request_method == 'post': return resource_object.post() elif request_method == 'get': return resource_object.get() elif request_method == 'put': return resource_object.put() elif request_method == 'delete': return resource_object.delete() elif request_method == 'patch': return resource_object.patch() except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def post(self): try: output = self.snmp_work() data = { 'data': { 'data': output, }, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.PUT_SUCCESSFUL } } return api_return(data=data) except Exception, e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get_data_table_item(**kwargs): """!@brief Get the queryset of the DataTableItems table @pre call when need to select DataTableItems table @post according to the need to deal with the DataTableItems table @note @return result: queryset of DataTableItems table """ try: result = DataTableItems.objects.filter( **kwargs).values('item__device__device_id').annotate( total_num=Count('item__device__device_id')) return result except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)
def get(self): """!@brief load the collection policy information when open the collection policy edit page from cp tree page @param @pre @post @note @return rule information,verify result @author Gin Chen @date 2018/1/18 """ try: coll_policy_id = views_helper.get_request_value(self.request, "coll_policy_id", "GET") obj = CollPolicy.objects.get(coll_policy_id=coll_policy_id) is_used = False if not Tool.get_policy_status(coll_policy_id): is_used = True is_not_in_group = True if len(PolicysGroups.objects.filter(policy=coll_policy_id)) > 0: is_not_in_group = False column_status={ 'name': True, 'desc': True, 'ostype': is_not_in_group, 'cli_command': is_used } serializer = CollPolicyEditSerializer(obj) data = { 'data': { 'policy_detail': serializer.data, 'verify_result': column_status }, 'new_token': self.new_token, constants.STATUS: { constants.STATUS: constants.TRUE, constants.MESSAGE: constants.SUCCESS } } return api_return(data=data) except Exception as e: if constants.DEBUG_FLAG: print traceback.format_exc(e) return exception_handler(e)