def get_published_posts_page(self, page=1, perpage=10, category=None): cid = None if category: real_category = self.category_repo.find_by_slug(category) if not real_category: return Paginator([], 0, page, perpage, '/category/' + category) cid = real_category.cid total = self.post_repo.category_count(cid) pages = self.post_repo.get_published_posts(page, perpage, cid) url = 'category/' + category if category else '/posts' pagination = Paginator(pages, total, page, perpage, url) return total, pagination
def page(self, status, page=1, perpage=10): total = self.page_repo.count(status) pages = self.page_repo.paginate(page, perpage, status) if status: url = '/admin/page/status/' + status else: url = '/admin/page' pagination = Paginator(pages, total, page, perpage, url) return pagination
class PaginatorTest(unittest.TestCase): def setUp(self): result = [_ for _ in range(1, 10)] self.p = Paginator(result, 100, 10, 5, '/test') def test_next_link(self): self.assertEqual(str(self.p.next_link('next')), '<a href="/test/11">next</a>') self.p.page = 22 self.assertEqual(str(self.p.next_link('next')), '') def test_pre_link(self): self.assertEqual(str(self.p.pre_link('pre')), '<a href="/test/9">pre</a>') self.p.page = 1 self.assertEqual(str(self.p.pre_link('pre')), '') def test_iter(self): for _ in range(1, 9): next(self.p) self.assertEqual(next(self.p), 9) self.assertEqual(self.p._index, 9) self.assertRaises(StopIteration, lambda: next(self.p)) def test_len(self): self.assertEqual(9, len(self.p)) def test_links(self): self.assertTrue(self.p.links().startswith( '<a href="/test">First</a><a href="/test/9">Previous</a><a href="/test/7">7</a>' )) self.p.page = 0 self.assertEqual( self.p.links(), '<a href="/test/1">1</a><a href="/test/2">2</a><a href="/test/3">3</a><a href="/test/4">4</a><a href="/test/1">Next</a> <a href="/test/20">Last</a>' ) self.p.page = 22 self.assertEqual( self.p.links(), '<a href="/test">First</a><a href="/test/21">Previous</a><a href="/test/19">19</a><a href="/test/20">20</a>' )
class PaginatorTest(unittest.TestCase): def setUp(self): result = [_ for _ in range(1, 10)] self.p = Paginator(result, 100, 10, 5, '/test') def test_next_link(self): self.assertEqual(str(self.p.next_link('next')), '<a href="/test/11">next</a>') self.p.page = 22 self.assertEqual(str(self.p.next_link('next')), '') def test_pre_link(self): self.assertEqual(str(self.p.pre_link('pre')), '<a href="/test/9">pre</a>') self.p.page = 1 self.assertEqual(str(self.p.pre_link('pre')), '') def test_iter(self): for _ in range(1, 9): next(self.p) self.assertEqual(next(self.p), 9) self.assertEqual(self.p._index, 9) self.assertRaises(StopIteration, lambda: next(self.p)) def test_len(self): self.assertEqual(9, len(self.p)) def test_links(self): self.assertTrue(self.p.links().startswith( '<a href="/test">First</a><a href="/test/9">Previous</a><a href="/test/7">7</a>')) self.p.page = 0 self.assertEqual( self.p.links(), '<a href="/test/1">1</a><a href="/test/2">2</a><a href="/test/3">3</a><a href="/test/4">4</a><a href="/test/1">Next</a> <a href="/test/20">Last</a>') self.p.page = 22 self.assertEqual( self.p.links(), '<a href="/test">First</a><a href="/test/21">Previous</a><a href="/test/19">19</a><a href="/test/20">20</a>')
def page(self, page=1, perpage=10, category=None): total = self.post_repo.count(category) pages = self.post_repo.paginate(page, perpage, category) pagination = Paginator(pages, total, page, perpage, '/admin/post') return pagination
def lists(self, page=1, perpage=10): total = self.post_repo.count() pages = self.post_repo.paginate(page, perpage) pagination = Paginator(pages, total, page, perpage, '/posts') return total, pagination
def search(self, key, page, perpage=10): pages = self.post_repo.search(key, page, perpage) total = self.post_repo.serach_count(key) pagination = Paginator(pages, total, page, perpage, '/admin/post') return pagination
def setUp(self): result = [_ for _ in range(1, 10)] self.p = Paginator(result, 100, 10, 5, '/test')
def page(self, page=1, perpage=10): total = self.category_repo.count() pages = self.category_repo.paginate(page, perpage) pagination = Paginator(pages, total, page, perpage, '/admin/category') return pagination
def page(self, status, page=1, perpage=10): total = self.comment_repo.count() pages = self.comment_repo.paginate(page, perpage, status) pagination = Paginator(pages, total, page, perpage, '/admin/comment') return pagination
def get_user_page(self, user): return Paginator([user], 1, 1, 5, '/admin/user')
def page(self, page, perpage=5): total = self.repo.count() users = self.repo.take(page, perpage) page = Paginator(users, total, page, perpage, '/admin/user') return page
def field_page(self, page=1, perpage=10): total = self.extend_repo.count() pages = self.extend_repo.paginate(page, perpage) pagination = Paginator(pages, total, page, perpage, '/admin/extend/field') return pagination