Exemplo n.º 1
0
def traversal_path(path):
    """ Variant of :func:`pyramid.traversal.traversal_path_info` suitable for
    decoding paths that are URL-encoded.

    If this function is passed a Unicode object instead of a sequence of
    bytes as ``path``, that Unicode object *must* directly encodeable to
    ASCII.  For example, u'/foo' will work but u'/<unprintable unicode>' (a
    Unicode object with characters that cannot be encoded to ascii) will
    not. A :exc:`UnicodeEncodeError` will be raised if the Unicode cannot be
    encoded directly to ASCII.
    """
    if isinstance(path, text_type):
        # must not possess characters outside ascii
        path = path.encode("ascii")
    # we unquote this path exactly like a PEP 3333 server would
    path = unquote_bytes_to_wsgi(path)  # result will be a native string
    return traversal_path_info(path)  # result will be a tuple of unicode
Exemplo n.º 2
0
def traversal_path(path):
    """ Variant of :func:`pyramid.traversal.traversal_path_info` suitable for
    decoding paths that are URL-encoded.

    If this function is passed a Unicode object instead of a sequence of
    bytes as ``path``, that Unicode object *must* directly encodeable to
    ASCII.  For example, u'/foo' will work but u'/<unprintable unicode>' (a
    Unicode object with characters that cannot be encoded to ascii) will
    not. A :exc:`UnicodeEncodeError` will be raised if the Unicode cannot be
    encoded directly to ASCII.
    """
    if isinstance(path, text_type):
        # must not possess characters outside ascii
        path = path.encode('ascii')
    # we unquote this path exactly like a PEP 3333 server would
    path = unquote_bytes_to_wsgi(path) # result will be a native string
    return traversal_path_info(path) # result will be a tuple of unicode
Exemplo n.º 3
0
def embed(request, *elements, **kw):
    """ as_user=True for current user
    """
    # Should really be more careful about what gets included instead.
    # Cache cut response time from ~800ms to ~420ms.
    as_user = kw.get('as_user')
    path = join(*elements)
    path = unquote_bytes_to_wsgi(native_(path))
    if as_user is not None:
        result, embedded, linked = _embed(request, path, as_user)
    else:
        cached = embed_cache.get(path, None)
        if cached is None:
            cached = _embed(request, path)
            embed_cache[path] = cached
        result, embedded, linked = cached
        result = deepcopy(result)
    request._embedded_uuids.update(embedded)
    request._linked_uuids.update(linked)
    return result
Exemplo n.º 4
0
def embed(request, *elements, **kw):
    """ as_user=True for current user
    """
    # Should really be more careful about what gets included instead.
    # Cache cut response time from ~800ms to ~420ms.
    as_user = kw.get('as_user')
    path = join(*elements)
    path = unquote_bytes_to_wsgi(native_(path))
    log.debug('embed: %s', path)
    if as_user is not None:
        result, embedded, linked = _embed(request, path, as_user)
    else:
        cached = embed_cache.get(path, None)
        if cached is None:
            cached = _embed(request, path)
            embed_cache[path] = cached
        result, embedded, linked = cached
        result = deepcopy(result)
    request._embedded_uuids.update(embedded)
    request._linked_uuids.update(linked)
    return result
Exemplo n.º 5
0
def embed(request, *elements, **kw):
    """
    Incredibly important function that is central to getting views in snovault.
    Since it is a reified method on Request, you can call it like:
    `request.embed(<elements to be joined in path>)`
    This function handles propogation of important request attrs to subrequests,
    as well as caching of requests and grabbing attrs from the subreq result.

    Check connection.py and cache.py for details on the embed_cache

    NOTES:
        path is formed by joining all positional args
        as_user=True for current user
        Pass in fields_to_embed as a keyword arg

    Args:
        request: Request calling this method
        *elements: variable length positional args used to make path
        **kw: arbitrary keyword arguments

    Returns:
        result of the invoked request
    """
    # Should really be more careful about what gets included instead.
    # Cache cut response time from ~800ms to ~420ms.
    embed_cache = request.registry[CONNECTION].embed_cache
    as_user = kw.get('as_user')
    index_uuid = kw.get('index_uuid')
    path = join(*elements)
    path = unquote_bytes_to_wsgi(native_(path))
    # as_user controls whether or not the embed_cache is used
    # if request._indexing_view is True, always use the cache
    if as_user is not None and not request._indexing_view:
        cached = _embed(request, path, as_user)
    else:
        cached = embed_cache.get(path, None)
        if cached is None:
            # handle common cases of as_user, otherwise use what's given
            subreq_user = '******' if as_user is None else as_user
            cached = _embed(request, path, as_user=subreq_user)
            # caching audits is safe because they don't add to linked_uuids
            embed_cache[path] = cached

    # NOTE: if result was retrieved from ES, the following cached attrs will be
    # empty: _aggregated_items, _linked_uuids, _rev_linked_by_item
    result = deepcopy(cached['result'])

    # aggregated_items may be cached; if so, add them to the request
    # these conditions only fulfilled when using @@embedded and aggregated
    # items have NOT yet been processed (_aggregate_for is removed if so)
    if index_uuid and getattr(request, '_aggregate_for').get('uuid') == index_uuid:
        request._aggregated_items = cached['_aggregated_items']
        request._aggregate_for['uuid'] = None
    # hardcode this because audits can cause serious problems with frame=page
    if '@@audit' not in path:
        request._linked_uuids.update(cached['_linked_uuids'])
        request._sid_cache.update(cached['_sid_cache'])
        # this is required because rev_linked_uuids_by_item is formatted as
        # a dict keyed by item with value of set of uuids rev linking to that item
        for item, rev_links in cached['_rev_linked_by_item'].items():
            if item in request._rev_linked_uuids_by_item:
                request._rev_linked_uuids_by_item[item].update(rev_links)
            else:
                request._rev_linked_uuids_by_item[item] = rev_links
    return result