Beispiel #1
0
def date_field(data, field=None, required=True):
    field_data = _field(data, field, required)
    try:
        return datetime_help.parse_date(
            field_data) if field_data is not None else None
    except:
        raise exceptions.ValidationError('%s是ISO_8601格式的日期字符串' % field)
Beispiel #2
0
def datetime_field(data, field=None, required=True):
    field_data = _field(data, field, required)
    try:
        return datetime_help.parse_datetime(
            field_data) if field_data is not None else None
    except:
        raise exceptions.ValidationError(
            '{field}是ISO_8601格式的时间字符串'.format(field=field or data))
Beispiel #3
0
 def data(self):
     if not hasattr(self, '_data'):
         self._data = None
         if self.request.body:
             try:
                 self._data = json.loads(self.request.body.decode('utf-8'))
             except:
                 raise exceptions.ValidationError('请求的body是非json格式')
     return self._data
Beispiel #4
0
def list_field(data, field=None, required=True):
    """ list类型检查
    @param data 如果field不为None,那么field从data里取值,否则判断data是否为list类型
    @param field 如果不为None,那么需要从data里提取值
    @param required 是否data里必须存在field字段,如果字段不存在且required为False,返回None
    """
    field_data = _field(data, field, required)
    if not field_data and not required:
        return None
    if isinstance(field_data, str):
        try:
            field_data = json.loads(field_data)
        except:
            raise exceptions.ValidationError(
                '{field}是list类型'.format(field=field))
    if not isinstance(field_data, (list, set, tuple)):
        raise exceptions.ValidationError('{field}是list类型'.format(field=field))
    return list(field_data)
Beispiel #5
0
def _field(data, field, required):
    if field:
        data = data or {}
        if not isinstance(data, dict):
            raise exceptions.SystemError()
        if required and field not in data:
            raise exceptions.ValidationError('{field}必填'.format(field=field))
        return data.get(field)
    else:
        return data
Beispiel #6
0
def float_field(data, field=None, required=True):
    """ float类型检查
    @param data 如果field不为None,那么field从data里取值,否则判断data是否为float类型
    @param field 如果不为None,那么需要从data里提取值
    @param required 是否data里必须存在field字段,如果字段不存在且required为False,返回None
    """
    field_data = _field(data, field, required)
    if not field_data and not required:
        return None
    try:
        return float(field_data)
    except:
        raise exceptions.ValidationError('{field}是float类型'.format(field=field))
Beispiel #7
0
 def get_body(self, parse_json=True):
     """ 提取http请求的body数据
     @param parse_json 是否将body数据解析成json格式
     @return body http请求的body数据
     """
     body = self.request.body
     if not body:
         return None
     if parse_json:
         try:
             body = json.loads(body.decode('utf8'))
         except:
             exceptions.ValidationError(msg='请求body数据格式错误')
     return body
Beispiel #8
0
def bool_field(data, field=None, required=True):
    """ bool类型检查
    @param data 如果field不为None,那么field从data里取值,否则判断data是否为bool类型
    @param field 如果不为None,那么需要从data里提取值
    @param required 是否data里必须存在field字段,如果字段不存在且required为False,返回None
    """
    field_data = _field(data, field, required)
    if str(field_data).lower() == 'true':
        return True
    if str(field_data).lower() == 'false':
        return False
    if not required:
        return None
    raise exceptions.ValidationError('{field}是bool类型'.format(field=field))