Example #1
0
def get_page(queryset, order_by, per_page, start=0):
    if start < 0:
        raise InvalidPage()

    object_list = list(_slice_queryset(queryset, order_by, per_page, start))
    if start and not object_list:
        raise EmptyPage()

    next_cursor = None
    if len(object_list) > per_page:
        next_slice_first_item = object_list.pop(-1)
        attr_name = order_by.lstrip("-")
        next_cursor = getattr(next_slice_first_item, attr_name)

    return CursorPage(start, object_list, next_cursor)
    def get_context(self, page, range_gap=5):
        try:
            page = int(page)
        except (ValueError, TypeError) as exc:
            raise InvalidPage(exc)
        
        try:
            paginator = self.page(page)
        except EmptyPage:
            return {
                'EMPTY_PAGE': True,
            }
        
        if page > 5:
            start = page-range_gap
        else:
            start = 1

        if page < self.num_pages-range_gap:
            end = page+range_gap+1
        else:
            end = self.num_pages+1

        context = {
            'page_range': list(range(start, end)),
            'objects': paginator.object_list,
            'num_pages': self.num_pages,
            'page': page,
            'has_pages': self.num_pages > 1,
            'has_previous': paginator.has_previous(),
            'has_next': paginator.has_next(),
            'previous_page': paginator.previous_page_number() if paginator.has_previous() else None,
            'next_page': paginator.next_page_number() if paginator.has_next() else None,
            'is_first': page == 1,
            'is_last': page == self.num_pages,
        }
        
        return context