def test_onepage_large_current(self): segment, current, next, last = util.paginate(QuerySetMock(range(97)), page='current', per_page=100) self.assertEqual(segment, range(97)) self.assertEqual(current, 1) self.assertEqual(next, None) self.assertEqual(last, 1)
def test_multipage_current(self): segment, current, next, last = util.paginate(QuerySetMock(range(15)), page='current', per_page=10) self.assertEqual(segment, range(5, 15)) self.assertEqual(current, 2) self.assertEqual(next, None) self.assertEqual(last, 2)
def test_onefullpage_onlyonepage(self): segment, current, next, last = util.paginate(QuerySetMock(range(10)), page=1, per_page=10) self.assertEqual(segment, range(10)) self.assertEqual(current, 1) self.assertEqual(next, None) self.assertEqual(last, 1)
def test_multipage_toohigh(self): segment, current, next, last = util.paginate(QuerySetMock(range(25)), page=4, per_page=10) self.assertEqual(segment, range(20, 25)) self.assertEqual(current, 3) self.assertEqual(next, None) self.assertEqual(last, 3)
def test_multipage_middlepage(self): segment, current, next, last = util.paginate(QuerySetMock(range(25)), page=2, per_page=10) self.assertEqual(segment, range(10, 20)) self.assertEqual(current, 2) self.assertEqual(next, 3) self.assertEqual(last, 3)
def test_handles_string_page(self): segment, current, next, last = util.paginate(QuerySetMock(range(30)), page='2', per_page=10) self.assertEqual(segment, range(10, 20)) self.assertEqual(current, 2) self.assertEqual(next, 3) self.assertEqual(last, 3)
def test_empty_iterable(self): segment, current, next, last = util.paginate(QuerySetMock([]), page=1, per_page=10) self.assertEqual(segment, []) self.assertEqual(current, 1) self.assertEqual(next, None) self.assertEqual(last, 1)
def __init__(self, request, short_id, page=None, gotoreply=None): try: mapping_id = get_mapping_id_from_short_id(short_id) except ValueError: raise Http404 self.request = request self.short_id = short_id self.page = page try: self.gotoreply = int(gotoreply) except TypeError: self.gotoreply = None _op_comment = get_object_or_404(Comment.published, id=mapping_id) # Get relevant OP data. _op_content = _op_comment.reply_content op_content = _op_content.details() if _op_content else None op_comment = _op_comment.details op_category = _op_comment.category.details() if _op_comment.category else Category.ALL.details() linked_comment = op_comment _linked_comment = _op_comment reply_ids = list(_op_comment.get_replies().values_list('id', flat=True)) # Remove user-hidden comments. reply_ids = remove_hidden_comment_ids(request.user, reply_ids) # Pagination. explicit_page_view = bool(page) num_replies = len(reply_ids) if gotoreply is not None: gotoreply = int(gotoreply) try: page = page_divide(reply_ids.index(gotoreply)+1, knobs.COMMENTS_PER_PAGE) # If going to a reply on the last page, show 'current'. if reply_ids.index(gotoreply) + 1 >= num_replies - knobs.COMMENTS_PER_PAGE: page = 'current' except ValueError: page = '1' # Grab the gotoreply's content for metadata _linked_comment = Comment.objects.get_or_none(id=gotoreply) if _linked_comment: linked_comment = _linked_comment.details elif page is None: page = '1' self.page_reply_ids, self.page_current, self.page_next, self.page_last = paginate( reply_ids, page=page, per_page=knobs.COMMENTS_PER_PAGE) self.page_penultimate = self.page_last - 1 # Make it easy for Django to do a for loop on the page numbers. pages = xrange(1, self.page_last + 1) self.op_comment = op_comment self._op_comment = _op_comment self.op_content = op_content self._op_content = _op_content self.op_category = op_category self.linked_comment = linked_comment self._linked_comment = _linked_comment self.page = page self.pages = pages self.per_page = knobs.COMMENTS_PER_PAGE self.num_replies = num_replies self.reply_ids = reply_ids self.explicit_page_view = explicit_page_view self.is_author = request.user == _op_comment.author
def test_multipage_current(self): segment, current, next, last = util.paginate(QuerySetMock(range(15)), page='current', per_page=10) self.assertEqual(segment, range(5,15)) self.assertEqual(current, 2) self.assertEqual(next, None) self.assertEqual(last, 2)
def test_onepage_iterable(self): segment, current, next, last = util.paginate(QuerySetMock(['a', 'b', 'c']), page=1, per_page=10) self.assertEqual(segment, ['a', 'b', 'c']) self.assertEqual(current, 1) self.assertEqual(next, None) self.assertEqual(last, 1)