Ejemplo n.º 1
0
def _pagination_links(resource, req, documents_count):
    """Returns the appropriate set of resource links depending on the
    current page and the total number of documents returned by the query.

    :param resource: the resource name.
    :param req: and instace of :class:`eve.utils.ParsedRequest`.
    :param document_count: the number of documents returned by the query.

    .. versionchanged:: 0.0.5
       Support for optional pagination.

    .. versionchanged:: 0.0.3
       JSON links
    """
    _links = {'parent': home_link(), 'self': collection_link(resource)}

    if documents_count and config.DOMAIN[resource]['pagination']:
        if req.page * req.max_results < documents_count:
            q = querydef(req.max_results, req.where, req.sort, req.page + 1)
            _links['next'] = {'title': 'next page', 'href': '%s%s' %
                              (resource_uri(resource), q)}

        if req.page > 1:
            q = querydef(req.max_results, req.where, req.sort, req.page - 1)
            _links['prev'] = {'title': 'previous page', 'href': '%s%s' %
                              (resource_uri(resource), q)}

    return _links
Ejemplo n.º 2
0
Archivo: get.py Proyecto: klyr/eve
def _pagination_links(resource, req, documents_count):
    """Returns the appropriate set of resource links depending on the
    current page and the total number of documents returned by the query.

    :param resource: the resource name.
    :param req: and instace of :class:`eve.utils.ParsedRequest`.
    :param document_count: the number of documents returned by the query.

    .. versionchanged:: 0.0.7
       Support for Rate-Limiting.

    .. versionchanged:: 0.0.5
       Support for optional pagination.

    .. versionchanged:: 0.0.3
       JSON links
    """
    _links = {"parent": home_link(), "self": collection_link(resource)}

    if documents_count and config.DOMAIN[resource]["pagination"]:
        if req.page * req.max_results < documents_count:
            q = querydef(req.max_results, req.where, req.sort, req.page + 1)
            _links["next"] = {"title": "next page", "href": "%s%s" % (resource_uri(resource), q)}

        if req.page > 1:
            q = querydef(req.max_results, req.where, req.sort, req.page - 1)
            _links["prev"] = {"title": "previous page", "href": "%s%s" % (resource_uri(resource), q)}

    return _links
Ejemplo n.º 3
0
Archivo: get.py Proyecto: Tefnet/eve
def _pagination_links(resource, req, documents_count):
    """Returns the appropriate set of resource links depending on the
    current page and the total number of documents returned by the query.

    :param resource: the resource name.
    :param req: and instace of :class:`eve.utils.ParsedRequest`.
    :param document_count: the number of documents returned by the query.

    .. versionchanged:: 0.0.5
       Support for optional pagination.

    .. versionchanged:: 0.0.3
       JSON links
    """
    _links = {'parent': home_link(), 'self': collection_link(resource)}

    if documents_count and config.DOMAIN[resource]['pagination']:
        if req.page * req.max_results < documents_count:
            q = querydef(req.max_results, req.where, req.sort, req.page + 1)
            _links['next'] = {
                'title': 'next page',
                'href': '%s%s' % (resource_uri(resource), q)
            }

        if req.page > 1:
            q = querydef(req.max_results, req.where, req.sort, req.page - 1)
            _links['prev'] = {
                'title': 'previous page',
                'href': '%s%s' % (resource_uri(resource), q)
            }

    return _links
