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


Python Api.inherit方法代码示例

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


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

示例1: test_marshal_with_handle_polymorph

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import inherit [as 别名]
    def test_marshal_with_handle_polymorph(self, app, client):
        api = Api(app)

        parent = api.model('Person', {
            'name': fields.String,
        })

        child1 = api.inherit('Child1', parent, {
            'extra1': fields.String,
        })

        child2 = api.inherit('Child2', parent, {
            'extra2': fields.String,
        })

        class Child1(object):
            name = 'child1'
            extra1 = 'extra1'

        class Child2(object):
            name = 'child2'
            extra2 = 'extra2'

        mapping = {
            Child1: child1,
            Child2: child2
        }

        thing = api.model('Thing', {
            'owner': fields.Polymorph(mapping),
        })

        @api.route('/thing-1/')
        class Thing1Resource(Resource):
            @api.marshal_with(thing)
            def get(self):
                return {'owner': Child1()}

        @api.route('/thing-2/')
        class Thing2Resource(Resource):
            @api.marshal_with(thing)
            def get(self):
                return {'owner': Child2()}

        data = client.get_json('/thing-1/', headers={'X-Fields': 'owner{name}'})
        assert data == {'owner': {'name': 'child1'}}

        data = client.get_json('/thing-1/', headers={'X-Fields': 'owner{extra1}'})
        assert data == {'owner': {'extra1': 'extra1'}}

        data = client.get_json('/thing-2/', headers={'X-Fields': 'owner{name}'})
        assert data == {'owner': {'name': 'child2'}}
开发者ID:hivelearning,项目名称:flask-restplus,代码行数:54,代码来源:test_fields_mask.py

示例2: test_marshal_handle_inheritance

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import inherit [as 别名]
    def test_marshal_handle_inheritance(self):
        api = Api(self.app)

        person = api.model('Person', {
            'name': fields.String,
            'age': fields.Integer,
        })

        child = api.inherit('Child', person, {
            'extra': fields.String,
        })

        data = {
            'name': 'John Doe',
            'age': 42,
            'extra': 'extra'
        }

        values = (
            ('name', {'name': 'John Doe'}),
            ('name,extra', {'name': 'John Doe', 'extra': 'extra'}),
            ('extra', {'extra': 'extra'}),
        )

        for mask, expected in values:
            result = marshal(data, child, mask=mask)
            self.assertEqual(result, expected)
开发者ID:kmalyar-tic,项目名称:flask-restplus,代码行数:29,代码来源:test_fields_mask.py

示例3: test_list_fields_with_nested_inherited

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import inherit [as 别名]
    def test_list_fields_with_nested_inherited(self):
        api = Api(self.app)

        person = api.model('Person', {
            'name': fields.String,
            'age': fields.Integer
        })
        child = api.inherit('Child', person, {
            'attr': fields.String
        })

        family = api.model('Family', {
            'children': fields.List(fields.Nested(child))
        })

        result = mask.apply(family.resolved, 'children{name,attr}')

        data = {'children': [
            {'name': 'John', 'age': 5, 'attr': 'value-john'},
            {'name': 'Jane', 'age': 42, 'attr': 'value-jane'},
        ]}
        expected = {'children': [
            {'name': 'John', 'attr': 'value-john'},
            {'name': 'Jane', 'attr': 'value-jane'},
        ]}

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family), data)
开发者ID:kmalyar-tic,项目名称:flask-restplus,代码行数:31,代码来源:test_fields_mask.py

