Esempio n. 1
0
    def _paginate_request(self, http_method, method, collection_key,
                          perpage=None, limit=None, params=None,
                          paginate_attr_class=None, **kwargs):
        if paginate_attr_class is None:
            paginate_attr_class = PaginateMixin

        if perpage is None:
            perpage = DEFAULT_PERPAGE

        if params is None:
            params = {}
        params.update(limit=perpage)

        resp = self._request(http_method, method, params=params, **kwargs)

        coll_keys = collection_key.split('.')
        if not nested_in(resp, coll_keys) and resp.get('total') == '0':
            nested_set(resp, coll_keys, PaginatedIterator(0, 0, iter([])))
            return resp

        attributes = paginate_attr_class(resp)

        if limit and limit < attributes.total:
            n_pages = min(attributes.total_pages, ceildiv(limit, perpage))
            pagerange = six.moves.range(2, n_pages + 1)
        else:
            pagerange = six.moves.range(2, attributes.total_pages + 1)

        def pagequery(page, http_method=http_method, method=method,
                      collection_key=collection_key, params=params,
                      kwargs=kwargs):
            params['page'] = page

            return self._request(http_method, method, params=params,
                                 collection_key=collection_key, **kwargs)

        thispage = _list_response(nested_get(resp, coll_keys))
        remaining_pages = chain.from_iterable(pagequery(page)
                                              for page in pagerange)
        iterator = chain(thispage, remaining_pages)

        nested_set(resp, coll_keys, PaginatedIterator(attributes.total_pages,
                                                      attributes.total,
                                                      iterator))

        return resp
Esempio n. 2
0
 def total_pages(self):
     return ceildiv(self.total, self.items_per_page)