Ejemplo n.º 4
0
Archivo: get.py Proyecto: simobil/eve
def _pagination_links(resource, req, documents_count):
    """Returns the appropriate set of resource links depending on the
    current page and the total number of documents returned by the query.

    :param resource: the resource name.
    :param req: and instace of :class:`eve.utils.ParsedRequest`.
    :param document_count: the number of documents returned by the query.

    .. versionchanged:: 0.0.8
       Link to last page is provided if pagination is enabled (and the current
       page is not the last one).

    .. versionchanged:: 0.0.7
       Support for Rate-Limiting.

    .. versionchanged:: 0.0.5
       Support for optional pagination.

    .. versionchanged:: 0.0.3
       JSON links
    """
    _links = {'parent': home_link(), 'self': collection_link(resource)}

    if documents_count and config.DOMAIN[resource]['pagination']:
        if req.page * req.max_results < documents_count:
            q = querydef(req.max_results, req.where, req.sort, req.page + 1)
            _links['next'] = {
                'title': 'next page',
                'href': '%s%s' % (resource_uri(resource), q)
            }

            # in python 2.x dividing 2 ints produces an int and that's rounded
            # before the ceil call. Have to cast one value to float to get
            # a correct result. Wonder if 2 casts + ceil() call are actually
            # faster than documents_count // req.max_results and then adding
            # 1 if the modulo is non-zero...
            last_page = int(math.ceil(documents_count /
                                      float(req.max_results)))
            q = querydef(req.max_results, req.where, req.sort, last_page)
            _links['last'] = {
                'title': 'last page',
                'href': '%s%s' % (resource_uri(resource), q)
            }

        if req.page > 1:
            q = querydef(req.max_results, req.where, req.sort, req.page - 1)
            _links['prev'] = {
                'title': 'previous page',
                'href': '%s%s' % (resource_uri(resource), q)
            }

    return _links
Ejemplo n.º 5
0
def home_endpoint():
    """ Home/API entry point. Will provide links to each available resource

    .. versionchanged:: 0.4
       Prevent versioning collections from being added in links.

    .. versionchanged:: 0.2
       Use new 'resource_title' setting for link titles.

    .. versionchanged:: 0.1.0
       Support for optional HATEOAS.
    """
    if config.HATEOAS:
        response = {}
        links = []
        for resource in config.DOMAIN.keys():
            if not resource.endswith(config.VERSIONS):
                links.append({'href': '%s' % resource_uri(resource),
                              'title': '%s' %
                              config.DOMAIN[resource]['resource_title']})
        response[config.LINKS] = {'child': links}
        return send_response(None, (response,))
    else:
        abort(404, debug_error_message("HATEOAS is disabled so we have no data"
                                       " to display at the API homepage."))
Ejemplo n.º 6
0
Archivo: get.py Proyecto: bcattle/eve
def _pagination_links(resource, req, documents_count):
    """Returns the appropriate set of resource links depending on the
    current page and the total number of documents returned by the query.

    :param resource: the resource name.
    :param req: and instace of :class:`eve.utils.ParsedRequest`.
    :param document_count: the number of documents returned by the query.

    .. versionchanged:: 0.0.8
       Link to last page is provided if pagination is enabled (and the current
       page is not the last one).

    .. versionchanged:: 0.0.7
       Support for Rate-Limiting.

    .. versionchanged:: 0.0.5
       Support for optional pagination.

    .. versionchanged:: 0.0.3
       JSON links
    """
    _links = {'parent': home_link(), 'self': collection_link(resource)}

    if documents_count and config.DOMAIN[resource]['pagination']:
        if req.page * req.max_results < documents_count:
            q = querydef(req.max_results, req.where, req.sort, req.page + 1)
            _links['next'] = {'title': 'next page', 'href': '%s%s' %
                              (resource_uri(resource), q)}

            # in python 2.x dividing 2 ints produces an int and that's rounded
            # before the ceil call. Have to cast one value to float to get
            # a correct result. Wonder if 2 casts + ceil() call are actually
            # faster than documents_count // req.max_results and then adding
            # 1 if the modulo is non-zero...
            last_page = int(math.ceil(documents_count
                                      / float(req.max_results)))
            q = querydef(req.max_results, req.where, req.sort, last_page)
            _links['last'] = {'title': 'last page', 'href': '%s%s'
                              % (resource_uri(resource), q)}

        if req.page > 1:
            q = querydef(req.max_results, req.where, req.sort, req.page - 1)
            _links['prev'] = {'title': 'previous page', 'href': '%s%s' %
                              (resource_uri(resource), q)}

    return _links
