本文整理汇总了Python中morepath.app.App类的典型用法代码示例。如果您正苦于以下问题:Python App类的具体用法?Python App怎么用?Python App使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了App类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_traject_consumer_combination
def test_traject_consumer_combination():
app = App()
traject = Traject()
def get_model(foo):
result = Model()
result.foo = foo
return result
traject.register('special', Special)
traject.register('{foo}', get_model)
app.register(generic.traject, [App], lambda base: traject)
found, obj, stack = traject_consumer(app, parse_path('something'),
Lookup(app))
assert found
assert isinstance(obj, Model)
assert stack == []
assert obj.foo == 'something'
found, obj, stack = traject_consumer(app, parse_path('special'),
Lookup(app))
assert found
assert isinstance(obj, Special)
assert stack == []
示例2: test_view_predicates
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'
示例3: test_traject_with_multiple_variables
def test_traject_with_multiple_variables():
app = App()
traject = Traject()
def get_model(first_id):
result = Model()
result.first_id = first_id
return result
def get_special(first_id, second_id):
result = Special()
result.first_id = first_id
result.second_id = second_id
return result
traject.add_pattern('{first_id}', get_model)
traject.add_pattern('{first_id}/{second_id}', get_special)
app.register(generic.traject, [App], lambda base: traject)
found, request = consume(app, 'a')
assert isinstance(found, Model)
assert found.first_id == 'a'
assert not hasattr(found, 'second_id')
assert request.unconsumed == []
found, request = consume(app, 'a/b')
assert isinstance(found, Special)
assert found.first_id == 'a'
assert found.second_id == 'b'
assert request.unconsumed == []
示例4: test_traject_nested_with_variable
def test_traject_nested_with_variable():
app = App()
traject = Traject()
def get_model(id):
result = Model()
result.id = id
return result
def get_special(id):
result = Special()
result.id = id
return result
traject.add_pattern('{id}', get_model)
traject.add_pattern('{id}/sub', get_special)
app.register(generic.traject, [App], lambda base: traject)
found, request = consume(app, 'a')
assert isinstance(found, Model)
assert request.unconsumed == []
found, request = consume(app, 'b')
assert isinstance(found, Model)
assert request.unconsumed == []
found, request = consume(app, 'a/sub')
assert isinstance(found, Special)
assert request.unconsumed == []
示例5: test_traject_nested_with_variable
def test_traject_nested_with_variable():
app = App()
traject = Traject()
def get_model(id):
result = Model()
result.id = id
return result
def get_special(id):
result = Special()
result.id = id
return result
traject.add_pattern("{id}", (get_model, paramfac))
traject.add_pattern("{id}/sub", (get_special, paramfac))
app.register(generic.traject, [App], lambda base: traject)
app.register(generic.context, [object], lambda obj: {})
found, request = consume(app, "a")
assert isinstance(found, Model)
assert request.unconsumed == []
found, request = consume(app, "b")
assert isinstance(found, Model)
assert request.unconsumed == []
found, request = consume(app, "a/sub")
assert isinstance(found, Special)
assert request.unconsumed == []
示例6: test_notfound
def test_notfound():
config = setup()
app = App(testing_config=config)
config.commit()
response = publish(app.request(get_environ(path='')), app.mounted())
assert response.status == '404 NOT FOUND'
示例7: test_traject_with_multiple_variables
def test_traject_with_multiple_variables():
app = App()
traject = Traject()
def get_model(first_id):
result = Model()
result.first_id = first_id
return result
def get_special(first_id, second_id):
result = Special()
result.first_id = first_id
result.second_id = second_id
return result
traject.add_pattern("{first_id}", (get_model, paramfac))
traject.add_pattern("{first_id}/{second_id}", (get_special, paramfac))
app.register(generic.traject, [App], lambda base: traject)
found, request = consume(app, "a")
assert isinstance(found, Model)
assert found.first_id == "a"
assert not hasattr(found, "second_id")
assert request.unconsumed == []
found, request = consume(app, "a/b")
assert isinstance(found, Special)
assert found.first_id == "a"
assert found.second_id == "b"
assert request.unconsumed == []
示例8: test_traject_nested_with_variable
def test_traject_nested_with_variable():
app = App()
traject = Traject()
def get_model(id):
result = Model()
result.id = id
return result
def get_special(id):
result = Special()
result.id = id
return result
traject.register('{id}', get_model)
traject.register('{id}/sub', get_special)
app.register(generic.traject, [App], lambda base: traject)
found, obj, stack = traject_consumer(app, parse_path('a'), Lookup(app))
assert found
assert isinstance(obj, Model)
assert stack == []
found, obj, stack = traject_consumer(app, parse_path('b'), Lookup(app))
assert found
assert isinstance(obj, Model)
assert stack == []
found, obj, stack = traject_consumer(app, parse_path('a/sub'), Lookup(app))
assert found
assert isinstance(obj, Special)
assert stack == []
示例9: test_register_model
def test_register_model():
setup()
app = App()
root = Root()
app.root_model = Root
app.root_obj = root
lookup = Lookup(ChainClassLookup(app, global_app))
def get_model(id):
model = Model()
model.id = id
return model
register_root(app, Root, lambda: root)
register_model(app, Model, '{id}', lambda model: {'id': model.id},
get_model)
found, obj, stack = traject_consumer(app, parse_path('a'), lookup)
assert obj.id == 'a'
model = Model()
model.id = 'b'
request = get_request()
request.lookup = lookup
assert generic.path(request, model, lookup=lookup) == 'b'
assert generic.base(model, lookup=lookup) is app
示例10: test_request_view_with_predicates
def test_request_view_with_predicates():
app = App()
c = setup()
c.configurable(app)
c.commit()
def view(request, model):
return {'hey': 'hey'}
register_view(app, Model, view, render=render_json,
predicates=dict(name='foo'))
request = app.request(get_environ(path=''))
request.mounts = [app] # XXX should do this centrally
model = Model()
# since the name is set to foo, we get nothing here
assert request.view(model) is None
# we have to pass the name predicate ourselves
assert request.view(model, name='foo') == {'hey': 'hey'}
# the predicate information in the request is ignored when we do a
# manual view lookup using request.view
request = app.request(get_environ(path='foo'))
request.mounts = [app] # XXX should do this centrally
assert request.view(model) is None
示例11: test_traject_with_multiple_variables
def test_traject_with_multiple_variables():
app = App()
traject = Traject()
def get_model(first_id):
result = Model()
result.first_id = first_id
return result
def get_special(first_id, second_id):
result = Special()
result.first_id = first_id
result.second_id = second_id
return result
traject.register('{first_id}', get_model)
traject.register('{first_id}/{second_id}', get_special)
app.register(generic.traject, [App], lambda base: traject)
found, obj, stack = traject_consumer(app, parse_path('a'), Lookup(app))
assert found
assert isinstance(obj, Model)
assert stack == []
assert obj.first_id == 'a'
assert not hasattr(obj, 'second_id')
found, obj, stack = traject_consumer(app, parse_path('a/b'), Lookup(app))
assert found
assert isinstance(obj, Special)
assert stack == []
assert obj.first_id == 'a'
assert obj.second_id == 'b'
示例12: test_register_path_with_parameters
def test_register_path_with_parameters():
config = setup()
app = App(testing_config=config)
root = Root()
lookup = app.lookup
def get_model(id, param='default'):
model = Model()
model.id = id
model.param = param
return model
config.commit()
register_path(app, Root, '', lambda m: {}, None, None, None, lambda: root)
register_path(app, Model, '{id}', lambda model: {'id': model.id,
'param': model.param},
None, None, None, get_model)
app.register(generic.context, [object], lambda obj: {})
obj, request = consume(app, 'a')
assert obj.id == 'a'
assert obj.param == 'default'
obj, request = consume(app, 'a', {'param': 'value'})
assert obj.id == 'a'
assert obj.param == 'value'
model = Model()
model.id = 'b'
model.param = 'other'
assert generic.path(model, lookup=lookup) == ('b', {'param': ['other']})
示例13: test_extra_predicates
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'
示例14: test_traject_consume
def test_traject_consume():
app = App()
traject = Traject()
traject.add_pattern('sub', Model)
app.register(generic.traject, [App], lambda base: traject)
found, request = consume(app, 'sub')
assert isinstance(found, Model)
assert request.unconsumed == []
示例15: test_notfound
def test_notfound():
app = App()
c = setup()
c.configurable(app)
c.commit()
response = publish(app.request(get_environ(path='')), app.mounted())
assert response.status == '404 NOT FOUND'