| | |
| | | |
| | | import os |
| | | |
| | | from pyramid.interfaces import ( |
| | | IResourceURL, |
| | | IRoutesMapper, |
| | | IStaticURLInfo, |
| | | ) |
| | | from pyramid.interfaces import IResourceURL, IRoutesMapper, IStaticURLInfo |
| | | |
| | | from pyramid.compat import ( |
| | | bytes_, |
| | | lru_cache, |
| | | string_types, |
| | | ) |
| | | from pyramid.encode import ( |
| | | url_quote, |
| | | urlencode, |
| | | ) |
| | | from pyramid.compat import bytes_, lru_cache, string_types |
| | | from pyramid.encode import url_quote, urlencode |
| | | from pyramid.path import caller_package |
| | | from pyramid.threadlocal import get_current_registry |
| | | |
| | |
| | | quote_path_segment, |
| | | PATH_SAFE, |
| | | PATH_SEGMENT_SAFE, |
| | | ) |
| | | ) |
| | | |
| | | QUERY_SAFE = "/?:@!$&'()*+,;=" # RFC 3986 |
| | | QUERY_SAFE = "/?:@!$&'()*+,;=" # RFC 3986 |
| | | ANCHOR_SAFE = QUERY_SAFE |
| | | |
| | | |
| | | def parse_url_overrides(request, kw): |
| | | """ |
| | |
| | | anchor = kw.pop('_anchor', '') |
| | | |
| | | if app_url is None: |
| | | if (scheme is not None or host is not None or port is not None): |
| | | if scheme is not None or host is not None or port is not None: |
| | | app_url = request._partial_application_url(scheme, host, port) |
| | | else: |
| | | app_url = request.application_url |
| | |
| | | frag = '#' + url_quote(anchor, ANCHOR_SAFE) |
| | | |
| | | return app_url, qs, frag |
| | | |
| | | |
| | | class URLMethodsMixin(object): |
| | | """ Request methods mixin for BaseRequest having to do with URL |
| | |
| | | if port: |
| | | url += ':%s' % port |
| | | |
| | | url_encoding = getattr(self, 'url_encoding', 'utf-8') # webob 1.2b3+ |
| | | url_encoding = getattr(self, 'url_encoding', 'utf-8') # webob 1.2b3+ |
| | | bscript_name = bytes_(self.script_name, url_encoding) |
| | | return url + url_quote(bscript_name, PATH_SAFE) |
| | | |
| | |
| | | try: |
| | | reg = self.registry |
| | | except AttributeError: |
| | | reg = get_current_registry() # b/c |
| | | reg = get_current_registry() # b/c |
| | | mapper = reg.getUtility(IRoutesMapper) |
| | | route = mapper.get_route(route_name) |
| | | |
| | |
| | | |
| | | app_url, qs, anchor = parse_url_overrides(self, kw) |
| | | |
| | | path = route.generate(kw) # raises KeyError if generate fails |
| | | path = route.generate(kw) # raises KeyError if generate fails |
| | | |
| | | if elements: |
| | | suffix = _join_elements(elements) |
| | |
| | | try: |
| | | reg = self.registry |
| | | except AttributeError: |
| | | reg = get_current_registry() # b/c |
| | | reg = get_current_registry() # b/c |
| | | |
| | | url_adapter = reg.queryMultiAdapter((resource, self), IResourceURL) |
| | | if url_adapter is None: |
| | |
| | | virtual_path = getattr(url_adapter, 'virtual_path', None) |
| | | |
| | | urlkw = {} |
| | | for name in ( |
| | | 'app_url', 'scheme', 'host', 'port', 'query', 'anchor' |
| | | ): |
| | | for name in ('app_url', 'scheme', 'host', 'port', 'query', 'anchor'): |
| | | val = kw.get(name, None) |
| | | if val is not None: |
| | | urlkw['_' + name] = val |
| | |
| | | |
| | | return resource_url + suffix + qs + anchor |
| | | |
| | | model_url = resource_url # b/w compat forever |
| | | model_url = resource_url # b/w compat forever |
| | | |
| | | def resource_path(self, resource, *elements, **kw): |
| | | """ |
| | |
| | | try: |
| | | reg = self.registry |
| | | except AttributeError: |
| | | reg = get_current_registry() # b/c |
| | | reg = get_current_registry() # b/c |
| | | |
| | | info = reg.queryUtility(IStaticURLInfo) |
| | | if info is None: |
| | |
| | | """ |
| | | return request.route_url(route_name, *elements, **kw) |
| | | |
| | | |
| | | def route_path(route_name, request, *elements, **kw): |
| | | """ |
| | | This is a backwards compatibility function. Its result is the same as |
| | |
| | | See :meth:`pyramid.request.Request.route_path` for more information. |
| | | """ |
| | | return request.route_path(route_name, *elements, **kw) |
| | | |
| | | |
| | | def resource_url(resource, request, *elements, **kw): |
| | | """ |
| | |
| | | """ |
| | | return request.resource_url(resource, *elements, **kw) |
| | | |
| | | model_url = resource_url # b/w compat (forever) |
| | | |
| | | model_url = resource_url # b/w compat (forever) |
| | | |
| | | |
| | | def static_url(path, request, **kw): |
| | |
| | | path = '%s:%s' % (package.__name__, path) |
| | | return request.static_path(path, **kw) |
| | | |
| | | |
| | | def current_route_url(request, *elements, **kw): |
| | | """ |
| | | This is a backwards compatibility function. Its result is the same as |
| | |
| | | information. |
| | | """ |
| | | return request.current_route_url(*elements, **kw) |
| | | |
| | | |
| | | def current_route_path(request, *elements, **kw): |
| | | """ |
| | |
| | | """ |
| | | return request.current_route_path(*elements, **kw) |
| | | |
| | | |
| | | @lru_cache(1000) |
| | | def _join_elements(elements): |
| | | return '/'.join([quote_path_segment(s, safe=PATH_SEGMENT_SAFE) for s in elements]) |
| | | return '/'.join( |
| | | [quote_path_segment(s, safe=PATH_SEGMENT_SAFE) for s in elements] |
| | | ) |