本文整理汇总了Python中storage.Storage.controller方法的典型用法代码示例。如果您正苦于以下问题:Python Storage.controller方法的具体用法?Python Storage.controller怎么用?Python Storage.controller使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类storage.Storage
的用法示例。
在下文中一共展示了Storage.controller方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testURL
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def testURL(self):
self.assertEqual(URL('a', 'c', 'f', args='1'), '/a/c/f/1')
self.assertEqual(URL('a', 'c', 'f', args=('1', '2')), '/a/c/f/1/2')
self.assertEqual(URL('a', 'c', 'f', args=['1', '2']), '/a/c/f/1/2')
self.assertEqual(URL('a', 'c', '/f'), '/a/c/f')
self.assertEqual(URL('a', 'c', 'f.json'), '/a/c/f.json')
self.assertRaises(SyntaxError, URL, *['a'])
request = Storage()
request.application = 'a'
request.controller = 'c'
request.function = 'f'
request.env = {}
from globals import current # Can't be moved with other import
current.request = request
must_return = '/a/c/f'
self.assertEqual(URL(), must_return)
self.assertEqual(URL('f'), must_return)
self.assertEqual(URL('c', 'f'), must_return)
self.assertEqual(URL('a', 'c', 'f'), must_return)
self.assertEqual(URL('a', 'c', 'f', extension='json'), '/a/c/f.json')
def weird():
pass
self.assertEqual(URL('a', 'c', weird), '/a/c/weird')
self.assertRaises(SyntaxError, URL, *['a', 'c', 1])
# test signature
rtn = URL(
a='a', c='c', f='f', args=['x', 'y', 'z'],
vars={'p': (1, 3), 'q': 2}, anchor='1', hmac_key='key'
)
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1')
# test _signature exclusion
rtn = URL(
a='a', c='c', f='f', args=['x', 'y', 'z'],
vars={'p': (1, 3), 'q': 2, '_signature': 'abc'},
anchor='1', hmac_key='key'
)
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1')
# emulate user_signature
current.session = Storage(auth=Storage(hmac_key='key'))
self.assertEqual(URL(user_signature=True), '/a/c/f?_signature=c4aed53c08cff08f369dbf8b5ba51889430cf2c2')
# hash_vars combination
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key')
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars=True)
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars=False)
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=0b5a0702039992aad23c82794b8496e5dcd59a5b')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars=['p'])
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars='p')
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
# test CRLF detection
self.assertRaises(SyntaxError, URL, *['a\n', 'c', 'f'])
self.assertRaises(SyntaxError, URL, *['a\r', 'c', 'f'])
示例2: test_verifyURL
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def test_verifyURL(self):
r = Storage()
r.application = "a"
r.controller = "c"
r.function = "f"
r.extension = "html"
r.env = {}
r.get_vars = Storage()
# missing signature as request.get_vars returns False
rtn = verifyURL(r, "key")
self.assertEqual(rtn, False)
# reverse tests from previous testcase with hash_vars combinations
r.args = ["x", "y", "z"]
r.get_vars = Storage(p=(1, 3), q=2)
# add signature
r.get_vars["_signature"] = "a32530f0d0caa80964bb92aad2bedf8a4486a31f"
rtn = verifyURL(r, "key")
self.assertEqual(rtn, True)
r.get_vars["_signature"] = "a32530f0d0caa80964bb92aad2bedf8a4486a31f"
rtn = verifyURL(r, "key", hash_vars=True)
self.assertEqual(rtn, True)
r.get_vars["_signature"] = "0b5a0702039992aad23c82794b8496e5dcd59a5b"
rtn = verifyURL(r, "key", hash_vars=False)
self.assertEqual(rtn, True)
r.get_vars["_signature"] = "5d01b982fd72b39674b012e0288071034e156d7a"
rtn = verifyURL(r, "key", hash_vars=["p"])
self.assertEqual(rtn, True)
r.get_vars["_signature"] = "5d01b982fd72b39674b012e0288071034e156d7a"
rtn = verifyURL(r, "key", hash_vars="p")
self.assertEqual(rtn, True)
# without session, user_signature returns always False
rtn = verifyURL(r, user_signature=True)
self.assertEqual(rtn, False)
# same goes if you don't use an hmac_key
rtn = verifyURL(r)
self.assertEqual(rtn, False)
# emulate user signature
from globals import current
current.session = Storage(auth=Storage(hmac_key="key"))
r.get_vars["_signature"] = "a32530f0d0caa80964bb92aad2bedf8a4486a31f"
rtn = verifyURL(r, user_signature=True)
self.assertEqual(rtn, True)
示例3: test_verifyURL
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def test_verifyURL(self):
r = Storage()
r.application = 'a'
r.controller = 'c'
r.function = 'f'
r.extension = 'html'
r.env = {}
r.get_vars = Storage()
# missing signature as request.get_vars returns False
rtn = verifyURL(r, 'key')
self.assertEqual(rtn, False)
# reverse tests from previous testcase with hash_vars combinations
r.args = ['x', 'y', 'z']
r.get_vars = Storage(p=(1, 3), q=2)
# add signature
r.get_vars['_signature'] = 'a32530f0d0caa80964bb92aad2bedf8a4486a31f'
rtn = verifyURL(r, 'key')
self.assertEqual(rtn, True)
r.get_vars['_signature'] = 'a32530f0d0caa80964bb92aad2bedf8a4486a31f'
rtn = verifyURL(r, 'key', hash_vars=True)
self.assertEqual(rtn, True)
r.get_vars['_signature'] = '0b5a0702039992aad23c82794b8496e5dcd59a5b'
rtn = verifyURL(r, 'key', hash_vars=False)
self.assertEqual(rtn, True)
r.get_vars['_signature'] = '5d01b982fd72b39674b012e0288071034e156d7a'
rtn = verifyURL(r, 'key', hash_vars=['p'])
self.assertEqual(rtn, True)
r.get_vars['_signature'] = '5d01b982fd72b39674b012e0288071034e156d7a'
rtn = verifyURL(r, 'key', hash_vars='p')
self.assertEqual(rtn, True)
# without session, user_signature returns always False
rtn = verifyURL(r, user_signature=True)
self.assertEqual(rtn, False)
# same goes if you don't use an hmac_key
rtn = verifyURL(r)
self.assertEqual(rtn, False)
# emulate user signature
from globals import current
current.session = Storage(auth=Storage(hmac_key='key'))
r.get_vars['_signature'] = 'a32530f0d0caa80964bb92aad2bedf8a4486a31f'
rtn = verifyURL(r, user_signature=True)
self.assertEqual(rtn, True)
示例4: testURL
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def testURL(self):
self.assertEqual(URL('a', 'c', 'f', args='1'), '/a/c/f/1')
self.assertEqual(URL('a', 'c', 'f', args=('1', '2')), '/a/c/f/1/2')
self.assertEqual(URL('a', 'c', 'f', args=['1', '2']), '/a/c/f/1/2')
self.assertEqual(URL('a', 'c', '/f'), '/a/c/f')
self.assertEqual(URL('a', 'c', 'f.json'), '/a/c/f.json')
self.assertRaises(SyntaxError, URL, *['a'])
request = Storage()
request.application = 'a'
request.controller = 'c'
request.function = 'f'
request.env = {}
from globals import current
current.request = request
must_return = '/a/c/f'
self.assertEqual(URL('f'), must_return)
self.assertEqual(URL('c', 'f'), must_return)
self.assertEqual(URL('a', 'c', 'f'), must_return)
self.assertEqual(URL('a', 'c', 'f', extension='json'), '/a/c/f.json')
def weird():
pass
self.assertEqual(URL('a', 'c', weird), '/a/c/weird')
self.assertRaises(SyntaxError, URL, *['a','c', 1])
示例5: __call__
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def __call__(self, c=None, f='index', args=None, vars=None,
extension=None, target=None, ajax=False, ajax_trap=False,
url=None, user_signature=False, content='loading...', **attr):
if args is None:
args = []
vars = Storage(vars or {})
import globals
target = target or 'c' + str(random.random())[2:]
attr['_id'] = target
request = self.environment['request']
if '.' in f:
f, extension = f.rsplit('.', 1)
if url or ajax:
url = url or html.URL(request.application, c, f, r=request,
args=args, vars=vars, extension=extension,
user_signature=user_signature)
script = html.SCRIPT('web2py_component("%s","%s")' % (url, target),
_type="text/javascript")
return html.TAG[''](script, html.DIV(content, **attr))
else:
if not isinstance(args, (list, tuple)):
args = [args]
c = c or request.controller
other_request = Storage(request)
other_request['env'] = Storage(request.env)
other_request.controller = c
other_request.function = f
other_request.extension = extension or request.extension
other_request.args = List(args)
other_request.vars = vars
other_request.get_vars = vars
other_request.post_vars = Storage()
other_response = globals.Response()
other_request.env.path_info = '/' + \
'/'.join([request.application, c, f] +
map(str, other_request.args))
other_request.env.query_string = \
vars and html.URL(vars=vars).split('?')[1] or ''
other_request.env.http_web2py_component_location = \
request.env.path_info
other_request.cid = target
other_request.env.http_web2py_component_element = target
other_response.view = '%s/%s.%s' % (c, f, other_request.extension)
other_environment = copy.copy(self.environment)
other_response._view_environment = other_environment
other_response.generic_patterns = \
copy.copy(current.response.generic_patterns)
other_environment['request'] = other_request
other_environment['response'] = other_response
## some magic here because current are thread-locals
original_request, current.request = current.request, other_request
original_response, current.response = current.response, other_response
page = run_controller_in(c, f, other_environment)
if isinstance(page, dict):
other_response._vars = page
other_response._view_environment.update(page)
run_view_in(other_response._view_environment)
page = other_response.body.getvalue()
current.request, current.response = original_request, original_response
js = None
if ajax_trap:
link = html.URL(request.application, c, f, r=request,
args=args, vars=vars, extension=extension,
user_signature=user_signature)
js = "web2py_trap_form('%s','%s');" % (link, target)
script = js and html.SCRIPT(js, _type="text/javascript") or ''
return html.TAG[''](html.DIV(html.XML(page), **attr), script)
示例6: LOAD
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def LOAD(c=None, f='index', args=None, vars=None,
extension=None, target=None, ajax=False, ajax_trap=False,
url=None, user_signature=False, timeout=None, times=1,
content='loading...', **attr):
""" LOAD a component into the action's document
Timing options:
-times: An integer or string ("infinity"/"continuous")
specifies how many times the component is requested
-timeout (milliseconds): specifies the time to wait before
starting the request or the frequency if times is greater than
1 or "infinity".
Timing options default to the normal behavior. The component
is added on page loading without delay.
"""
from html import TAG, DIV, URL, SCRIPT, XML
if args is None:
args = []
vars = Storage(vars or {})
target = target or 'c' + str(random.random())[2:]
attr['_id'] = target
request = current.request
if '.' in f:
f, extension = f.rsplit('.', 1)
if url or ajax:
url = url or URL(request.application, c, f, r=request,
args=args, vars=vars, extension=extension,
user_signature=user_signature)
# timing options
if isinstance(times, basestring):
if times.upper() in ("INFINITY", "CONTINUOUS"):
times = "Infinity"
else:
raise TypeError("Unsupported times argument %s" % times)
elif isinstance(times, int):
if times <= 0:
raise ValueError("Times argument must be greater than zero, 'Infinity' or None")
else:
raise TypeError("Unsupported times argument type %s" % type(times))
if timeout is not None:
if not isinstance(timeout, (int, long)):
raise ValueError("Timeout argument must be an integer or None")
elif timeout <= 0:
raise ValueError(
"Timeout argument must be greater than zero or None")
statement = "web2py_component('%s','%s', %s, %s);" \
% (url, target, timeout, times)
else:
statement = "web2py_component('%s','%s');" % (url, target)
script = SCRIPT(statement, _type="text/javascript")
if not content is None:
return TAG[''](script, DIV(content, **attr))
else:
return TAG[''](script)
else:
if not isinstance(args, (list, tuple)):
args = [args]
c = c or request.controller
other_request = Storage(request)
other_request['env'] = Storage(request.env)
other_request.controller = c
other_request.function = f
other_request.extension = extension or request.extension
other_request.args = List(args)
other_request.vars = vars
other_request.get_vars = vars
other_request.post_vars = Storage()
other_response = Response()
other_request.env.path_info = '/' + \
'/'.join([request.application, c, f] +
map(str, other_request.args))
other_request.env.query_string = \
vars and URL(vars=vars).split('?')[1] or ''
other_request.env.http_web2py_component_location = \
request.env.path_info
other_request.cid = target
other_request.env.http_web2py_component_element = target
other_response.view = '%s/%s.%s' % (c, f, other_request.extension)
other_environment = copy.copy(current.globalenv) # NASTY
other_response._view_environment = other_environment
other_response.generic_patterns = \
copy.copy(current.response.generic_patterns)
other_environment['request'] = other_request
other_environment['response'] = other_response
## some magic here because current are thread-locals
original_request, current.request = current.request, other_request
original_response, current.response = current.response, other_response
page = run_controller_in(c, f, other_environment)
if isinstance(page, dict):
other_response._vars = page
other_response._view_environment.update(page)
run_view_in(other_response._view_environment)
page = other_response.body.getvalue()
current.request, current.response = original_request, original_response
js = None
#.........这里部分代码省略.........
示例7: test_URL
# 需要导入模块: from storage import Storage [as 别名]
# 或者: from storage.Storage import controller [as 别名]
def test_URL(self):
self.assertEqual(URL("a", "c", "f", args="1"), "/a/c/f/1")
self.assertEqual(URL("a", "c", "f", args=("1", "2")), "/a/c/f/1/2")
self.assertEqual(URL("a", "c", "f", args=["1", "2"]), "/a/c/f/1/2")
self.assertEqual(URL("a", "c", "/f"), "/a/c/f")
self.assertEqual(URL("a", "c", "f.json"), "/a/c/f.json")
self.assertRaises(SyntaxError, URL, *["a"])
request = Storage()
request.application = "a"
request.controller = "c"
request.function = "f"
request.env = {}
from globals import current # Can't be moved with other import
current.request = request
must_return = "/a/c/f"
self.assertEqual(URL(), must_return)
self.assertEqual(URL("f"), must_return)
self.assertEqual(URL("c", "f"), must_return)
self.assertEqual(URL("a", "c", "f"), must_return)
self.assertEqual(URL("a", "c", "f", extension="json"), "/a/c/f.json")
def weird():
pass
self.assertEqual(URL("a", "c", weird), "/a/c/weird")
self.assertRaises(SyntaxError, URL, *["a", "c", 1])
# test signature
rtn = URL(a="a", c="c", f="f", args=["x", "y", "z"], vars={"p": (1, 3), "q": 2}, anchor="1", hmac_key="key")
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1")
# test _signature exclusion
rtn = URL(
a="a",
c="c",
f="f",
args=["x", "y", "z"],
vars={"p": (1, 3), "q": 2, "_signature": "abc"},
anchor="1",
hmac_key="key",
)
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1")
# emulate user_signature
current.session = Storage(auth=Storage(hmac_key="key"))
self.assertEqual(URL(user_signature=True), "/a/c/f?_signature=c4aed53c08cff08f369dbf8b5ba51889430cf2c2")
# hash_vars combination
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"p": (1, 3), "q": 2}, hmac_key="key")
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f")
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"p": (1, 3), "q": 2}, hmac_key="key", hash_vars=True)
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f")
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"p": (1, 3), "q": 2}, hmac_key="key", hash_vars=False)
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=0b5a0702039992aad23c82794b8496e5dcd59a5b")
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"p": (1, 3), "q": 2}, hmac_key="key", hash_vars=["p"])
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a")
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"p": (1, 3), "q": 2}, hmac_key="key", hash_vars="p")
self.assertEqual(rtn, "/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a")
# test url_encode
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"maï": (1, 3), "lié": 2}, url_encode=False)
self.assertEqual(rtn, "/a/c/f/x/y/z?li\xc3\xa9=2&ma\xc3\xaf=1&ma\xc3\xaf=3")
rtn = URL("a", "c", "f", args=["x", "y", "z"], vars={"maï": (1, 3), "lié": 2}, url_encode=True)
self.assertEqual(rtn, "/a/c/f/x/y/z?li%C3%A9=2&ma%C3%AF=1&ma%C3%AF=3")
# test CRLF detection
self.assertRaises(SyntaxError, URL, *["a\n", "c", "f"])
self.assertRaises(SyntaxError, URL, *["a\r", "c", "f"])