本文整理汇总了Python中pyramid.urldispatch._compile_route函数的典型用法代码示例。如果您正苦于以下问题:Python _compile_route函数的具体用法?Python _compile_route怎么用?Python _compile_route使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_compile_route函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: auth_request
def auth_request(request):
response = None
# ignore static routes for authentication
if 'static' in request.path.split('/'):
return handler(request)
logged_in_user = request.session.get('logged_in_user', None)
protected_routes = []
routes = DBSession.query(RoutePermission.route_name).distinct().all()
for R in routes:
protected_routes.append(R[0])
#print(protected_routes)
matched_routename = None
for r in request.registry.introspector.get_category('routes'):
R = r['introspectable']
matcher, generator = _compile_route(R['pattern'])
if type(matcher(request.path)) == types.DictionaryType:
#print(R['name'] + ':' + R['pattern'])
matched_routename = R['name']
break
# Check routes from protected routes here.
if matched_routename and matched_routename in protected_routes:
# first check if there is any static permission given and if yes then validate routes against that permission
user_permissions = []
if request.session.get('auth_static_permission', None):
user_permissions.append(request.session.get('auth_static_permission', None))
else:
if not logged_in_user:
return HTTPForbidden()
# get user permissions
user_permissions = request.session.get('auth_user_permissions', [])
# get route permissions for the current route
# match if there are any common permissions and check for all matching request methods
has_permission = DBSession.query(func.count(RoutePermission.permission)).filter(
RoutePermission.route_name == matched_routename).filter(
or_(RoutePermission.method == 'ALL',
RoutePermission.method == request.method)).filter(
RoutePermission.permission.in_(user_permissions)).scalar()
if has_permission > 0:
return handler(request)
else:
return HTTPForbidden()
else:
return handler(request)
示例2: auth_request
def auth_request(request):
response = None
# ignore static routes for authentication
if 'static' in request.path.split('/'):
return handler(request)
logged_in_user = request.session.get('logged_in_user', None)
protected_routes = []
routes = db.query(RoutePermission.route_name).distinct().all()
for R in routes:
protected_routes.append(R[0])
#print(protected_routes)
matched_routename = None
for r in request.registry.introspector.get_category('routes'):
R = r['introspectable']
matcher, generator = _compile_route(R['pattern'])
if isinstance(matcher(request.path), dict):
#print(R['name'] + ':' + R['pattern'])
matched_routename = R['name']
break
# Check routes from protected routes here.
if matched_routename and matched_routename in protected_routes:
# first check if there is any static permission given and if yes then validate routes against that permission
if not logged_in_user:
return HTTPForbidden()
if is_allowed(request, matched_routename, method=['ALL', request.method], check_route=False):
return handler(request)
else:
return HTTPForbidden()
else:
return handler(request)
示例3: generates
def generates(self, pattern, dict, result):
from pyramid.urldispatch import _compile_route
self.assertEqual(_compile_route(pattern)[1](dict), result)
示例4: matches
def matches(self, pattern, path, expected):
from pyramid.urldispatch import _compile_route
matcher = _compile_route(pattern)[0]
result = matcher(path)
self.assertEqual(result, expected)
示例5: _callFUT
def _callFUT(self, pattern):
from pyramid.urldispatch import _compile_route
return _compile_route(pattern)
示例6: make_predicates
#.........这里部分代码省略.........
if header_val is None:
return header_name in request.headers
val = request.headers.get(header_name)
if val is None:
return False
return header_val.match(val) is not None
header_predicate.__text__ = text
weights.append(1 << 5)
predicates.append(header_predicate)
h.update(bytes_('header:%r=%r' % (header_name, header_val)))
if accept is not None:
def accept_predicate(context, request):
return accept in request.accept
accept_predicate.__text__ = "accept = %s" % accept
weights.append(1 << 6)
predicates.append(accept_predicate)
h.update(bytes_('accept:%r' % accept))
if containment is not None:
def containment_predicate(context, request):
return find_interface(context, containment) is not None
containment_predicate.__text__ = "containment = %s" % containment
weights.append(1 << 7)
predicates.append(containment_predicate)
h.update(bytes_('containment:%r' % hash(containment)))
if request_type is not None:
def request_type_predicate(context, request):
return request_type.providedBy(request)
text = "request_type = %s"
request_type_predicate.__text__ = text % request_type
weights.append(1 << 8)
predicates.append(request_type_predicate)
h.update(bytes_('request_type:%r' % hash(request_type)))
if match_param is not None:
if isinstance(match_param, string_types):
match_param, match_param_val = match_param.split('=', 1)
match_param = {match_param: match_param_val}
text = "match_param %s" % match_param
def match_param_predicate(context, request):
for k, v in match_param.items():
if request.matchdict.get(k) != v:
return False
return True
match_param_predicate.__text__ = text
weights.append(1 << 9)
predicates.append(match_param_predicate)
h.update(bytes_('match_param:%r' % match_param))
if custom:
for num, predicate in enumerate(custom):
if getattr(predicate, '__text__', None) is None:
text = '<unknown custom predicate>'
try:
predicate.__text__ = text
except AttributeError:
# if this happens the predicate is probably a classmethod
if hasattr(predicate, '__func__'):
predicate.__func__.__text__ = text
else: # pragma: no cover ; 2.5 doesn't have __func__
predicate.im_func.__text__ = text
predicates.append(predicate)
# using hash() here rather than id() is intentional: we
# want to allow custom predicates that are part of
# frameworks to be able to define custom __hash__
# functions for custom predicates, so that the hash output
# of predicate instances which are "logically the same"
# may compare equal.
h.update(bytes_('custom%s:%r' % (num, hash(predicate))))
weights.append(1 << 10)
if traverse is not None:
# ``traverse`` can only be used as a *route* "predicate"; it
# adds 'traverse' to the matchdict if it's specified in the
# routing args. This causes the ResourceTreeTraverser to use
# the resolved traverse pattern as the traversal path.
from pyramid.urldispatch import _compile_route
_, tgenerate = _compile_route(traverse)
def traverse_predicate(context, request):
if 'traverse' in context:
return True
m = context['match']
tvalue = tgenerate(m)
m['traverse'] = traversal_path_info(tvalue)
return True
# This isn't actually a predicate, it's just a infodict
# modifier that injects ``traverse`` into the matchdict. As a
# result, the ``traverse_predicate`` function above always
# returns True, and we don't need to update the hash or attach
# a weight to it
predicates.append(traverse_predicate)
score = 0
for bit in weights:
score = score | bit
order = (MAX_ORDER - score) / (len(predicates) + 1)
phash = h.hexdigest()
return order, predicates, phash
示例7: __init__
def __init__(self, val, config):
_, self.tgenerate = _compile_route(val)
self.val = val
示例8: hash
# using hash() here rather than id() is intentional: we
# want to allow custom predicates that are part of
# frameworks to be able to define custom __hash__
# functions for custom predicates, so that the hash output
# of predicate instances which are "logically the same"
# may compare equal.
h.update('custom%s:%r' % (num, hash(predicate)))
weights.append(1 << 10)
if traverse is not None:
# ``traverse`` can only be used as a *route* "predicate"; it
# adds 'traverse' to the matchdict if it's specified in the
# routing args. This causes the ResourceTreeTraverser to use
# the resolved traverse pattern as the traversal path.
from pyramid.urldispatch import _compile_route
_, tgenerate = _compile_route(traverse)
def traverse_predicate(context, request):
if 'traverse' in context:
return True
m = context['match']
tvalue = tgenerate(m)
m['traverse'] = traversal_path(tvalue)
return True
# This isn't actually a predicate, it's just a infodict
# modifier that injects ``traverse`` into the matchdict. As a
# result, the ``traverse_predicate`` function above always
# returns True, and we don't need to update the hash or attach
# a weight to it
predicates.append(traverse_predicate)
score = 0