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
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
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
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': { 'title': config.DOMAIN[resource]['resource_title'], 'href': _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' % (_collection_link(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' % (_collection_link(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' % (_collection_link(resource), q) } return _links
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.4 HATOEAS link for contains the business unit value even when regexes have been configured for the resource endpoint. .. 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': {'title': config.DOMAIN[resource]['resource_title'], 'href': resource_link()}} 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_link(), 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_link(), 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_link(), q)} return _links
def paginationResource(cursor): page = request.args.get('page', default=1, type=int) size = request.args.get('size', default=40, type=int) offset = page*size - size total = cursor.count() if total > offset: next = dict(href=request.base_url+querydef(max_results=size, page=page + 1)) else: next = None return cursor.skip(offset).limit(size), next, total
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
def test_querydef(self): self.assertEqual(querydef(max_results=10), "?max_results=10") self.assertEqual(querydef(page=10), "?page=10") self.assertEqual(querydef(where="wherepart"), "?where=wherepart") self.assertEqual(querydef(sort="sortpart"), "?sort=sortpart") self.assertEqual(querydef(where="wherepart", sort="sortpart"), "?where=wherepart&sort=sortpart") self.assertEqual(querydef(max_results=10, sort="sortpart"), "?max_results=10&sort=sortpart")
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
def test_querydef(self): self.assertEqual(querydef(max_results=10), '?max_results=10') self.assertEqual(querydef(page=10), '?page=10') self.assertEqual(querydef(where='wherepart'), '?where=wherepart') self.assertEqual(querydef(sort='sortpart'), '?sort=sortpart') self.assertEqual(querydef(where='wherepart', sort='sortpart'), '?where=wherepart&sort=sortpart') self.assertEqual(querydef(max_results=10, sort='sortpart'), '?max_results=10&sort=sortpart')
def test_querydef(self): self.assertEqual(querydef(max_results=10), "?max_results=10") self.assertEqual(querydef(page=10), "?page=10") self.assertEqual(querydef(where="wherepart"), "?where=wherepart") self.assertEqual(querydef(sort="sortpart"), "?sort=sortpart") self.assertEqual( querydef(where="wherepart", sort="sortpart"), "?where=wherepart&sort=sortpart", ) self.assertEqual(querydef(max_results=10, sort="sortpart"), "?max_results=10&sort=sortpart")
def _pagination_links(resource, req, documents_count, document_id=None): """ 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. :param document_id: the document id (used for versions). Defaults to None. .. versionchanged:: 0.5 Create pagination links given a document ID to allow paginated versions pages (#475). Pagination links reflect current query. (#464) .. versionchanged:: 0.4 HATOEAS link for contains the business unit value even when regexes have been configured for the resource endpoint. .. 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 """ version = None if config.DOMAIN[resource]['versioning'] is True: version = request.args.get(config.VERSION_PARAM) other_params = _other_params(req.args) # construct the default links q = querydef(req.max_results, req.where, req.sort, version, req.page, other_params) resource_title = config.DOMAIN[resource]['resource_title'] _links = {'parent': home_link(), 'self': {'title': resource_title, 'href': resource_link()}} # change links if document ID is given if document_id: _links['self'] = document_link(resource, document_id) _links['collection'] = {'title': resource_title, 'href': '%s%s' % (resource_link(), q)} # make more specific links for versioned requests if version in ('all', 'diffs'): _links['parent'] = {'title': resource_title, 'href': resource_link()} _links['collection'] = document_link(resource, document_id) elif version: _links['parent'] = document_link(resource, document_id) _links['collection'] = {'title': resource_title, 'href': '%s?version=all' % _links['parent']['href']} # modify the self link to add query params or version number if documents_count: _links['self']['href'] = '%s%s' % (_links['self']['href'], q) elif not documents_count and version and version not in ('all', 'diffs'): _links['self'] = document_link(resource, document_id, version) # create pagination links if documents_count and config.DOMAIN[resource]['pagination']: # strip any queries from the self link if present _pagination_link = _links['self']['href'].split('?')[0] if req.page * req.max_results < documents_count: q = querydef(req.max_results, req.where, req.sort, version, req.page + 1, other_params) _links['next'] = {'title': 'next page', 'href': '%s%s' % (_pagination_link, 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, version, last_page, other_params) _links['last'] = {'title': 'last page', 'href': '%s%s' % (_pagination_link, q)} if req.page > 1: q = querydef(req.max_results, req.where, req.sort, version, req.page - 1, other_params) _links['prev'] = {'title': 'previous page', 'href': '%s%s' % (_pagination_link, q)} return _links
def _pagination_links(resource, req, documents_count, document_id=None): """ 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. :param document_id: the document id (used for versions). Defaults to None. .. versionchanged:: 0.5 Create pagination links given a document ID to allow paginated versions pages (#475). Pagination links reflect current query. (#464) .. versionchanged:: 0.4 HATOEAS link for contains the business unit value even when regexes have been configured for the resource endpoint. .. 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 """ version = None if config.DOMAIN[resource]['versioning'] is True: version = request.args.get(config.VERSION_PARAM) # construct the default links q = querydef(req.max_results, req.where, req.sort, version, req.page) resource_title = config.DOMAIN[resource]['resource_title'] _links = {'parent': home_link(), 'self': {'title': resource_title, 'href': resource_link()}} # change links if document ID is given if document_id: _links['self'] = document_link(resource, document_id) _links['collection'] = {'title': resource_title, 'href': '%s%s' % (resource_link(), q)} # make more specific links for versioned requests if version in ('all', 'diffs'): _links['parent'] = {'title': resource_title, 'href': resource_link()} _links['collection'] = document_link(resource, document_id) elif version: _links['parent'] = document_link(resource, document_id) _links['collection'] = {'title': resource_title, 'href': '%s?version=all' % _links['parent']['href']} # modify the self link to add query params or version number if documents_count: _links['self']['href'] = '%s%s' % (_links['self']['href'], q) elif not documents_count and version and version not in ('all', 'diffs'): _links['self'] = document_link(resource, document_id, version) # create pagination links if documents_count and config.DOMAIN[resource]['pagination']: # strip any queries from the self link if present _pagination_link = _links['self']['href'].split('?')[0] if req.page * req.max_results < documents_count: q = querydef(req.max_results, req.where, req.sort, version, req.page + 1) _links['next'] = {'title': 'next page', 'href': '%s%s' % (_pagination_link, 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, version, last_page) _links['last'] = {'title': 'last page', 'href': '%s%s' % (_pagination_link, q)} if req.page > 1: q = querydef(req.max_results, req.where, req.sort, version, req.page - 1) _links['prev'] = {'title': 'previous page', 'href': '%s%s' % (_pagination_link, q)} return _links
def _pagination_links(resource, req, document_count, document_id=None): """ 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. :param document_id: the document id (used for versions). Defaults to None. .. versionchanged:: 0.5 Create pagination links given a document ID to allow paginated versions pages (#475). Pagination links reflect current query. (#464) .. versionchanged:: 0.4 HATEOAS link for contains the business unit value even when regexes have been configured for the resource endpoint. .. 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 """ version = None if config.DOMAIN[resource]["versioning"] is True: version = request.args.get(config.VERSION_PARAM) other_params = _other_params(req.args) # construct the default links q = querydef(req.max_results, req.where, req.sort, version, req.page, other_params) resource_title = config.DOMAIN[resource]["resource_title"] _links = { "parent": home_link(), "self": {"title": resource_title, "href": resource_link()}, } # change links if document ID is given if document_id: _links["self"] = document_link(resource, document_id) _links["collection"] = { "title": resource_title, "href": "%s%s" % (resource_link(), q), } # make more specific links for versioned requests if version in ("all", "diffs"): _links["parent"] = {"title": resource_title, "href": resource_link()} _links["collection"] = document_link(resource, document_id) elif version: _links["parent"] = document_link(resource, document_id) _links["collection"] = { "title": resource_title, "href": "%s?version=all" % _links["parent"]["href"], } # modify the self link to add query params or version number if document_count: _links["self"]["href"] = "%s%s" % (_links["self"]["href"], q) elif not document_count and version and version not in ("all", "diffs"): _links["self"] = document_link(resource, document_id, version) # create pagination links if config.DOMAIN[resource]["pagination"]: # strip any queries from the self link if present _pagination_link = _links["self"]["href"].split("?")[0] if ( req.page * req.max_results < (document_count or 0) or config.OPTIMIZE_PAGINATION_FOR_SPEED ): q = querydef( req.max_results, req.where, req.sort, version, req.page + 1, other_params, ) _links["next"] = { "title": "next page", "href": "%s%s" % (_pagination_link, q), } if document_count: last_page = int(math.ceil(document_count / float(req.max_results))) q = querydef( req.max_results, req.where, req.sort, version, last_page, other_params, ) _links["last"] = { "title": "last page", "href": "%s%s" % (_pagination_link, q), } if req.page > 1: q = querydef( req.max_results, req.where, req.sort, version, req.page - 1, other_params, ) _links["prev"] = { "title": "previous page", "href": "%s%s" % (_pagination_link, q), } return _links