本文整理汇总了Python中werkzeug.routing.RequestRedirect方法的典型用法代码示例。如果您正苦于以下问题:Python routing.RequestRedirect方法的具体用法?Python routing.RequestRedirect怎么用?Python routing.RequestRedirect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.routing
的用法示例。
在下文中一共展示了routing.RequestRedirect方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: match_request
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def match_request(self) -> None:
"""Match the request against the adapter.
Override this method to configure request matching, it should
set the request url_rule and view_args and optionally a
routing_exception.
"""
try:
(
self.request_websocket.url_rule,
self.request_websocket.view_args,
) = self.url_adapter.match(
return_rule=True
) # noqa
except WBadRequest:
self.request_websocket.routing_exception = BadRequest()
except WNotFound:
self.request_websocket.routing_exception = NotFound()
except WMethodNotAllowed as error:
new_error = MethodNotAllowed(error.valid_methods)
self.request_websocket.routing_exception = new_error
except WRequestRedirect as error:
new_error = RedirectRequired(error.new_url) # type: ignore
self.request_websocket.routing_exception = new_error
示例2: test_http_exception
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_http_exception(self):
for exception_cls in HTTPException.__subclasses__():
if exception_cls is RequestRedirect or exception_cls().code == 412:
continue
exception_instance = exception_cls()
self.add_route_raises_exception(
ExceptionHandlingSpec(
Exception, broad_exception_handler, exception_instance
)
)
resp = self.request()
self.assertEqual(exception_instance.code, resp.status_code)
self.assertTrue(resp.is_json)
self.assertDictEqual({"error": exception_instance.description}, resp.json)
示例3: dispatch_url
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def dispatch_url(self, url_string):
url, url_adapter, query_args = self.parse_url(url_string)
try:
endpoint, kwargs = url_adapter.match()
except NotFound:
raise NotSupported(url_string)
except RequestRedirect as e:
new_url = "{0.new_url}?{1}".format(e, url_encode(query_args))
return self.dispatch_url(new_url)
try:
handler = import_string(endpoint)
request = Request(url=url, args=query_args)
return handler(request, **kwargs)
except RequestRedirect as e:
return self.dispatch_url(e.new_url)
示例4: get_view_function
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def get_view_function(url, method='GET'):
adapter = current_app.url_map.bind('localhost')
try:
match = adapter.match(url, method=method)
except RequestRedirect as e:
# recursively match redirects
return get_view_function(e.new_url, method)
except (MethodNotAllowed, NotFound):
# no match
return None
try:
# return the view function and arguments
return current_app.view_functions[match[0]], match[1]
except KeyError:
# no view is associated with the endpoint
return None
示例5: raise_routing_exception
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def raise_routing_exception(self, request):
"""Exceptions that are recording during routing are reraised with
this method. During debug we are not reraising redirect requests
for non ``GET``, ``HEAD``, or ``OPTIONS`` requests and we're raising
a different error instead to help debug situations.
:internal:
"""
if not self.debug \
or not isinstance(request.routing_exception, RequestRedirect) \
or request.method in ('GET', 'HEAD', 'OPTIONS'):
raise request.routing_exception
from .debughelpers import FormDataRoutingRedirect
raise FormDataRoutingRedirect(request)
示例6: _does_route_exist
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def _does_route_exist(self, url: str, method: str) -> bool:
adapter = self.app.url_map.bind('')
try:
adapter.match(url, method=method)
except RequestRedirect as e:
# recursively match redirects
return self._does_route_exist(e.new_url, method)
except (MethodNotAllowed, NotFound):
# no match
return False
return True
示例7: test_defaults
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_defaults(self):
map = r.Map([
r.Rule('/foo/', defaults={'page': 1}, endpoint='foo'),
r.Rule('/foo/<int:page>', endpoint='foo')
])
adapter = map.bind('example.org', '/')
assert adapter.match('/foo/') == ('foo', {'page': 1})
self.assert_raises(r.RequestRedirect, lambda: adapter.match('/foo/1'))
assert adapter.match('/foo/2') == ('foo', {'page': 2})
assert adapter.build('foo', {}) == '/foo/'
assert adapter.build('foo', {'page': 1}) == '/foo/'
assert adapter.build('foo', {'page': 2}) == '/foo/2'
示例8: test_path
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_path(self):
map = r.Map([
r.Rule('/', defaults={'name': 'FrontPage'}, endpoint='page'),
r.Rule('/Special', endpoint='special'),
r.Rule('/<int:year>', endpoint='year'),
r.Rule('/<path:name>', endpoint='page'),
r.Rule('/<path:name>/edit', endpoint='editpage'),
r.Rule('/<path:name>/silly/<path:name2>', endpoint='sillypage'),
r.Rule('/<path:name>/silly/<path:name2>/edit', endpoint='editsillypage'),
r.Rule('/Talk:<path:name>', endpoint='talk'),
r.Rule('/User:<username>', endpoint='user'),
r.Rule('/User:<username>/<path:name>', endpoint='userpage'),
r.Rule('/Files/<path:file>', endpoint='files'),
])
adapter = map.bind('example.org', '/')
assert adapter.match('/') == ('page', {'name':'FrontPage'})
self.assert_raises(r.RequestRedirect, lambda: adapter.match('/FrontPage'))
assert adapter.match('/Special') == ('special', {})
assert adapter.match('/2007') == ('year', {'year':2007})
assert adapter.match('/Some/Page') == ('page', {'name':'Some/Page'})
assert adapter.match('/Some/Page/edit') == ('editpage', {'name':'Some/Page'})
assert adapter.match('/Foo/silly/bar') == ('sillypage', {'name':'Foo', 'name2':'bar'})
assert adapter.match('/Foo/silly/bar/edit') == ('editsillypage', {'name':'Foo', 'name2':'bar'})
assert adapter.match('/Talk:Foo/Bar') == ('talk', {'name':'Foo/Bar'})
assert adapter.match('/User:thomas') == ('user', {'username':'thomas'})
assert adapter.match('/User:thomas/projects/werkzeug') == \
('userpage', {'username':'thomas', 'name':'projects/werkzeug'})
assert adapter.match('/Files/downloads/werkzeug/0.2.zip') == \
('files', {'file':'downloads/werkzeug/0.2.zip'})
示例9: test_request_direct_charset_bug
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_request_direct_charset_bug(self):
map = r.Map([r.Rule(u'/öäü/')])
adapter = map.bind('localhost', '/')
try:
adapter.match(u'/öäü')
except r.RequestRedirect as e:
assert e.new_url == 'http://localhost/%C3%B6%C3%A4%C3%BC/'
else:
self.fail('expected request redirect exception')
示例10: test_request_redirect_default_subdomain
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_request_redirect_default_subdomain(self):
map = r.Map([r.Rule(u'/foo', defaults={'bar': 42}, subdomain='test'),
r.Rule(u'/foo/<int:bar>', subdomain='other')])
adapter = map.bind('localhost', '/', subdomain='other')
try:
adapter.match(u'/foo/42')
except r.RequestRedirect as e:
assert e.new_url == 'http://test.localhost/foo'
else:
self.fail('expected request redirect exception')
示例11: test_alias_redirects
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_alias_redirects(self):
m = r.Map([
r.Rule('/', endpoint='index'),
r.Rule('/index.html', endpoint='index', alias=True),
r.Rule('/users/', defaults={'page': 1}, endpoint='users'),
r.Rule('/users/index.html', defaults={'page': 1}, alias=True,
endpoint='users'),
r.Rule('/users/page/<int:page>', endpoint='users'),
r.Rule('/users/page-<int:page>.html', alias=True, endpoint='users'),
])
a = m.bind('example.com')
def ensure_redirect(path, new_url, args=None):
try:
a.match(path, query_args=args)
except r.RequestRedirect as e:
assert e.new_url == 'http://example.com' + new_url
else:
assert False, 'expected redirect'
ensure_redirect('/index.html', '/')
ensure_redirect('/users/index.html', '/users/')
ensure_redirect('/users/page-2.html', '/users/page/2')
ensure_redirect('/users/page-1.html', '/users/')
ensure_redirect('/users/page-1.html', '/users/?foo=bar', {'foo': 'bar'})
assert a.build('index') == '/'
assert a.build('users', {'page': 1}) == '/users/'
assert a.build('users', {'page': 2}) == '/users/page/2'
示例12: test_host_matching
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_host_matching(self):
m = r.Map([
r.Rule('/', endpoint='index', host='www.<domain>'),
r.Rule('/', endpoint='files', host='files.<domain>'),
r.Rule('/foo/', defaults={'page': 1}, host='www.<domain>', endpoint='x'),
r.Rule('/<int:page>', host='files.<domain>', endpoint='x')
], host_matching=True)
a = m.bind('www.example.com')
assert a.match('/') == ('index', {'domain': 'example.com'})
assert a.match('/foo/') == ('x', {'domain': 'example.com', 'page': 1})
try:
a.match('/foo')
except r.RequestRedirect as e:
assert e.new_url == 'http://www.example.com/foo/'
else:
assert False, 'expected redirect'
a = m.bind('files.example.com')
assert a.match('/') == ('files', {'domain': 'example.com'})
assert a.match('/2') == ('x', {'domain': 'example.com', 'page': 2})
try:
a.match('/1')
except r.RequestRedirect as e:
assert e.new_url == 'http://www.example.com/foo/'
else:
assert False, 'expected redirect'
示例13: test_redirect_request_exception_code
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import RequestRedirect [as 别名]
def test_redirect_request_exception_code(self):
exc = r.RequestRedirect('http://www.google.com/')
exc.code = 307
env = create_environ()
self.assert_strict_equal(exc.get_response(env).status_code, exc.code)