示例4: test_marshal_with_honour_complex_field_mask_header

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import inherit [as 别名]
    def test_marshal_with_honour_complex_field_mask_header(self):
        api = Api(self.app)

        person = api.model('Person', person_fields)
        child = api.inherit('Child', person, {
            'attr': fields.String
        })

        family = api.model('Family', {
            'father': fields.Nested(person),
            'mother': fields.Nested(person),
            'children': fields.List(fields.Nested(child)),
            'free': fields.List(fields.Raw),
        })

        house = api.model('House', {
            'family': fields.Nested(family, attribute='people')
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(house)
            def get(self):
                return {'people': {
                    'father': {'name': 'John', 'age': 42},
                    'mother': {'name': 'Jane', 'age': 42},
                    'children': [
                        {'name': 'Jack', 'age': 5, 'attr': 'value-1'},
                        {'name': 'Julie', 'age': 7, 'attr': 'value-2'},
                    ],
                    'free': [
                        {'key-1': '1-1', 'key-2': '1-2'},
                        {'key-1': '2-1', 'key-2': '2-2'},
                    ]
                }}

        data = self.get_json('/test/', headers={
            'X-Fields': 'family{father{name},mother{age},children{name,attr},free{key-2}}'
        })
        expected = {'family': {
            'father': {'name': 'John'},
            'mother': {'age': 42},
            'children': [{'name': 'Jack', 'attr': 'value-1'}, {'name': 'Julie', 'attr': 'value-2'}],
            'free': [{'key-2': '1-2'}, {'key-2': '2-2'}]
        }}
        self.assertEqual(data, expected)
开发者ID:kmalyar-tic,项目名称:flask-restplus,代码行数:48,代码来源:test_fields_mask.py

示例5: OrderedDict

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import inherit [as 别名]
        description='Name of the submitted task',
        example='doSomething',
    )),
    ('taskId', fields.String(
        required=True,
        description='ID of the created task',
        example='942b3fb5-fe63-49cd-9b6d-230c36070d8f',
    )),
]))

taskResponseModel = api.inherit('taskResponse', taskInfoModel, OrderedDict([
    ('statusUrl', fields.String(
        required=True,
        description='URL to receive the status of the task',
        example='/status/doSomething/942b3fb5-fe63-49cd-9b6d-230c36070d8f',
    )),
    ('statusStreamUrl', fields.String(
        description='URL to receive a stream of the task status',
        example='/statusStream/doSomething/942b3fb5-fe63-49cd-9b6d-230c36070d8',
    )),
]))

taskStateModel = api.model('taskState', OrderedDict([
    ('state', fields.String(
        required=True,
        description='Celery state of the job',
        example='STARTED',
    )),
    ('info', fields.Raw(
        required=True,
        description='Data related to its execution',
开发者ID:zhaofengli,项目名称:refill,代码行数:33,代码来源:app.py

示例6: ModelTestCase

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import inherit [as 别名]

#.........这里部分代码省略.........
            'age': fields.Integer,
            'birthdate': fields.DateTime,
        })

        child = self.api.extend('Child', [grand_parent, parent], {
            'extra': fields.String,
        })

        self.assertEqual(child.__schema__, {
            'properties': {
                'grand_parent': {
                    'type': 'string'
                },
                'name': {
                    'type': 'string'
                },
                'age': {
                    'type': 'integer'
                },
                'birthdate': {
                    'type': 'string',
                    'format': 'date-time'
                },
                'extra': {
                    'type': 'string'
                }
            }
        })

        self.assertIn('GrandParent', self.api.models)
        self.assertIn('Parent', self.api.models)
        self.assertIn('Child', self.api.models)

    def test_inherit(self):
        parent = self.api.model('Parent', {
            'name': fields.String,
            'age': fields.Integer,
        })

        child = self.api.inherit('Child', parent, {
            'extra': fields.String,
        })

        self.assertEqual(parent.__schema__, {
            'properties': {
                'name': {'type': 'string'},
                'age': {'type': 'integer'},
            }
        })
        self.assertEqual(child.__schema__, {
            'allOf': [{
                    '$ref': '#/definitions/Parent'
                }, {
                    'properties': {
                        'extra': {'type': 'string'}
                    }
                }
            ]
        })

        self.assertIn('Parent', self.api.models)
        self.assertIn('Child', self.api.models)

    def test_inherit_inline(self):
        parent = self.api.model('Person', {
            'name': fields.String,
开发者ID:AleixDev,项目名称:flask-restplus,代码行数:70,代码来源:test_model.py


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