Ejemplo n.º 7
0
def home_endpoint():
    """ Home/API entry point. Will provide links to each available resource
    """
    response = {}
    links = []
    for resource in config.DOMAIN.keys():
        links.append({'href': '%s' % resource_uri(resource),
                      'title': '%s' % config.URLS[resource]})
    response['_links'] = {'child': links}
    return send_response(None, (response,))
Ejemplo n.º 8
0
def home_endpoint():
    """ Home/API entry point. Will provide links to each available resource
    """
    response = dict()
    links = list()
    for resource in config.DOMAIN.keys():
        links.append("<link rel='child' title='%s' href='%s' />" %
                     (config.URLS[resource], resource_uri(resource)))
    response['links'] = links
    return send_response(None, response)
Ejemplo n.º 9
0
def home_endpoint():
    """ Home/API entry point. Will provide links to each available resource
    """
    response = {}
    links = []
    for resource in config.DOMAIN.keys():
        links.append({
            'href': '%s' % resource_uri(resource),
            'title': '%s' % config.URLS[resource]
        })
    response['_links'] = {'child': links}
    return send_response(None, (response, ))
Ejemplo n.º 10
0
Archivo: get.py Proyecto: marchon/eve
def _pagination_links(resource, req, documents_count):
    """Returns the appropriate set of resource links depending on the
    current page and the total number of documents returned by the query.

    :param resource: the resource name.
    :param req: and instace of :class:`eve.utils.ParsedRequest`.
    :param document_count: the number of documents returned by the query.
    """
    _pagination_links = standard_links(resource)

    if documents_count:
        if req.page * req.max_results < documents_count:
            q = querydef(req.max_results, req.where, req.sort, req.page + 1)
            _pagination_links.append("<link rel='next' title='next page'"
                                     " href='%s%s' />" %
                                     (resource_uri(resource), q))

        if req.page > 1:
            q = querydef(req.max_results, req.where, req.sort, req.page - 1)
            _pagination_links.append("<link rel='prev' title='previous page'"
                                     " href='%s%s' />" %
                                     (resource_uri(resource), q))

    return _pagination_links
Ejemplo n.º 11
0
def home_endpoint():
    """ Home/API entry point. Will provide links to each available resource

    .. versionchanged:: 0.1.0
       Support for optional HATEOAS.
    """
    if config.HATEOAS:
        response = {}
        links = []
        for resource in config.DOMAIN.keys():
            links.append({'href': '%s' % resource_uri(resource),
                          'title': '%s' % config.URLS[resource]})
        response['_links'] = {'child': links}
        return send_response(None, (response,))
    else:
        abort(404, debug_error_message("HATEOAS is disabled so we have no data"
                                       " to display at the API homepage."))
Ejemplo n.º 12
0
def home_endpoint():
    """ Home/API entry point. Will provide links to each available resource

    .. versionchanged:: 0.1.0
       Support for optional HATEOAS.
    """
    if config.HATEOAS:
        response = {}
        links = []
        for resource in config.DOMAIN.keys():
            links.append({
                'href': '%s' % resource_uri(resource),
                'title': '%s' % config.URLS[resource]
            })
        response['_links'] = {'child': links}
        return send_response(None, (response, ))
    else:
        abort(
            404,
            debug_error_message("HATEOAS is disabled so we have no data"
                                " to display at the API homepage."))
Ejemplo n.º 13
0
 def test_resource_uri(self):
     with self.app.test_request_context():
         self.app.config['URL_PROTOCOL'] = 'http'
         self.app.config['SERVER_NAME'] = '0.0.0.0:5000'
         self.assertEqual(resource_uri('users'),
                          'http://0.0.0.0:5000/users')