def param_check(data, must_dict=None, optional_dict=None, extra=False): """ api参数校验函数 :param data: dict,被校验的数据 :param must_dict: dict, 必要参数字典 :param optional_dict: dict,可选参数字典 :param extra: bool, 是否能存在额外的参数 :return: 校验失败就抛异常 """ if not isinstance(data, dict): raise ParameterErrorException("data参数:{},不是字典!".format(data)) schema_dict = {} if must_dict: for key, val in must_dict.items(): schema_dict[Required(key)] = val if optional_dict: for key, val in optional_dict.items(): schema_dict[key] = val schema = Schema(schema_dict, extra=extra) try: schema(data) except MultipleInvalid as e: raise ParameterErrorException("参数校验失败,{}!".format(e.errors))
def check_admin_name(admin_name): StrCheck.check_not_null(admin_name) if len(admin_name) > const.NAME_CHAR_LEN: raise ParameterErrorException("管理员名称长度只能为[1-{}]个字符!".format( const.NAME_CHAR_LEN)) if not re.match(r'[\u4e00-\u9fa5a-zA-Z0-9]+$', admin_name): raise ParameterErrorException("管理员名称只能为中文、大小写字母、数字!")
def check_navigation_bar_icon(icon): StrCheck.check_not_null(icon) if len(icon) > const.NAVIGATION_BAR_ICON_CHAR_LEN: raise ParameterErrorException("导航栏图标字符长度只能为[1-{}]个字符!".format( const.NAVIGATION_BAR_ICON_CHAR_LEN))
def check_remark(remark): StrCheck.check_is_str(remark) if len(remark) > const.REMARK_CHAR_LEN: raise ParameterErrorException("备注字符长度只能为[0-{}]个字符!".format( const.REMARK_CHAR_LEN))
def check_password(password): StrCheck.check_not_null(password) if len(password) > const.PASSWORD_CHAR_LEN: raise ParameterErrorException("密码字符长度只能为[1-{}]个字符!".format( const.PASSWORD_CHAR_LEN))
def check_valve_type(data): valve_type_list = [ Valve.SHARE_TYPE, Valve.INLINE_TYPE, Valve.INDEPENDENT_TYPE ] if data not in valve_type_list: raise ParameterErrorException("没有该阀门类型:{}!", data)
def check_msg(url): StrCheck.check_not_null(url) if len(url) > const.MSG_CHAR_LEN: raise ParameterErrorException("信息字符长度只能为[1-{}]个字符!".format( const.MSG_CHAR_LEN))
def check_configure_name(url): StrCheck.check_not_null(url) if len(url) > const.NAME_CHAR_LEN: raise ParameterErrorException("配置名称字符长度只能为[1-{}]个字符!".format( const.NAME_CHAR_LEN))
def check_value(url): StrCheck.check_not_null(url) if len(url) > const.VALUE_CHAR_LEN: raise ParameterErrorException("值类型字符长度只能为[1-{}]个字符!".format( const.VALUE_CHAR_LEN))
def check_action_type(url): StrCheck.check_not_null(url) if len(url) > const.ACTION_TYPE_CHAR_LEN: raise ParameterErrorException("动作类型字符长度只能为[1-{}]个字符!".format( const.ACTION_TYPE_CHAR_LEN))
def check_auth_permission_action(action): StrCheck.check_not_null(action) if len(action) > const.AUTH_PERMISSION_ACTION_CHAR_LEN: raise ParameterErrorException("权限行为名称字符长度只能为[1-{}]个字符!".format( const.AUTH_PERMISSION_ACTION_CHAR_LEN))
def check_role_name(name): StrCheck.check_not_null(name) if len(name) > const.NAME_CHAR_LEN: raise ParameterErrorException("权限名称字符长度只能为[1-{}]个字符!".format( const.NAME_CHAR_LEN))
def check_auth_category_name(name): StrCheck.check_not_null(name) if len(name) > const.AUTH_CATEGORY_NAME_CHAR_LEN: raise ParameterErrorException("导航栏名称字符长度只能为[1-{}]个字符!".format( const.AUTH_CATEGORY_NAME_CHAR_LEN))
def check_navigation_bar_name(name): StrCheck.check_not_null(name) if len(name) > const.NAVIGATION_BAR_NAME_CHAR_LEN: raise ParameterErrorException("导航栏名称字符长度只能为[1-{}]个字符!".format( const.NAVIGATION_BAR_NAME_CHAR_LEN))
def check_region_can_del(region_id): """ 判断该DTU区间是否可被删除,不能删除就抛异常 :param region_id: :return: """ region = conf_region_api.find_region_by_id(region_id) if region.used_num != 0: raise ParameterErrorException("供气商名称:{},DTU起始编号为:{}的区间上已经存在DTU,删除失败!")
def check_alarm_type(alarm_type): type_list = [ AlarmLog.ALARM_EXCEED_LIMIT, AlarmLog.ALARM_SUB_VALVE, AlarmLog.ALARM_INTERRUPT, AlarmLog.ALARM_SENSOR_ERROR, AlarmLog.ALARM_VALVE_ERROR ] if alarm_type not in type_list: raise ParameterErrorException("没有该警报类型:{}!", alarm_type)
def check_alarm_state(state): state_list = [ AlarmLogReader.STATE_READ, AlarmLogReader.STATE_UNREAD, ] if state not in state_list: raise ParameterErrorException("没有该警报状态:{}!", state)
def check_opr_state(state): state_list = [ OprLog.SUCCESS_STATE, OprLog.ERROR_STATE, OprLog.WAITE_STATE, ] if state not in state_list: raise ParameterErrorException("没有该操作状态:{}!", state)
def check_role_type(role_type): role_type_list = [ RoleType.ADMIN, RoleType.MANUFACTURER, RoleType.DTU_USER ] for now_role in role_type_list: if now_role == role_type: return raise ParameterErrorException("{} 不属于系统的基本角色:{}", format(role_type, str(role_type_list)))
def check_user_state_type(state_type): state_type_list = [ UserStateType.ENABLE_STATE, UserStateType.FORBIDDEN_STATE ] for now_state in state_type_list: if now_state == state_type: return raise ParameterErrorException("{} 不属于用户状态的基本类型:{}", format(state_type, str(state_type_list)))
def check_online_state(state_type): state_type_list = [ const.STATE_ONLINE, const.STATE_OFFLINE, const.UNKNOWN_STATE ] for now_state in state_type_list: if now_state == state_type: return raise ParameterErrorException("{} 不属于仪表在线状态的基本类型:{}", format(state_type, str(state_type_list)))
def check_owe_state(state_type): state_type_list = [ const.OWE_STATE_TRUE, const.OWE_STATE_FALSE, const.UNKNOWN_VALUE ] for now_state in state_type_list: if now_state == state_type: return raise ParameterErrorException("{} 不属于欠费状态的基本类型:{}", format(state_type, str(state_type_list)))
def check_battery_pressure_state(state_type): state_type_list = [ const.BATTERY_PRESSURE_STATE_ERROR, const.BATTERY_PRESSURE_STATE_NORMAL, const.UNKNOWN_VALUE ] for now_state in state_type_list: if now_state == state_type: return raise ParameterErrorException("{} 不属于电池欠压状态的基本类型:{}", format(state_type, str(state_type_list)))
def check_recharge_state(state_type): state_type_list = [ const.RECHARGE_STATE_OPEN, const.RECHARGE_STATE_CLOSE, const.UNKNOWN_VALUE ] for now_state in state_type_list: if now_state == state_type: return raise ParameterErrorException("{} 不属于预充值状态的基本类型:{}", format(state_type, str(state_type_list)))
def check_valve_error_flag(flag_type): flag_type_list = [ const.VALVE_ERROR_FLAG_TRUE, const.VALVE_ERROR_FLAG_FALSE, const.UNKNOWN_VALUE ] for now_flag in flag_type_list: if now_flag == flag_type: return raise ParameterErrorException("{} 不属阀门错误标志的基本类型:{}", format(flag_type, str(flag_type_list)))
def check_sensor_state(state_type): state_type_list = [ const.SENSOR_ERROR_FLAG_TRUE, const.SENSOR_ERROR_FLAG_FALSE, const.UNKNOWN_VALUE ] for now_state in state_type_list: if now_state == state_type: return raise ParameterErrorException("{} 不属于传感器状态的基本类型:{}", format(state_type, str(state_type_list)))
def update_dtu_region(region_info): must_dict = { "id": int, "total_num": IntCheck.check_is_positive_int, } param_check(region_info, must_dict) region = conf_region_api.find_region_by_id(region_info['id']) total_num = region_info['total_num'] # 先判断total_num是否合法 if core.is_total_num_legal(region, total_num): core.update_region_total_num(region, total_num) else: raise ParameterErrorException("DTU最大数目:{},太大,更新失败!".format(total_num))
def check_opr_type(opr_type): type_list = [ Operator.QUERY, Operator.RECHARGE, Operator.RESET, Operator.SET_METER_ADDRESS, Operator.CLOSE_VALVE, Operator.OPEN_VALVE, Operator.CLOSE_RECHARGE, Operator.OPEN_RECHARGE, Operator.SET_FLOW_RATIO, ] if opr_type not in type_list: raise ParameterErrorException("没有该操作类型:{}!", opr_type)
def __open_read_sheet(file_name, sheet_name=None): """ 打开一个用于读的excel工作表 :param file_name: :return: """ try: # 打开文件,获取excel文件的workbook(工作簿)对象 workbook = xlrd.open_workbook(file_name) if sheet_name is None: # 默认读取第一个单元格的内容 worksheet = workbook.sheet_by_index( ExcelUtils.__FIRST_WORK_SHEET) else: # 通过sheet_name获得一个工作表对象 worksheet = workbook.sheet_by_name(sheet_name) return worksheet except Exception as e: logging.error(e) raise ParameterErrorException("读取EXCEL文件失败,该文件不是EXCEL格式!")
def check_phone(phone): StrCheck.check_not_null(phone) if not re.match(r'[0-9]{11}$', phone): raise ParameterErrorException("{},不是合法的电话格式!".format(phone))