当前位置: 首页>>代码示例>>Python>>正文


Python fields.Str方法代码示例

本文整理汇总了Python中marshmallow.fields.Str方法的典型用法代码示例。如果您正苦于以下问题:Python fields.Str方法的具体用法?Python fields.Str怎么用?Python fields.Str使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在marshmallow.fields的用法示例。


在下文中一共展示了fields.Str方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_use_kwargs_schema_with_post_load

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_use_kwargs_schema_with_post_load(self, app, client):
        class User:
            def __init__(self, name):
                self.name = name

            def update(self, name):
                self.name = name

        class ArgSchema(Schema):
            name = fields.Str()

            @post_load
            def make_object(self, data, **kwargs):
                return User(**data)

        @app.route('/', methods=('POST', ))
        @use_kwargs(ArgSchema())
        def view(user):
            assert isinstance(user, User)
            return {'name': user.name}

        data = {'name': 'freddie'}
        res = client.post('/', data)
        assert res.json == data 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:26,代码来源:test_views.py

示例2: test_use_kwargs_callable_as_schema

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_use_kwargs_callable_as_schema(self, app, client):
        def schema_factory(request):
            assert request.method == 'GET'
            assert request.path == '/'

            class ArgSchema(Schema):
                name = fields.Str()

            return ArgSchema

        @app.route('/')
        @use_kwargs(schema_factory)
        def view(**kwargs):
            return kwargs
        res = client.get('/', {'name': 'freddie'})
        assert res.json == {'name': 'freddie'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:18,代码来源:test_views.py

示例3: test_partial_param_for_schema_loading

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_partial_param_for_schema_loading(app, client):  # noqa
    class TestSchema(Schema):
        foo = fields.Str(required=True)
        bar = fields.Str(required=True)

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(schema=TestSchema(), api=api, partial=True)
        def post(self):
            return "success"

    with client as cl:
        resp = cl.post("/test", json={"foo": 'foo', "bar": "bar"})
        assert resp.status_code == 200
        resp = cl.post("/test", json={"foo": 'foo'})
        assert resp.status_code == 200 
开发者ID:apryor6,项目名称:flask_accepts,代码行数:20,代码来源:decorators_test.py

示例4: __init__

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def __init__(self, allow_extra):
        class LocationSchema(Schema):
            latitude = fields.Float(allow_none=True)
            longitude = fields.Float(allow_none=True)

        class SkillSchema(Schema):
            subject = fields.Str(required=True)
            subject_id = fields.Integer(required=True)
            category = fields.Str(required=True)
            qual_level = fields.Str(required=True)
            qual_level_id = fields.Integer(required=True)
            qual_level_ranking = fields.Float(default=0)

        class Model(Schema):
            id = fields.Integer(required=True)
            client_name = fields.Str(validate=validate.Length(max=255), required=True)
            sort_index = fields.Float(required=True)
            # client_email = fields.Email()
            client_phone = fields.Str(validate=validate.Length(max=255), allow_none=True)

            location = fields.Nested(LocationSchema)

            contractor = fields.Integer(validate=validate.Range(min=0), allow_none=True)
            upstream_http_referrer = fields.Str(validate=validate.Length(max=1023), allow_none=True)
            grecaptcha_response = fields.Str(validate=validate.Length(min=20, max=1000), required=True)
            last_updated = fields.DateTime(allow_none=True)
            skills = fields.Nested(SkillSchema, many=True)

        self.allow_extra = allow_extra  # unused
        self.schema = Model() 
开发者ID:samuelcolvin,项目名称:pydantic,代码行数:32,代码来源:test_marshmallow.py

示例5: use_kwargs

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def use_kwargs(args, locations=None, inherit=None, apply=None, **kwargs):
    """Inject keyword arguments from the specified webargs arguments into the
    decorated view function.

    Usage:

    .. code-block:: python

        from marshmallow import fields

        @use_kwargs({'name': fields.Str(), 'category': fields.Str()})
        def get_pets(**kwargs):
            return Pet.query.filter_by(**kwargs).all()

    :param args: Mapping of argument names to :class:`Field <marshmallow.fields.Field>`
        objects, :class:`Schema <marshmallow.Schema>`, or a callable which accepts a
        request and returns a :class:`Schema <marshmallow.Schema>`
    :param locations: Default request locations to parse
    :param inherit: Inherit args from parent classes
    :param apply: Parse request with specified args
    """
    kwargs.update({'locations': locations})

    def wrapper(func):
        options = {
            'args': args,
            'kwargs': kwargs,
        }
        annotate(func, 'args', [options], inherit=inherit, apply=apply)
        return activate(func)
    return wrapper 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:33,代码来源:annotations.py

示例6: test_use_kwargs

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_use_kwargs(self, app, client):
        @app.route('/')
        @use_kwargs({'name': fields.Str()})
        def view(**kwargs):
            return kwargs
        res = client.get('/', {'name': 'freddie'})
        assert res.json == {'name': 'freddie'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:9,代码来源:test_views.py

示例7: test_view_returning_tuple

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_view_returning_tuple(self, app, client):
        @app.route('/all')
        @use_kwargs({'name': fields.Str()})
        def all(**kwargs):
            return kwargs, 202, {'x-msg': 'test'}

        @app.route('/headers')
        @use_kwargs({'name': fields.Str()})
        def view_headers(**kwargs):
            return kwargs, {'x-msg': 'test'}

        @app.route('/code')
        @use_kwargs({'name': fields.Str()})
        def view_code(**kwargs):
            return kwargs, 202

        res_all = client.get('/all', {'name': 'freddie'})
        assert res_all.json == {'name': 'freddie'}
        assert res_all.status_code == 202
        assert res_all.headers.get('x-msg') == 'test'

        res_headers = client.get('/headers', {'name': 'freddie'})
        assert res_headers.json == {'name': 'freddie'}
        assert res_headers.status_code == 200
        assert res_headers.headers.get('x-msg') == 'test'

        res_code = client.get('/code', {'name': 'freddie'})
        assert res_code.json == {'name': 'freddie'}
        assert res_code.status_code == 202
        assert 'x-msg' not in res_code.headers 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:32,代码来源:test_views.py

示例8: test_use_kwargs_schema

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_use_kwargs_schema(self, app, client):
        class ArgSchema(Schema):
            name = fields.Str()

        @app.route('/')
        @use_kwargs(ArgSchema)
        def view(**kwargs):
            return kwargs
        res = client.get('/', {'name': 'freddie'})
        assert res.json == {'name': 'freddie'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:12,代码来源:test_views.py

示例9: test_use_kwargs_multiple

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_use_kwargs_multiple(self, app, client):
        @app.route('/')
        @use_kwargs({'name': fields.Str()})
        @use_kwargs({'instrument': fields.Str()})
        def view(**kwargs):
            return kwargs
        res = client.get('/', {'name': 'freddie', 'instrument': 'vocals'})
        assert res.json == {'name': 'freddie', 'instrument': 'vocals'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:10,代码来源:test_views.py

示例10: test_integration

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_integration(self, app, client, models, schemas):
        @app.route('/')
        @use_kwargs({'name': fields.Str(), 'genre': fields.Str()})
        @marshal_with(schemas.BandSchema)
        def view(**kwargs):
            return models.Band(**kwargs)
        res = client.get('/', {'name': 'queen', 'genre': 'rock'})
        assert res.json == {'name': 'queen', 'genre': 'rock'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:10,代码来源:test_views.py

示例11: test_inheritance_only_http_methods

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_inheritance_only_http_methods(self, app):
        @use_kwargs({'genre': fields.Str()})
        class ConcreteResource(MethodResource):
            def _helper(self, **kwargs):
                return kwargs

        with app.test_request_context():
            resource = ConcreteResource()
            assert resource._helper() == {} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:11,代码来源:test_views.py

示例12: test_kwargs_inheritance

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_kwargs_inheritance(self, app, client):
        class BaseResource(MethodResource):
            @use_kwargs({'name': fields.Str()})
            def get(self, **kwargs):
                pass

        class ConcreteResource(BaseResource):
            @use_kwargs({'genre': fields.Str()})
            def get(self, **kwargs):
                return kwargs

        app.add_url_rule('/', view_func=ConcreteResource.as_view('concrete'))
        res = client.get('/', {'name': 'queen', 'genre': 'rock'})
        assert res.json == {'name': 'queen', 'genre': 'rock'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:16,代码来源:test_views.py

示例13: test_kwargs_inheritance_false

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_kwargs_inheritance_false(self, app, client, models, schemas):
        class BaseResource(MethodResource):
            @use_kwargs({'name': fields.Str(), 'genre': fields.Str()})
            def get(self):
                pass

        class ConcreteResource(BaseResource):
            @use_kwargs({'name': fields.Str()}, inherit=False)
            def get(self, **kwargs):
                return kwargs

        app.add_url_rule('/', view_func=ConcreteResource.as_view('concrete'))
        res = client.get('/', {'name': 'queen', 'genre': 'rock'})
        assert res.json == {'name': 'queen'} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:16,代码来源:test_views.py

示例14: test_kwargs_apply_false

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def test_kwargs_apply_false(self, app, client):
        class ConcreteResource(MethodResource):
            @use_kwargs({'genre': fields.Str()}, apply=False)
            def get(self, **kwargs):
                return kwargs

        app.add_url_rule('/', view_func=ConcreteResource.as_view('concrete'))
        res = client.get('/', {'name': 'queen', 'genre': 'rock'})
        assert res.json == {} 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:11,代码来源:test_views.py

示例15: function_view

# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Str [as 别名]
def function_view(self, app, models, schemas):
        @app.route('/bands/<int:band_id>/')
        @doc(tags=['band'])
        @use_kwargs({'name': fields.Str(missing='queen')}, locations=('query', ))
        @marshal_with(schemas.BandSchema, description='a band')
        def get_band(band_id):
            return models.Band(name='slowdive', genre='spacerock')
        return get_band 
开发者ID:jmcarp,项目名称:flask-apispec,代码行数:10,代码来源:test_openapi.py


注:本文中的marshmallow.fields.Str方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。