def test_nested_prefixes(self): "Reverse with nested prefexes" app = ( web.prefix("/news/<section>") | web.namespace("news") | web.cases( web.match(), web.prefix("/<int:id>") | web.namespace("item") | web.cases( web.match(), web.prefix("/docs") | web.namespace("docs") | web.cases(web.match(), web.match("/<int:id>", "item")), ), ) ) r = web.Reverse.from_handler(app) # Normal behavior self.assertEqual(r.news(section="top").as_url, "/news/top") self.assertEqual(r.news(section="top").item(id=1).as_url, "/news/top/1") self.assertEqual(r.news(section="top").item(id=1).docs.as_url, "/news/top/1/docs") self.assertEqual(r.news(section="top").item(id=1).docs.item(id=2).as_url, "/news/top/1/docs/2") # Exceptional behavior self.assertRaises(UrlBuildingError, lambda: r.news.as_url) # XXX this one raises KeyError, not UrlBuildingError self.assertRaises(UrlBuildingError, lambda: r.news(foo="top").as_url) self.assertRaises(UrlBuildingError, lambda: r.news(section="top").item.as_url) self.assertRaises(UrlBuildingError, lambda: r.news(section="top").item(id=1).docs()) self.assertRaises(UrlBuildingError, lambda: r.news(section="top")()) self.assertRaises(UrlBuildingError, lambda: r.news.item)
def test_subdomains_and_namespace(self): app = ( web.subdomain("d2") | web.namespace("subdomain") | web.subdomain("d1") | web.namespace("more") | web.match("/", "index") ) r = web.Reverse.from_handler(app) self.assertEqual(r.subdomain.more.index.as_url, "http://d1.d2/")
def test_multiple_params(self): app = ( web.prefix("/persons/<int:person_id>") | web.namespace("persons") | web.cases(web.prefix("/news") | web.namespace("news") | web.match("/<int:news_id>", "item")) ) r = web.Reverse.from_handler(app) r1 = r.persons(person_id=1).news.item(news_id=2) self.assertEqual(r1.as_url, "/persons/1/news/2") self.assertEqual(r.build_url("persons.news.item", person_id=1, news_id=2), "/persons/1/news/2")
def test_multiple_params2(self): app = web.prefix('/persons') | web.namespace('persons') |\ web.prefix('/<int:person_id>') | web.cases( web.prefix('/news') | web.namespace('news') | web.match('/<int:news_id>', 'item') ) r = web.Reverse.from_handler(app) r1 = r.persons.news(person_id=1).item(news_id=2) self.assertEqual(r1.as_url, '/persons/1/news/2') self.assertEqual(r.build_url('persons.news.item', person_id=1, news_id=2), '/persons/1/news/2')
def test_namespace_and_cases(self): 'Locations namespace with web.cases' chain = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) self.assertEqual(location_keys(chain), ['news']) self.assertEqual(set(locations(chain)['news'][1].keys()), {'index', 'docs'})
def test_multiple_params_with_params_in_endpoints(self): app = ( web.prefix("/persons/<int:person_id>") | web.namespace("persons") | web.cases( web.match("/index/<page>", ""), web.prefix("/news") | web.namespace("news") | web.cases(web.match("/index/<news_page>", ""), web.match("/<int:news_id>", "item")), ) ) r = web.Reverse.from_handler(app) self.assertEqual(r.persons(person_id=1, page=2).as_url, "/persons/1/index/2") self.assertEqual(r.build_url("persons", person_id=1, page=2), "/persons/1/index/2") self.assertRaises(UrlBuildingError, lambda: r.persons(person_id=1).as_url) self.assertRaises(UrlBuildingError, lambda: r.persons(person_id=1).news.as_url)
def test_url_building_errors(self): 'UrlBuildingError' app = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'), web.match('/', 'anchor', fragment='<int:id>'), ) r = web.Reverse.from_handler(app) self.assertRaises(UrlBuildingError, r.build_url, '') self.assertRaises(UrlBuildingError, lambda: r.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'main') self.assertRaises(UrlBuildingError, lambda: r.main) self.assertRaises(UrlBuildingError, r.build_url, 'news.item') self.assertRaises(UrlBuildingError, lambda: r.news.item.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.item', uid=3) self.assertRaises(UrlBuildingError, lambda: r.news.item(uid=3)) self.assertRaises(UrlBuildingError, r.build_url, 'news.item', id=3, uid=3) self.assertRaises(UrlBuildingError, r.build_url, 'news') self.assertRaises(UrlBuildingError, lambda: r.news.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.hash') self.assertRaises(UrlBuildingError, lambda: r.news.anchor.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.hash', uid=3) self.assertRaises(UrlBuildingError, lambda: r.news.anchor(uid=3).as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.hash', id=3, uid=3)
def test_multiple_params_with_params_in_endpoints(self): app = web.prefix('/persons/<int:person_id>') | web.namespace('persons') |\ web.cases( web.match('/index/<page>', ''), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/index/<news_page>', ''), web.match('/<int:news_id>', 'item') ) ) r = web.Reverse.from_handler(app) self.assertEqual(r.persons(person_id=1, page=2).as_url, '/persons/1/index/2') self.assertEqual(r.build_url('persons', person_id=1, page=2), '/persons/1/index/2') self.assertRaises(UrlBuildingError, lambda: r.persons(person_id=1).as_url) self.assertRaises(UrlBuildingError, lambda: r.persons(person_id=1).news.as_url)
def test_repr(self): # for coverage '%r' % web.WebHandler() '%r' % web.cases() '%r' % web.request_filter(lambda e, d, n: None) '%r' % web.match('/', 'index') '%r' % web.method('GET') '%r' % web.static_files('/prefix') '%r' % web.prefix('/prefix') '%r' % web.subdomain('name') '%r' % web.namespace('name')
def test_string_api(self): 'String API for reverse (build_url)' app = web.prefix('/news/<section>') | web.namespace('news') | web.cases( web.match(), web.prefix('/<int:id>') | web.namespace('item') | web.cases( web.match(), web.prefix('/docs') | web.namespace('docs') | web.cases( web.match() ))) r = web.Reverse.from_handler(app) # Normal behavior self.assertEqual(r.build_url('news', section='top'), '/news/top') self.assertEqual(r.build_url('news.item', section='top', id=1), '/news/top/1') self.assertEqual(r.build_url('news.item.docs', section='top', id=1), '/news/top/1/docs') # Exceptional behavior self.assertRaises(UrlBuildingError, r.build_url, 'news') self.assertRaises(UrlBuildingError, r.build_url, 'news', section='top', subsection="bottom")
def test_repr(self): # for coverage "%r" % web.WebHandler() "%r" % web.cases() "%r" % web.request_filter(lambda e, d, n: None) "%r" % web.match("/", "index") "%r" % web.method("GET") "%r" % web.static_files("/prefix") "%r" % web.prefix("/prefix") "%r" % web.subdomain("name") "%r" % web.namespace("name")
def test_mix(self): 'Loactions mix' chain = web.prefix('/items') | web.cases( web.match('/'), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/'), web.match('/<int:id>', 'item'), web.prefix('/docs') | web.namespace('docs') | web.cases( web.match('/'), web.match('/<int:id>', 'item')))) locs = locations(chain) self.assertEqual(list(locs.keys()), ['', 'news']) self.assertEqual(locs[''][0], Location(*(UrlTemplate('/items', match_whole_str=False), UrlTemplate('/')))) self.assertEqual(locs['news'][0], Location(*(UrlTemplate('/items', match_whole_str=False), UrlTemplate('/news', match_whole_str=False)))) self.assertEqual(locs['news'][1][''][0], Location(UrlTemplate('/'))) self.assertEqual(locs['news'][1]['item'][0], Location(UrlTemplate('/<int:id>'))) self.assertEqual(locs['news'][1]['docs'][0], Location(UrlTemplate('/docs', match_whole_str=False))) self.assertEqual(locs['news'][1]['docs'][1][''][0], Location(UrlTemplate('/'))) self.assertEqual(locs['news'][1]['docs'][1]['item'][0], Location(UrlTemplate('/<int:id>')))
def test_string_api(self): "String API for reverse (build_url)" app = ( web.prefix("/news/<section>") | web.namespace("news") | web.cases( web.match(), web.prefix("/<int:id>") | web.namespace("item") | web.cases(web.match(), web.prefix("/docs") | web.namespace("docs") | web.cases(web.match())), ) ) r = web.Reverse.from_handler(app) # Normal behavior self.assertEqual(r.build_url("news", section="top"), "/news/top") self.assertEqual(r.build_url("news.item", section="top", id=1), "/news/top/1") self.assertEqual(r.build_url("news.item.docs", section="top", id=1), "/news/top/1/docs") # Exceptional behavior self.assertRaises(UrlBuildingError, r.build_url, "news") self.assertRaises(UrlBuildingError, r.build_url, "news", section="top", subsection="bottom")
def _create_subapp(self, ns=''): prefix = ns and (ns + '.') or '' handlers_by_namespace = self._handlers_by_namespace() children = filter( lambda x: x and x.startswith(prefix) and not '.' in x[len( prefix):], handlers_by_namespace) child_apps = [web.prefix('/' + x.rsplit('.', 1)[-1]) | \ web.namespace(x.rsplit('.', 1)[-1]) | self._create_subapp(x) for x in children] return web.cases(*(child_apps + handlers_by_namespace[ns]))
def _create_subapp(self, ns=''): prefix = ns and (ns + '.') or '' handlers_by_namespace = self._handlers_by_namespace() children = filter( lambda x: x and x.startswith(prefix) and not '.' in x[len(prefix):], handlers_by_namespace) child_apps = [web.prefix('/' + x.rsplit('.', 1)[-1]) | \ web.namespace(x.rsplit('.', 1)[-1]) | self._create_subapp(x) for x in children] return web.cases(*(child_apps + handlers_by_namespace[ns]))
def test_mix(self): "Loactions mix" chain = web.prefix("/items") | web.cases( web.match("/"), web.prefix("/news") | web.namespace("news") | web.cases( web.match("/"), web.match("/<int:id>", "item"), web.prefix("/docs") | web.namespace("docs") | web.cases(web.match("/"), web.match("/<int:id>", "item")), ), ) locations = web.locations(chain) self.assertEqual(locations.keys(), ["", "news"]) self.assertEqual(locations[""][0], Location(*(UrlTemplate("/items", match_whole_str=False), UrlTemplate("/")))) self.assertEqual( locations["news"][0], Location(*(UrlTemplate("/items", match_whole_str=False), UrlTemplate("/news", match_whole_str=False))), ) self.assertEqual(locations["news"][1][""][0], Location(UrlTemplate("/"))) self.assertEqual(locations["news"][1]["item"][0], Location(UrlTemplate("/<int:id>"))) self.assertEqual(locations["news"][1]["docs"][0], Location(UrlTemplate("/docs", match_whole_str=False))) self.assertEqual(locations["news"][1]["docs"][1][""][0], Location(UrlTemplate("/"))) self.assertEqual(locations["news"][1]["docs"][1]["item"][0], Location(UrlTemplate("/<int:id>")))
def test_prefix_state(self): '''Prefix state correctness''' def handler(env, data): return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.namespace('doc') | web.cases( web.match('/item', '') | handler, web.prefix('/list') | web.cases(web.match('/item', 'list') | handler), web.match('/other-thing', 'something') | handler), web.match('/something', 'something') | handler) self.assertEqual(web.ask(app, '/docs/something'), None) self.assertEqual(web.ask(app, '/docs/list/something'), None) self.assertEqual(web.ask(app, '/docs/list/other-thing'), None)
def test_prefix_state(self): '''Prefix state correctness''' def handler(env, data): return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.namespace('doc') | web.cases( web.match('/item', '') | handler, web.prefix('/list') | web.cases( web.match('/item', 'list') | handler), web.match('/other-thing', 'something') | handler ), web.match('/something', 'something') | handler) self.assertEqual(web.ask(app, '/docs/something'), None) self.assertEqual(web.ask(app, '/docs/list/something'), None) self.assertEqual(web.ask(app, '/docs/list/other-thing'), None)
def test_url_building_errors(self): "UrlBuildingError" app = web.namespace("news") | web.cases(web.match("/", "index"), web.match("/<int:id>", "item")) r = web.Reverse.from_handler(app) self.assertRaises(UrlBuildingError, r.build_url, "") self.assertRaises(UrlBuildingError, lambda: r.as_url) self.assertRaises(UrlBuildingError, r.build_url, "main") self.assertRaises(UrlBuildingError, lambda: r.main) self.assertRaises(UrlBuildingError, r.build_url, "news.item") self.assertRaises(UrlBuildingError, lambda: r.news.item.as_url) self.assertRaises(UrlBuildingError, r.build_url, "news.item", section="x") self.assertRaises(UrlBuildingError, lambda: r.news.item(section="x")) self.assertRaises(UrlBuildingError, r.build_url, "news") self.assertRaises(UrlBuildingError, lambda: r.news.as_url)
def test_prefix_state(self): """Prefix state correctness""" def handler(env, data): return Response() app = web.cases( web.match("/", "index"), web.prefix("/docs") | web.namespace("doc") | web.cases( web.match("/item", "") | handler, web.prefix("/list") | web.cases(web.match("/item", "list") | handler), web.match("/other-thing", "something") | handler, ), web.match("/something", "something") | handler, ) self.assertEqual(web.ask(app, "/docs/something"), None) self.assertEqual(web.ask(app, "/docs/list/something"), None) self.assertEqual(web.ask(app, "/docs/list/other-thing"), None)
def test_url_building_errors(self): 'UrlBuildingError' app = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'), ) r = web.Reverse.from_handler(app) self.assertRaises(UrlBuildingError, r.build_url, '') self.assertRaises(UrlBuildingError, lambda: r.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'main') self.assertRaises(UrlBuildingError, lambda: r.main) self.assertRaises(UrlBuildingError, r.build_url, 'news.item') self.assertRaises(UrlBuildingError, lambda: r.news.item.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.item', section='x') self.assertRaises(UrlBuildingError, lambda: r.news.item(section='x')) self.assertRaises(UrlBuildingError, r.build_url, 'news') self.assertRaises(UrlBuildingError, lambda: r.news.as_url)
def H_Language(i18n, active): return namespace(active) | H_SetActiveLanguage(i18n, active)
def test_namespace(self): "Locations namespace" chain = web.namespace("news") | web.match("/", "index") self.assert_(web.locations(chain).keys(), ["news.index"])
def test_namespace(self): 'Locations namespace' chain = web.namespace('news') | web.match('/', 'index') self.assert_(location_keys(chain), ['news.index'])
def test_subdomains_and_namespace(self): app = web.subdomain('d2') | web.namespace('subdomain') | \ web.subdomain('d1') | web.namespace('more') | \ web.match('/', 'index') r = web.Reverse.from_handler(app) self.assertEqual(r.subdomain.more.index.as_url, 'http://d1.d2/')
def test_namespace_and_cases(self): "Locations namespace with web.cases" chain = web.namespace("news") | web.cases(web.match("/", "index"), web.match("/docs", "docs")) self.assertEqual(web.locations(chain).keys(), ["news"]) self.assertEqual(web.locations(chain)["news"][1].keys(), ["index", "docs"])
def test_namespace_with_empty_name(self): "Namespaces with empty url name" chain = web.namespace("news") | web.match("/") self.assert_(web.locations(chain).keys(), ["news"])
def __init__(self, component, active): assert active in component.langs self.component = component self.active = active self._next_handler = namespace(active)
def test_namespace_with_empty_name(self): 'Namespaces with empty url name' chain = web.namespace('news') | web.match('/') self.assert_(location_keys(chain), ['news'])
# -*- coding: utf8 -*- from webob.exc import HTTPNotFound, HTTPMovedPermanently from iktomi.cms.views import auth_required from iktomi import web from iktomi.web.filters import static_files, method from .environment import environment, auth, static from iktomi.web.shortcuts import Rule from iktomi.cms.flashmessages import flash_message_handler from admin.front_environment import front_environment from front.urls import app_cases from . import cfg, streams, loners, views as h front_app = web.cases(web.namespace('en'), web.namespace('ru')) | app_cases dynamic_app = \ environment | \ flash_message_handler | \ web.cases( Rule('/pack.js', h.packer.js_packer), Rule('/pack.css', h.packer.css_packer), auth.login(), auth.logout(), auth | auth_required | web.cases( Rule('/', h.index, method='GET'),
def test_namespace_with_dot(self): app = web.cases( web.namespace("english.docs.news") | web.match('/item', 'item'), ) r = web.Reverse.from_handler(app) self.assertEqual(r.english.docs.news.item.as_url, '/item')