def get(self, request): feedback = {'permission': True} try: broadband_id = request.GET.get('id', None) try: serializer = IDSerializer(data=request.GET) if serializer.is_valid(Broadband): broadband = serializer.get_db() else: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=json.dumps(serializer.errors, ensure_ascii=False)) raise natrix_exception.ParameterInvalidException( parameter='id') broadband_info = dict() broadband_info["name"] = broadband.name broadband_info["operator"] = broadband.operator.name broadband_info[ "operator_verbosename"] = orgconf.OPERATOR_DICT.get( broadband.operator.name, {}).get('verbose_name', '') broadband_info["access_type"] = broadband.access_type broadband_info[ "access_type_verbosename"] = orgconf.BROADBAND_INFO.get( broadband.access_type, {}).get('verbose_name', '') broadband_info["speed"] = broadband.speed broadband_info["start_time"] = broadband.start_time broadband_info["end_time"] = broadband.end_time broadband_info["staff_contact"] = broadband.staff_contact broadband_info[ "staff_contact_telephone"] = broadband.staff_contact_telephone broadband_info[ "staff_contact_email"] = broadband.staff_contact_email broadband_info["isp_contact"] = broadband.isp_contact broadband_info[ "isp_contact_telephone"] = broadband.isp_contact_telephone broadband_info[ "isp_contact_email"] = broadband.isp_contact_email broadband_info["comment"] = broadband.comment feedback['data'] = { "code": 200, "message": u"宽带详情查询成功!", "info": broadband_info } except Broadband.DoesNotExist: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=u'数据库中不存在相应数据') raise natrix_exception.ParameterInvalidException( parameter='id') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def post(self, request, format=None): feedback = {'permission': True} user = request.user_rbac.user group = request.user_rbac.group try: post_data = request.data serializer = organization_serializer.OrganizationSerializer( user=user, group=group, data=post_data) if serializer.is_valid(): serializer.save() else: logger.error('Create organization error: {}'.format( serializer.format_errors())) feedback['data'] = ErrorCode.parameter_invalid( 'Organization Creation', serializer.format_errors()) raise natrix_exception.ParameterInvalidException( parameter='Organization Creation') feedback['data'] = {'code': 200, 'message': u'职场添加成功!'} except natrix_exception.BaseException as e: logger.info(e.get_log()) except Exception as e: natrix_exception.natrix_traceback() logger.error('There is an uncatch expection: {}'.format(e)) feedback['data'] = ErrorCode.sp_code_bug(str(e)) return JsonResponse(data=feedback)
def post(self, request, format=None): feedback = {'permission': True} try: post_data = request.data serializer = common_serializer.TerminalFilterSerializer( data=post_data) if serializer.is_valid(): res = serializer.query_result() feedback['data'] = { 'code': 200, 'message': 'Terminal list info', 'info': res } else: logger.error('Parameter Error: {}'.format( serializer.format_errors())) feedback['data'] = ErrorCode.parameter_invalid( 'terminallist', reason=serializer.format_errors()) except Exception as e: natrix_exception.natrix_traceback() logger.error('Common terminal list post ERROR') feedback['data'] = ErrorCode.sp_code_bug( 'Common terminal list post') return JsonResponse(data=feedback)
def post(self, request, format=None): feedback = {'permission': True} try: post_data = request.data serializer = common_serializer.TerminalFilterSerializer( data=post_data) if serializer.is_valid(): terminals = serializer.query_terminals() feedback['data'] = { 'code': 200, 'message': 'Terminal count info', 'info': { 'alive': len(terminals) } } else: logger.error('Parameter Error: {}'.format( serializer.format_errors())) feedback['data'] = ErrorCode.parameter_invalid( 'terminalcount', reason=serializer.format_errors()) except Exception as e: logger.error('Common terminal count post ERROR: {}'.format(e)) feedback['data'] = ErrorCode.sp_code_bug(e) return JsonResponse(data=feedback)
def get(self, request, format=None): feedback = {'permission': True} try: id = request.GET.get('id') try: org_instance = Organization.objects.get(id=id) serializer = organization_serializer.OrganizationSerializer( instance=org_instance) feedback['data'] = { 'code': 200, 'message': u'Terminal device summary info!', 'info': serializer.summary_presentation() } except Organization.DoesNotExist: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=u'Organization record is not exist!') raise natrix_exception.ParameterInvalidException( parameter='id') except natrix_exception.BaseException as e: feedback['data'] = ErrorCode.sp_code_bug(e.get_log()) raise natrix_exception.ClassInsideException( message=e.get_log()) except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
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_children(request): feedback = {'permission': True} try: data = {'id': request.GET.get('parent', None)} serializer = IDSerializer(data=data) if serializer.is_valid(Organization): organization = serializer.get_db() else: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=json.dumps(serializer.errors, ensure_ascii=False)) raise natrix_exception.ParameterInvalidException( parameter='id') children_info = [] for item in organization.get_children(): child_info = dict() child_info['id'] = item.id child_info['name'] = item.name child_info['level'] = item.level child_info['comment'] = item.comment children_info.append(child_info) feedback['data'] = { 'code': 200, 'message': u'子组织列表信息', 'info': children_info } except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def get(self, request): feedback = { 'permission': True } try: get_data = request.GET serializer = terminal_serializer.TerminalPostSerializer(data=get_data) if serializer.is_valid(): per_page = self.get_per_page(request) data = serializer.query_result(per_page=per_page) feedback['data'] = { 'code': 200, 'message': u'Terminal post info list', } feedback['data'].update(data) else: feedback['data'] = ErrorCode.parameter_invalid( 'terminal_post_search', reason=serializer.format_errors() ) raise natrix_exception.ParameterInvalidException(parameter='terminal_post_search') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def get(self, request): feedback = { 'permission': True } try: get_data = { 'type': request.GET.get('type'), 'filter': request.GET.getlist('filter'), 'show_level': request.GET.get('show_level') } serializer = terminal_serializer.OverviewQuerySerializer(data=get_data) if serializer.is_valid(): rest_data = serializer.query_result() feedback['data'] = { 'code': 200, 'message': u'终端设备分布', 'info': rest_data } else: feedback['data'] = ErrorCode.parameter_invalid( parameter='Overview get parameter', reason=serializer.format_errors() ) raise natrix_exception.ParameterInvalidException(parameter=serializer.format_errors()) 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 serializer = terminal_serializer.TerminalListQuerySerializer(data=post_data) if serializer.is_valid(): terminals = serializer.query_result() is_paginate = serializer.data.get('is_paginate') if is_paginate: pagenum = serializer.data.get('pagenum', 1) per_page = self.get_per_page(request) paginator = Paginator(terminals, per_page) try: current_page_query = paginator.page(pagenum) except PageNotAnInteger: current_page_query = paginator.page(1) except EmptyPage: current_page_query = paginator.page(paginator.num_pages) terminals = current_page_query feedback['data'] = { 'code': 200, 'message': u'Terminal list info', 'page_num': current_page_query.number, 'page_count': paginator.num_pages, } else: feedback['data'] = { 'code': 200, 'message': u'Terminal list info', } terminal_list = [] for record in terminals: serializer = alive_serializer.InterfaceSerializer(instance=record) terminal_list.append(serializer.data) feedback['data']['info'] = terminal_list else: feedback['data'] = ErrorCode.parameter_invalid( 'terminal_list', reason=serializer.format_errors() ) raise natrix_exception.ParameterInvalidException(parameter='terminal_list') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def delete(self, request, format=None): feedback = {'permission': True} try: user = request.user_rbac.user group = request.user_rbac.group post_data = request.GET serializer = IDSerializer(data=post_data) if serializer.is_valid(Organization): organization = serializer.get_db() if organization.id == 1: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=u'删除组织结构不存在') raise natrix_exception.ParameterInvalidException( parameter='id') elif organization.get_children().count() > 0: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=u'该组织存在子组织,不能删除') raise natrix_exception.ParameterInvalidException( parameter='id') else: organization.delete(user, group) feedback['data'] = {'code': 200, 'message': u'组织删除成功!'} else: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=json.dumps(serializer.errors, ensure_ascii=False)) raise natrix_exception.ParameterInvalidException( parameter='id') except natrix_exception.ClassInsideException as e: logger.info(e.get_log()) feedback['data'] = ErrorCode.sp_code_bug('IDSerializer error') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def post(self, request, format=None): feedback = { 'permission': True } try: post_data = request.data serializer = terminal_serializer.DeviceListQuerySerializer(data=post_data) if serializer.is_valid(): terminal_devices = serializer.query_result() is_paginate = post_data.get('is_paginate', False) if is_paginate: per_page = self.get_per_page(request) pagenum = post_data.get('pagenum', 1) paginator = Paginator(terminal_devices, per_page) try: current_page_query = paginator.page(pagenum) except PageNotAnInteger: current_page_query = paginator.page(1) except EmptyPage: current_page_query = paginator.page(paginator.num_pages) terminal_devices = current_page_query feedback['data'] = { 'code': 200, 'message': u'终端设备列表信息', 'page_num': current_page_query.number, 'page_count': paginator.num_pages, 'info': [] } else: feedback['data'] = { 'code': 200, 'message': u'全部终端设备信息', 'info': [] } for td in terminal_devices: device_serializer = terminal_serializer.DeviceBaiscSerializer(instance=td) feedback['data']['info'].append(device_serializer.data) else: feedback['data'] = ErrorCode.parameter_invalid( 'device_search', reason=serializer.format_errors() ) raise natrix_exception.ParameterInvalidException(parameter='device_search') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def get_cities(request): if request.method == 'POST': feedback = {'permission': True} try: post_data = json.loads(request.body) provinces = post_data.get('provinces') feedback['data'] = { 'code': 200, 'message': u'市列表信息', 'info': Region.get_cities(provinces) } except Exception as e: logger.info(e) feedback['data'] = ErrorCode.parameter_invalid('provinces') else: feedback = {'permission': False} return JsonResponse(data=feedback)
def get_full_path(request): """获取组织链信息 :param request: :return: """ feedback = {'permission': True} try: serializer = IDSerializer(data=request.GET) if serializer.is_valid(Organization): organization = serializer.get_db() else: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=json.dumps(serializer.errors, ensure_ascii=False)) raise natrix_exception.ParameterInvalidException( parameter='id') full_path_info = [] while True: if organization.level > 0: org_info = { "id": organization.id, "name": organization.name, "level": organization.level } full_path_info.insert(0, org_info) organization = organization.parent else: break feedback['data'] = { "code": 200, "message": u"获取组织全链成功!", "info": full_path_info } except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def put(self, request, format=None): feedback = { 'permission': True } try: put_data = request.data serializer = terminal_serializer.TerminalOperationSerializer(data=put_data) if serializer.is_valid(): serializer.action() feedback['data'] = { 'code': 200, 'message': u'终端监测点状态修改成功' } else: feedback['data'] = ErrorCode.parameter_invalid( 'terminal', reason=serializer.format_errors() ) raise natrix_exception.ParameterInvalidException(parameter='terminal') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def put(self, request, format=None): feedback = {'permission': True} try: user = request.user_rbac.user group = request.user_rbac.group post_data = request.data serializer = organization_serializer.OrganizationSerializer( user=user, group=group, data=post_data) if serializer.is_valid(): instance = serializer.save() feedback['data'] = {'code': 200, 'message': u'组织修改成功!'} else: logger.error('There are some errors: {}'.format( serializer.format_errors())) feedback['data'] = ErrorCode.parameter_invalid( 'organization change', serializer.format_errors()) 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)
def get(self, request): feedback = { 'permission': True } try: get_data = request.GET serializer = terminal_serializer.DeviceExceptionListQuerySerializer(data=get_data) if serializer.is_valid(): terminal_devices = serializer.query_result() is_paginate = get_data.get('is_paginate', False) if is_paginate: per_page = self.get_per_page(request) pagenum = get_data.get('pagenum', 1) paginator = Paginator(terminal_devices, per_page) try: current_page_query = paginator.page(pagenum) except PageNotAnInteger: current_page_query = paginator.page(1) except EmptyPage: current_page_query = paginator.page(paginator.num_pages) terminal_devices = current_page_query feedback['data'] = { 'code': 200, 'message': u'异常终端设备列表信息', 'page_num': current_page_query.number, 'page_count': paginator.num_pages, 'info': [] } else: feedback['data'] = { 'code': 200, 'message': u'全部异常终端设备信息', 'info': [] } for dev in terminal_devices: feedback['data']['info'].append( { 'sn': dev.sn, 'status': dev.status, 'reg_orgs': map(lambda item: {'id': item.id, 'name': item.name, 'desc': item.get_full_name()}, dev.register.organizations.all() if dev.register else []), 'detect_orgs': map(lambda item: {'id': item.id, 'name': item.name, 'desc': item.get_full_name()}, dev.organizations.all()), 'terminals': map(lambda item: {'name': item.name, 'local_ip': item.localip, 'status': item.status, 'is_active': item.is_active}, dev.terminal_set.all()) } ) else: feedback['data'] = ErrorCode.parameter_invalid( 'device_exception_search', reason=serializer.format_errors() ) raise natrix_exception.ParameterInvalidException(parameter='device_exception_search') except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)
def get(self, request, format=None): feedback = {'permission': True} try: serializer = IDSerializer(data=request.GET) if serializer.is_valid(Organization): organization = serializer.get_db() else: feedback['data'] = ErrorCode.parameter_invalid( 'id', reason=json.dumps(serializer.errors, ensure_ascii=False)) raise natrix_exception.ParameterInvalidException( parameter='id') # TODO: 用 REST-framework的方式取代 org_info = { "id": organization.id, "name": organization.name, "parent": organization.parent_full_name(), "level": organization.level, "comment": organization.comment } addresses_info = [] addresses = organization.get_addresses() for item in addresses: addr_info = { 'province': item.region.province, 'city': item.region.city, 'address': item.address, 'postcode': item.postcode } addresses_info.append(addr_info) contacts_info = [] contacts = organization.organizationcontact_set.all() for item in contacts: contact_info = { 'name': item.contact.name, 'telephone': item.contact.telephone, 'email': item.contact.email, 'wechat': item.contact.wechat, 'identity': item.identity, 'identity_verbosename': orgconf.IDENTITY_TYPE_INFO.get(item.identity, {}).get('verbose_name', '') } contacts_info.append(contact_info) networks_info = [] networks = organization.get_networks() for item in networks: network_info = { 'segment': item.segment, 'gateway': item.gateway, 'segment_type': item.segment_type, 'segment_type_verbosename': orgconf.SEGMENT_TYPES_INFO.get(item.segment_type, {}).get('verbose_name', ''), 'comment': item.comment } networks_info.append(network_info) broadbands_info = [] broadbands = organization.get_broadbands() for item in broadbands: broadband_info = { 'id': item.id, 'name': item.name, 'operator': item.operator.name, 'operator_verbosename': orgconf.OPERATOR_DICT.get(item.operator.name, {}).get('verbose_name', ''), 'access_type': item.access_type, 'access_type_verbosename': orgconf.BROADBAND_INFO.get(item.access_type, {}).get('verbose_name', ''), 'speed': item.speed, 'end_time': item.end_time, 'staff_contact': item.staff_contact, 'staff_contact_email': item.staff_contact_email, 'staff_contact_telephone': item.staff_contact_telephone, 'isp_contact': item.isp_contact, 'isp_contact_email': item.isp_contact_email, 'isp_contact_telephone': item.isp_contact_telephone, 'comment': item.comment } broadbands_info.append(broadband_info) exports_info = [] exports = organization.get_exports() for item in exports: export_info = { 'device': item.device, 'device_verbosename': orgconf.EXPORT_DEVICE_TYPE_INFO.get(item.device).get( 'verbose_name', ''), 'type': item.type, 'type_verbosename': orgconf.EXPORT_TYPE_INFO.get(item.type, {}).get('verbose_name', ''), 'ip': item.ip, 'comment': item.comment } exports_info.append(export_info) devices_info = [] register_list = organization.registerorganization_set.all() terminal_devices = set() for register in register_list: terminal_devices.update(register.terminaldevice_set.all()) for item in terminal_devices: terminals_info = [] terminals = item.terminal_set.all() for t in terminals: terminal_info = {"mac": t.mac, "id": t.id} terminals_info.append(terminal_info) device_info = { "sn": item.sn, "type": item.product, "piclient_version": item.natrixclient_version, "last_online_time": item.last_online_time, "terminals": terminals_info, "comment": item.comment } devices_info.append(device_info) children_info = [] children = organization.get_children() for item in children: child_info = { "name": item.name, "level": item.level, "comment": item.comment } children_info.append(child_info) feedback['data'] = { "code": 200, "message": u"组织详情查询详细信息!", "info": { "organization": org_info, "addresses": addresses_info, "contacts": contacts_info, "broadbands": broadbands_info, "networks": networks_info, "exports": exports_info, "devices": devices_info, "children": children_info } } except natrix_exception.BaseException as e: logger.info(e.get_log()) return JsonResponse(data=feedback)