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 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 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 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)