标签:django 应用
基于上一篇环境,本篇详细叙述 中控机通过API获取主机列表的内容。
在API中定义获取主机列表的url
from web_api import views urlpatterns = [ url(r‘untreated_server/‘, views.untreated_server), ]
在views中创建响应的函数
views.py import json from django.shortcuts import HttpResponse from web_api.bll import api_manager # ##### 获取需要今日未采集数据的服务器 ##### def untreated_server(request): ret = {‘status‘: 0, ‘message‘: ‘‘, ‘data‘: ‘‘} try: result = api_manager.get_untreated_servers() if result.status: ret[‘data‘] = result.data ret[‘status‘] = 1 else: raise Exception(result.message) except Exception, e: ret[‘message‘] = str(e) return HttpResponse(json.dumps(ret))
在views中调用web_api.bll中的api_manager.get_untreated_servers()函数进行具体的列表获取操作动作
from web_api.dal import asset as dal_asset from web_api.dal import server as dal_server from web_api.dal import nic as dal_nic from web_api.dal import memory as dal_memory from web_api.dal import disk as dal_disk from web_api.dal import user_profile as dal_user_profile from web_api.dal import handle_log as dal_handle_log from backend.response.base_response import BaseResponse from backend.commons import agorithm from django.db.models import Q import datetime def get_untreated_servers(): response = BaseResponse()#实例化BaseResponse类,此类接受数据及用户状态 try: current_date = datetime.date.today()#获取当前日期 values = (‘server__hostname‘,) condition = Q()#生成condition对象 con_date = Q()#实例化Q con_date.connector = ‘OR‘#连接Q con_date.children.append(("latest_date__lt", current_date))#更新时间 con_date.children.append(("latest_date", None)) con_status = Q() # 在线状态的服务器(1000:上架;1001:在线) con_status.children.append((‘device_status__code‘, ‘1001‘)) condition.add(con_date, ‘AND‘)#更新时间标记 condition.add(con_status, ‘AND‘)#更新状态 result = dal_asset.get_q(condition, *values)#获取当前数据 result = list(result) response.status = True#状态修改 response.data = result except Exception, e: response.message = str(e) return response #讲所有数据返回
BaseResponse类的内容如下:
class BaseResponse(object): def __init__(self): self.status = False self.message = ‘‘ self.data = None
至此,API配置基本完成,中控机进行获取即可
django02_02 CMDB_api_untreated_server (附源码)
标签:django 应用
原文地址:http://xiaofangliu.blog.51cto.com/4766193/1693015