def get(self, request): feedback = { 'permission': True } try: sn = request.GET.get('sn') if sn is None: feedback['data'] = ErrorCode.parameter_missing('sn') raise natrix_exception.ParameterMissingException(parameter='sn') try: device = TerminalDevice.objects.get(sn=sn) logger.debug('device info {}'.format(device)) except TerminalDevice.DoesNotExist: feedback['data'] = ErrorCode.parameter_invalid( parameter='sn', reason=u'The record is not exist') raise natrix_exception.ParameterInvalidException(parameter='sn') try: serializer = alive_serializer.AdavnceHardwareSerializer(instance=device) feedback['data'] = { 'code': 200, 'message': u'终端设备硬件信息', 'info': serializer.data } return JsonResponse(data=feedback) except natrix_exception.BaseException as e: feedback['data'] = ErrorCode.sp_db_fault(aspect=u'Terminal Hardware serializer error') raise natrix_exception.ClassInsideException(message=u'Terminal Hardware serializer error') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def get(self, request): feedback = { 'permission': True } try: terminal_id = request.GET.get("id") if terminal_id is None: feedback['data'] = ErrorCode.parameter_missing('id') raise natrix_exception.ParameterMissingException(parameter='id') try: terminal = Terminal.objects.get(id=terminal_id) except TerminalDevice.DoesNotExist: feedback['data'] = ErrorCode.parameter_invalid(parameter='id', reason=u'数据库中不存在响应数据') raise natrix_exception.ParameterInvalidException(parameter='id') terminal_info = { "mac": terminal.mac, "type": terminal.type, "localip": terminal.localip, "publicip": terminal.publicip, "netmask": terminal.netmask, "gateway": terminal.gateway, "status": terminal.status, } feedback["data"] = { "code": 200, "message": u"监测点详情查询成功!", "info": terminal_info } except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def post(self, request): feedback = {'permission': True} try: post_data = request.data parent = post_data.get('parent', None) is_paginate = post_data.get('is_paginate', None) search = post_data.get('search', '') pagenum = post_data.get('pagenum', 1) if parent is None: feedback['data'] = ErrorCode.parameter_missing('parent') raise natrix_exception.ParameterMissingException( parameter='parent') if is_paginate is None: feedback['data'] = ErrorCode.parameter_missing('is_paginate') raise natrix_exception.ParameterMissingException( parameter='is_paginate') try: org_parent = Organization.objects.get(id=parent) except Organization.DoesNotExist: feedback['data'] = ErrorCode.parameter_invalid( 'parent', reason=u'数据不存在!') raise natrix_exception.ParameterInvalidException( parameter='parent') origin_orgs = [] parents = [org_parent] while len(parents) > 0: children = list( Organization.objects.filter(parent__in=parents)) origin_orgs.extend(children) parents = children organizations = [] if search: for orgitem in origin_orgs: if search in orgitem.name: organizations.append(orgitem) continue networks = orgitem.networks.all() for net in networks: if search in net.segment: organizations.append(orgitem) break else: organizations = origin_orgs data = {'code': 200, 'message': u'职场信息列表'} if is_paginate: per_page = user_api.get_per_page(request) painator = Paginator(organizations, per_page) try: organizations = painator.page(pagenum) except EmptyPage: organizations = painator.page(1) except PageNotAnInteger: organizations = painator.page(painator.num_pages) data['page_count'] = painator.num_pages data['page_num'] = organizations.number organizations_info = [] for org in organizations: organizations_info.append({ 'id': org.id, 'name': org.name, 'parent': org.parent_full_name(), 'level': org.level, 'children_num': len(org.get_children()) }) data['info'] = organizations_info feedback['data'] = data except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)