def login_required(self): def _login_required(env, data, next_handler): if 'user' in env and env.user is not None: return next_handler(env, data) response = web.Response(status=303) response.headers['Location'] = str(env.url_for(self._login).set(next=env.request.path_info)) return response return web.handler(_login_required)
def login_required(self): def _login_required(env, data, next_handler): if "user" in env and env.user is not None: return next_handler(env, data) response = web.Response(status=303) response.headers["Location"] = str(env.url_for(self._login).set(next=env.request.path_info)) return response return web.handler(_login_required)
def test_functions_chain_call(self): "Functions chain call" def handler1(env, data, nh): return nh(env, data) def handler2(env, data, nh): return nh(env, data) chain = web.handler(handler1) | handler2 self.assert_(chain(VersionedStorage(), VersionedStorage()) is None)
def test_chain_with_list_and_postfix(self): "Chain with cases and postfix" def h(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h1(env, data, nh): self.assertEqual(env.count, 1) env["count"] = env["count"] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 2) env["count"] = env["count"] + 1 chain = web.handler(h) | web.cases(h1, web.handler(h1) | h2) | h2 count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count["count"], 0)
def test_functions_chain_call(self): 'Functions chain call' def handler1(env, data, nh): return nh(env, data) def handler2(env, data, nh): return nh(env, data) chain = web.handler(handler1) | handler2 self.assert_(chain(VersionedStorage(), VersionedStorage()) is None)
def test_chain_with_list_and_postfix(self): 'Chain with cases and postfix' def h(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h1(env, data, nh): self.assertEqual(env.count, 1) env['count'] = env['count'] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 2) env['count'] = env['count'] + 1 chain = web.handler(h) | web.cases(h1, web.handler(h1) | h2) | h2 count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count['count'], 0)
def test_functions_chain(self): '''Functions as a chain of handlers''' def handler1(env, data, nh): return nh(env, data) def handler2(env, data, nh): return nh(env, data) def handler3(env, data, nh): return nh(env, data) chain = web.handler(handler1) | handler2 | handler3 handler = chain._next_handler self.assert_(isinstance(handler, web.handler)) self.assertEqual(handler.func, handler2) handler = chain._next_handler._next_handler self.assert_(isinstance(handler, web.handler)) self.assertEqual(handler.func, handler3)
def test_functions_chain(self): """Functions as a chain of handlers""" def handler1(env, data, nh): return nh(env, data) def handler2(env, data, nh): return nh(env, data) def handler3(env, data, nh): return nh(env, data) chain = web.handler(handler1) | handler2 | handler3 handler = chain._next_handler self.assert_(isinstance(handler, web.handler)) self.assertEqual(handler.func, handler2) handler = chain._next_handler._next_handler self.assert_(isinstance(handler, web.handler)) self.assertEqual(handler.func, handler3)
def test_list_of_chains(self): "cases of chains" def h1(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h3(env, data, nh): self.assertEqual(env.count, 1) env["count"] = env["count"] + 1 return nh(env, data) chain = web.cases(h1, web.handler(h2) | h3) count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count["count"], 0)
def test_list_of_chains(self): 'cases of chains' def h1(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h3(env, data, nh): self.assertEqual(env.count, 1) env['count'] = env['count'] + 1 return nh(env, data) chain = web.cases(h1, web.handler(h2) | h3) count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count['count'], 0)
def environment(env, data, next_handler): env.cfg = cfg env.url_for = url_for env.url_for_static = static.construct_reverse() env.template = template env.db = db_maker() env.cache = memcache_client try: return next_handler(env, data) finally: env.db.close() app = web.handler(environment) | web.cases( auth.login_handler | h.render_to('login.html'), auth.logout_handler, # API match('/api/posts', 'api-posts') | web.cases( ctype(ctype.xml) | h.posts_paginator | h.to_xml, ctype(ctype.json) | h.posts_paginator | h.to_json, ), auth | web.cases( match('/', 'posts') | h.posts_paginator | h.render_to('posts.html'), match('/<int:id>', 'post') | h.post_by_id | h.render_to('post.html'), prefix('/posts') | auth.login_required | web.cases( match('/add', 'add-post') | h.post_form | h.render_to('add_post.html'), match('/edit/<int:id>', 'edit-post') | h.edit_post | h.render_to('add_post.html'), match('/delete/<int:id>', 'del-post') | h.del_post | h.render_to('del_post.html') )
env.session_storage = memcached # why not to have url for static files? # useful in templates env.url_for_static = static.construct_reverse() # redirect shortcut env.redirect_to = redirect_to try: return next_handler(env, data) finally: env.db.close() app = web.handler(config) | web.cases( static, auth.login_handler | template.render_to('login'), auth.logout_handler, auth | web.cases( web.match('/', 'dashboard') | views.dashboard, web.prefix('/issue') | web.cases( web.match('/<int:issue>', 'issue') | issue.get | web.cases( web.method('get'), web.method('post') | auth.login_required | issue.update, ) | template.render_to('issue'), ), web.prefix('/proj') | web.cases(