Exemplo n.º 1
0
def check_intstring(args):
    kwgs = {}
    if len(args) > 2:
        kwgs = args[2]
    f = params.IntegerField(**kwgs)
    if args[1]:
        f.validate(args[0])
    else:
        assert_raises(ValidationError, f.validate, args[0])
Exemplo n.º 2
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))
Exemplo n.º 3
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)
Exemplo n.º 4
0
def test_type_list():
    list_field = params.ListField(item_field=params.IntegerField(min=1, max=9),
                                  choices=[1, 2, 3])

    list_field.validate(['1', '2', '3'])
    with assert_raises(ValidationError):
        list_field.validate(['0', '1', '2'])
    with assert_raises(ValidationError):
        list_field.validate(['1', '2', '3', '4'])
    with assert_raises(ValidationError):
        list_field.validate(['a', '2', '3'])
Exemplo n.º 5
0
class PetitionParams(params.ParamSet):
    __datatype__ = "json"
    privilege_id = params.IntegerField(required=True)
    privilege_name = params.RegexField(pattern=r'[\w_-]+', required=True)
    node_id = params.Field()
    privilege_model = params.Field()

    def validate_privilege_id(self, pid):
        self.data['privilege_model'] = PrivilegeType.query.options(
            undefer_group("audit")).get_or_raise(pid)
        self.data['node_id'] = int(self.privilege_model.node_id)
        return pid
Exemplo n.º 6
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)
Exemplo n.º 7
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)
Exemplo n.º 8
0
    class NodeServersPostParams(params.ParamSet):
        __datatype__ = 'json'
        server_ids = params.ListField('server_ids invalid',
                                      item_field=params.IntegerField(),
                                      required=True)
        traffic_ratio = params.Field("ratio invalid", required=True)

        def validate_traffic_ratio(self, value):
            try:
                if value is None:
                    return None
                ratio = str(value)
                v = D(ratio)
            except Exception as e:
                raise errors.ValidationError(str(e))
            return v
Exemplo n.º 9
0
class UserNodesHandler(APIHandler):
    @params.define_params(
        {'with_path': params.IntegerField(choices=(0, 1), default=0)})
    def get(self):
        # root_node = TreeNode(1)

        node_ids = self.user.get_accessible_nodes()

        if node_ids is None:
            self.write_data([])
            return

        if self.params.with_path:

            def format_node(i):
                return {
                    'text': i.name,
                    'id': i.id,
                    'parent': '#' if i.parent is 0 else i.parent,
                    'path': i.path,
                    'type': 'bottom' if i.type is NodeType.leaf else 'node',
                }
        else:

            def format_node(i):
                return {
                    'text': i.name,
                    'id': i.id,
                    'parent': '#' if i.parent is 0 else i.parent,
                    'type': 'bottom' if i.type is NodeType.leaf else 'node',
                }

        node_context = _Context()
        data_dict = {}

        for i in node_ids:
            tn = TreeNode(i, _context=node_context)
            # Get child nodes
            for n in tn.dfs_generator():
                if n.id not in data_dict:
                    data_dict[n.id] = format_node(n)
            # Get parent nodes
            for n in tn.parents:
                if n.id not in data_dict:
                    data_dict[n.id] = format_node(n)

        self.write_data(data_dict.values())
Exemplo n.º 10
0
class NodesHandler(APIHandler):
    @params.define_params(
        {'with_path': params.IntegerField(choices=(0, 1), default=0)})
    def get(self):
        #nodes = zk.without_expire.get_tree_meta()
        #format_nodes(nodes)
        #self.write_data(nodes)

        root_node = TreeNode(1)

        if self.params.with_path:
            data = [{
                'text': i.name,
                'id': i.id,
                'parent': '#' if i.parent is 0 else i.parent,
                'path': i.path,
                'type': 'bottom' if i.type is NodeType.leaf else 'node',
            } for i in root_node.dfs_generator()]
        else:
            data = [{
                'text': i.name,
                'id': i.id,
                'parent': '#' if i.parent is 0 else i.parent,
                'type': 'bottom' if i.type is NodeType.leaf else 'node',
            } for i in root_node.dfs_generator()]
        self.write_data(data)

    @require_node_privileges(NodePrivilege.create_node,
                             lambda c: int(c.handler.get_argument('parent_id'))
                             )
    def post(self):
        parent_id = int(self.get_argument('parent_id'))
        name = self.get_argument('name')
        if not re.match(u'^[\u4e00-\u9fa5\w-]+$', name):
            raise errors.ParamsInvalidError(
                'name could only contains character')
        node_id = zk.get_next_id()
        try:
            zk.add_tree_node(dict(
                id=node_id,
                pId=parent_id,
                name=name,
            ))
        except ValueConflictError:
            raise errors.ParamsInvalidError('node name %s conflict' % name)
        self.write_data({'id': node_id, 'name': name})
Exemplo n.º 11
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