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


Python App.view方法代码示例

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


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

示例1: test_view_predicates

# 需要导入模块: from morepath.app import App [as 别名]
# 或者: from morepath.app.App import view [as 别名]
def test_view_predicates():
    app = App()

    class Root(object):
        pass

    def get(request, model):
        return 'GET'

    def post(request, model):
        return 'POST'

    c = setup()
    c.configurable(app)
    c.action(app.root(), Root)
    c.action(app.view(model=Root, name='foo', request_method='GET'),
             get)
    c.action(app.view(model=Root, name='foo', request_method='POST'),
             post)
    c.commit()

    c = Client(app, Response)

    response = c.get('/foo')
    assert response.data == 'GET'
    response = c.post('/foo')
    assert response.data == 'POST'
开发者ID:kod3r,项目名称:morepath,代码行数:29,代码来源:test_predicates.py

示例2: test_extra_predicates

# 需要导入模块: from morepath.app import App [as 别名]
# 或者: from morepath.app.App import view [as 别名]
def test_extra_predicates():
    app = App()

    class Model(object):
        def __init__(self, id):
            self.id = id

    def get_a(request, model):
        return 'a'

    def get_b(request, model):
        return 'b'

    def get_id(request, model):
        return model.id

    c = setup()
    c.configurable(app)
    c.action(app.model(path='{id}'), Model)
    c.action(app.view(model=Model, name='foo', id='a'),
             get_a)
    c.action(app.view(model=Model, name='foo', id='b'),
             get_b)
    c.action(app.predicate(name='id', order=2, default=''),
             get_id)
    c.commit()

    c = Client(app, Response)

    response = c.get('/a/foo')
    assert response.data == 'a'
    response = c.post('/b/foo')
    assert response.data == 'b'
开发者ID:kod3r,项目名称:morepath,代码行数:35,代码来源:test_predicates.py

示例3: test_extends

# 需要导入模块: from morepath.app import App [as 别名]
# 或者: from morepath.app.App import view [as 别名]
def test_extends():
    app = App()
    extending = App(extends=[app])

    c = setup()
    c.configurable(app)
    c.configurable(extending)

    class User(object):
        def __init__(self, username):
            self.username = username

    c.action(
        app.model(
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        User)

    def render_user(request, model):
        return "User: %s" % model.username

    c.action(
        app.view(
            model=User),
        render_user)

    def edit_user(request, model):
        return "Edit user: %s" % model.username

    c.action(
        extending.view(
            model=User,
            name='edit'),
        edit_user)

    c.commit()

    cl = Client(app, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'
    response = cl.get('/users/foo/edit')
    assert response.status == '404 NOT FOUND'

    cl = Client(extending, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'
    response = cl.get('/users/foo/edit')
    assert response.data == 'Edit user: foo'
开发者ID:kod3r,项目名称:morepath,代码行数:50,代码来源:test_extend.py

示例4: test_overrides_view

# 需要导入模块: from morepath.app import App [as 别名]
# 或者: from morepath.app.App import view [as 别名]
def test_overrides_view():
    app = App()
    overriding = App(extends=[app])

    c = setup()
    c.configurable(app)
    c.configurable(overriding)

    class User(object):
        def __init__(self, username):
            self.username = username

    c.action(
        app.model(
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        User)

    def render_user(request, model):
        return "User: %s" % model.username

    c.action(
        app.view(
            model=User),
        render_user)

    def render_user2(request, model):
        return "USER: %s" % model.username

    c.action(
        overriding.view(
            model=User),
        render_user2)

    c.commit()

    cl = Client(app, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'

    cl = Client(overriding, Response)
    response = cl.get('/users/foo')
    assert response.data == 'USER: foo'
开发者ID:kod3r,项目名称:morepath,代码行数:45,代码来源:test_extend.py

示例5: test_overrides_model

# 需要导入模块: from morepath.app import App [as 别名]
# 或者: from morepath.app.App import view [as 别名]
def test_overrides_model():
    app = App()
    overriding = App(extends=[app])

    c = setup()
    c.configurable(app)
    c.configurable(overriding)

    class User(object):
        def __init__(self, username):
            self.username = username

    c.action(
        app.model(
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        User)

    def render_user(request, model):
        return "User: %s" % model.username

    c.action(
        app.view(
            model=User),
        render_user)

    def get_user(username):
        if username != 'bar':
            return None
        return User(username)

    c.action(
        overriding.model(
            model=User,
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        get_user)

    c.commit()

    cl = Client(app, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'
    response = cl.get('/users/bar')
    assert response.data == 'User: bar'

    cl = Client(overriding, Response)
    response = cl.get('/users/foo')
    assert response.status == '404 NOT FOUND'
    response = cl.get('/users/bar')
    assert response.data == 'User: bar'
开发者ID:kod3r,项目名称:morepath,代码行数:53,代码来源:test_extend.py

示例6: test_base

# 需要导入模块: from morepath.app import App [as 别名]
# 或者: from morepath.app.App import view [as 别名]
def test_base():
    setup()

    class Root(object):
        pass

    class Container(object):
        def __init__(self, id):
            self.id = id
            self.items = {}

        def add_item(self, item_id):
            result = Item(item_id, self)
            self.items[item_id] = result
            return result

    class Item(object):
        def __init__(self, id, parent):
            self.id = id
            self.parent = parent

    alpha = Container('alpha')
    beta = Container('beta')
    alpha.add_item('a')
    alpha.add_item('b')
    c = alpha.add_item('c')
    beta.add_item('d')
    e = beta.add_item('e')

    app = App()

    c = Config()
    c.action(app, app)
    c.action(app.root(), Root)

    def get_container(container_id):
        if container_id == 'alpha':
            return alpha
        elif container_id == 'beta':
            return beta
        return None

    c.action(
        app.model(
            model=Container,
            path="{container_id}",
            variables=lambda container: {'container_id': container.id}),
        get_container)

    def get_item(base, item_id):
        return base.items.get(item_id)

    c.action(
        app.model(model=Item,
                  path="{item_id}",
                  variables=lambda item: {'item_id': item.id},
                  base=Container,
                  get_base=lambda item: item.parent),
        get_item)

    c.action(
        app.view(model=Container),
        lambda request, model: 'container: %s' % model.id)
    c.action(
        app.view(model=Container, name='link'),
        lambda request, model: request.link(model))
    c.action(
        app.view(model=Item),
        lambda request, model: 'item: %s' % model.id)
    c.action(
        app.view(model=Item, name='link'),
        lambda request, model: request.link(model))
    c.action(
        app.view(model=Item, name='otherlink'),
        lambda request, model: request.link(e))
    c.commit()

    c = Client(app, Response)

    response = c.get('/alpha')
    assert response.data == 'container: alpha'
    response = c.get('/beta')
    assert response.data == 'container: beta'
    response = c.get('/alpha/a')
    assert response.data == 'item: a'
    response = c.get('/beta/e')
    assert response.data == 'item: e'
    response = c.get('/alpha/e')
    assert response.status == '404 NOT FOUND'

    response = c.get('/alpha/@@link')
    assert response.data == 'alpha'
    response = c.get('/alpha/a/link')
    assert response.data == 'alpha/a'
    response = c.get('/alpha/a/otherlink')
    assert response.data == 'beta/e'
开发者ID:oohlaf,项目名称:morepath,代码行数:98,代码来源:test_base.py


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