def test_register_path(): config = setup() class App(morepath.App): testing_config = config root = Root() app = App() lookup = app.lookup def get_model(id): model = Model() model.id = id return model config.commit() registry = app.registry register_path(registry, Root, '', lambda m: {}, None, None, None, False, lambda: root) register_path(registry, Model, '{id}', lambda model: {'id': model.id}, None, None, None, False, get_model) registry.register(generic.context, [object], lambda obj: {}) obj, request = consume(app, 'a') assert obj.id == 'a' model = Model() model.id = 'b' assert generic.path(model, lookup=lookup) == ('b', {})
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']})
def test_register_path(): class App(morepath.App): pass root = Root() def get_model(id): model = Model() model.id = id return model dectate.commit(App) path_registry = App.config.path_registry path_registry.register_path( Root, '', lambda m: {}, None, None, None, False, lambda: root) path_registry.register_path( Model, '{id}', lambda model: {'id': model.id}, None, None, None, False, get_model) app = App() obj, request = consume(app, 'a') assert obj.id == 'a' model = Model() model.id = 'b' assert generic.path(model, lookup=app.lookup) == ('b', {})
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
def link(request, model, mounted): result = [] parameters = {} while mounted is not None: path, params = generic.path(model, lookup=mounted.lookup) result.append(path) parameters.update(params) model = mounted mounted = mounted.parent result.reverse() return '/'.join(result).strip('/'), parameters
def test_register_root(): app = App() root = Root() lookup = app.lookup() c = setup() c.configurable(app) c.commit() register_root(app, Root, lambda: root) assert generic.path(root, lookup=lookup) == '' assert generic.base(root, lookup=lookup) is app
def test_register_root(): app = App() root = Root() app.root_model = Root app.root_obj = root lookup = Lookup(ChainClassLookup(app, global_app)) register_root(app, Root, lambda: root) request = get_request() request.lookup = lookup assert generic.path(request, root, lookup=lookup) == '' assert generic.base(root, lookup=lookup) is app
def link(request, model): result = [] lookup = request.lookup while True: path = generic.path(request, model, lookup=lookup) if path: result.append(path) model = generic.base(model, lookup=lookup, default=None) if model is None: break # XXX should switch lookup back to lookup of base model in order # to mimic what happens during path resolution result.reverse() return '/'.join(result)
def link(model, app): """Create a link (URL) to a model, including any mounted applications. """ result = [] parameters = {} while app is not None: path_info = generic.path(model, lookup=app.lookup) if path_info is None: return None path, params = path_info result.append(path) parameters.update(params) model = app app = app.parent result.reverse() return '/'.join(result).strip('/'), parameters
def test_register_path_with_parameters(): class App(morepath.App): pass root = Root() def get_model(id, param='default'): model = Model() model.id = id model.param = param return model dectate.commit(App) path_registry = App.config.path_registry path_registry.register_path( Root, '', lambda m: {}, None, None, None, False, lambda: root) path_registry.register_path( Model, '{id}', lambda model: {'id': model.id, 'param': model.param}, None, None, None, False, get_model) mount = App() obj, request = consume(mount, 'a') assert obj.id == 'a' assert obj.param == 'default' obj, request = consume(mount, 'a', {'param': 'value'}) assert obj.id == 'a' assert obj.param == 'value' model = Model() model.id = 'b' model.param = 'other' assert generic.path(model, lookup=mount.lookup) == ( 'b', {'param': ['other']})
def test_register_path(): config = setup() app = App(testing_config=config) root = Root() lookup = app.lookup() def get_model(id): model = Model() model.id = id return model config.commit() register_path(app, Root, '', lambda m: {}, None, None, lambda: root) register_path(app, Model, '{id}', lambda model: {'id': model.id}, None, None, get_model) obj, request = consume(app, 'a') assert obj.id == 'a' model = Model() model.id = 'b' assert generic.path(model, lookup=lookup) == ('b', {}) assert generic.app(model, lookup=lookup) is app
def test_register_model(): app = App() root = Root() lookup = app.lookup() def get_model(id): model = Model() model.id = id return model c = setup() c.configurable(app) c.commit() register_root(app, Root, lambda: root) register_model(app, Model, '{id}', lambda model: {'id': model.id}, get_model) obj, request = consume(app, 'a') assert obj.id == 'a' model = Model() model.id = 'b' assert generic.path(model, lookup=lookup) == 'b' assert generic.base(model, lookup=lookup) is app