Beispiel #1
0
class ApplyParams(params.ParamSet):
    __datatype__ = "json"
    sn = params.RegexField(pattern="[a-zA-Z0-9-]+", required=True)
    hostname_prefix = params.RegexField(pattern="[a-zA-Z0-9-_]")
    hostname_pattern = params.RegexField(pattern="[a-zA-Z0-9*-_]")
    type = params.WordField(required=True)
    idc = params.WordField(required=True)
    network = params.Field()

    def validate_network(self, data):
        try:
            return IPv4Network(unicode(data))
        except Exception as e:
            raise ValidationError(str(e))

    def validate_type(self, data):
        try:
            return ServerType(data)
        except ValueError as e:
            raise ValidationError(str(e))

    def validate_hostname_pattern(self, value):
        if value.count("*") != 1:
            raise ValidationError('pattern must have 1 star(*) placeholder')
        return value
Beispiel #2
0
        class APIParams(params.ParamSet):
            id = params.IntegerField(PARAMS_ID_MSG, required=True, min=1)
            token = params.Field(PARAMS_TOKEN_MSG, required=True, length=32)

            tag = params.WordField(PARAMS_TAG_MSG, length=8, default='foo')
            from_ = params.WordField(PARAMS_FROM,
                                     key='from',
                                     required=False,
                                     length=16)
            text_anyway = params.WordField()
            text_not_null = params.WordField(null=False)
Beispiel #3
0
        class APIParams(params.ParamSet):
            __datatype__ = 'json'

            id = params.IntegerField(required=True, min=1)
            token = params.Field(required=True, length=32)
            headers = params.Field()

            tag = params.WordField(PARAMS_TAG_MSG, length=8, default='foo')
            from_ = params.WordField(PARAMS_FROM,
                                     key='from',
                                     required=False,
                                     length=16)
            text_anyway = params.WordField()
            text_not_null = params.WordField(null=False)
Beispiel #4
0
class FakeParams(params.ParamSet):
    id = params.IntegerField('wat are you?', required=True, min=0)
    name = params.WordField(
        'name should be a 1~8 length string, and is required',
        required=True,
        length=(1, 8))
    email = params.EmailField(
        'email should be a valid email format, and is required', required=True)
    content = params.Field('content should be a 1~20 length string',
                           length=(1, 20))
Beispiel #5
0
def test_words():
    f0 = params.WordField()
    f0.validate('')
    f0.validate('goodstr')
    with assert_raises(ValidationError):
        f0.validate('should not contain space')
    with assert_raises(ValidationError):
        f0.validate('andother*(*^&')

    f1 = params.WordField(length=(4, 8))
    f1.validate('asdf')
    f1.validate('asdfasdf')
    with assert_raises(ValidationError):
        f1.validate('s')
    with assert_raises(ValidationError):
        f1.validate('longggggg')

    f2 = params.WordField(null=False)
    with assert_raises(ValidationError):
        f2.validate('')
Beispiel #6
0
class AuthorizeParams(params.ParamSet):
    __datatype__ = "json"
    privilege_type = params.WordField(required=True)
    privilege_name = params.RegexField(pattern=r'[\w_-]+', required=True)
    username = params.Field()
    hostname = params.Field()
    node_id = params.IntegerField()
    token = params.Field()

    def validate_privilege_type(self, value):
        try:
            return get_privilege_by_name(value)
        except KeyError:
            raise errors.ValidationError("privilege_type %s not existed" %
                                         value)
Beispiel #7
0
class ApplyParams(params.ParamSet):
    __datatype__ = "json"
    node_id = params.IntegerField(required=True)
    privilege_type = params.WordField(required=True)
    privilege_names = params.ListField(item_field=params.RegexField(
        pattern=r'[\w_-]+'))
    token = params.Field()

    def validate_privilege_type(self, data):
        try:
            return get_privilege_by_name(data)
        except KeyError:
            raise errors.ValidationError("privilege_type %s not existed" %
                                         data)

    def validate_token(self, value):
        if value:
            if not decrypt_token(value, settings.AUTH_TOKEN_PREFIX):
                raise errors.ValidationError('Invalid token value')
        return value