Mercurial > hg > ltpdarepo
changeset 72:90c1fa07f6a7
Small tweaks to pagination API. Extend unit tests.
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Mon, 15 Aug 2011 20:05:59 +0200 |
parents | 543f68b3a8f0 |
children | 3d5850d65603 |
files | src/ltpdarepo/pagination.py src/ltpdarepo/templates/pagination.html src/ltpdarepo/tests/test_pagination.py |
diffstat | 3 files changed, 66 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ltpdarepo/pagination.py Mon Aug 15 20:05:13 2011 +0200 +++ b/src/ltpdarepo/pagination.py Mon Aug 15 20:05:59 2011 +0200 @@ -8,10 +8,6 @@ self.items = items @property - def page(self): - return self.current - - @property def pages(self): return int(ceil(self.count / float(self.size))) @@ -24,9 +20,9 @@ return self.current < self.pages @property - def slice(self): + def limits(self): start = (self.current - 1) * self.size - return slice(start, start + self.size) + return start, self.size def __iter__(self): # cache number of pages
--- a/src/ltpdarepo/templates/pagination.html Mon Aug 15 20:05:13 2011 +0200 +++ b/src/ltpdarepo/templates/pagination.html Mon Aug 15 20:05:59 2011 +0200 @@ -1,12 +1,12 @@ {% macro render(pagination) %} <div class="pagination wrapper"> {% if pagination.has_prev %} - <a class="prev" href="{{ url_for_other_page(pagination.page - 1) }}" alt="prev">«</a> + <a class="prev" href="{{ url_for_other_page(pagination.current - 1) }}" alt="prev">«</a> {% else %} <span class="prev discrete">«</span> {% endif %} - {%- for page in pagination %} - {% if page > 0%} + {% for page in pagination %} + {% if page > 0 %} {% if page != pagination.current %} <a href="{{ url_for_other_page(page) }}">{{ page }}</a> {% else %} @@ -15,9 +15,9 @@ {% else %} <span class="ellipsis">…</span> {% endif %} - {%- endfor %} + {% endfor %} {% if pagination.has_next %} - <a class="next" href="{{ url_for_other_page(pagination.page + 1) }}" alt="next">»</a> + <a class="next" href="{{ url_for_other_page(pagination.current + 1) }}" alt="next">»</a> {% else %} <span class="next discrete">»</span> {% endif %}
--- a/src/ltpdarepo/tests/test_pagination.py Mon Aug 15 20:05:13 2011 +0200 +++ b/src/ltpdarepo/tests/test_pagination.py Mon Aug 15 20:05:59 2011 +0200 @@ -9,71 +9,99 @@ def test_zero_elements(self): p = Pagination(1, 0, size) - l = list(p) - self.assertEqual(l, []) + self.assertEqual(list(p), []) + self.assertFalse(p.has_prev) + self.assertFalse(p.has_next) def test_one_page(self): p = Pagination(1, 1, size) - l = list(p) - self.assertEqual(l, [1]) + self.assertEqual(list(p), [1]) + self.assertFalse(p.has_prev) + self.assertFalse(p.has_next) p = Pagination(1, 2, size) - l = list(p) - self.assertEqual(l, [1]) + self.assertEqual(list(p), [1]) + self.assertFalse(p.has_prev) + self.assertFalse(p.has_next) def test_two_pages(self): p = Pagination(1, 18, size) - l = list(p) - self.assertEqual(l, [1, 2]) + self.assertEqual(list(p), [1, 2]) + self.assertFalse(p.has_prev) + self.assertTrue(p.has_next) + self.assertEqual(p.limits, (0, size)) p = Pagination(1, 20, size) - l = list(p) - self.assertEqual(l, [1, 2]) + self.assertEqual(list(p), [1, 2]) + self.assertFalse(p.has_prev) + self.assertTrue(p.has_next) + self.assertEqual(p.limits, (0, size)) + + p = Pagination(2, 20, size) + self.assertEqual(list(p), [1, 2]) + self.assertTrue(p.has_prev) + self.assertFalse(p.has_next) + self.assertEqual(p.limits, (10, size)) def test_pages_without_ellipsis(self): + # 7 elements pagination control should not use ellipsis up to 7 pages for i in range(1, 8): p = Pagination(1, size*i, size) - l = list(p) - self.assertEqual(l, range(1, i + 1)) + self.assertEqual(list(p), range(1, i+1)) def test_pages_with_ellipsis(self): - + # test the 7 elements pagination control with 15 pages npages = 15 nitems = size*npages - for i in range(1, 4): - p = Pagination(i, nitems, size) - l = list(p) - self.assertEqual(len(l), 7) - self.assertEqual(l, [1, 2, 3, 4, 5, -9, 15]) + # ellipsis just at the end + p = Pagination(1, nitems, size) + l = list(p) + self.assertEqual(len(l), 7) + self.assertEqual(l, [1, 2, 3, 4, 5, -9, 15]) + self.assertFalse(p.has_prev) + self.assertTrue(p.has_next) - p = Pagination(6, nitems, size) + p = Pagination(4, nitems, size) l = list(p) self.assertEqual(len(l), 7) - self.assertEqual(l, [1, -3, 5, 6, 7, -7, 15]) + self.assertEqual(l, [1, 2, 3, 4, 5, -9, 15]) + self.assertTrue(p.has_prev) + self.assertTrue(p.has_next) - p = Pagination(7, nitems, size) + # ellipsis on both sides + p = Pagination(5, nitems, size) l = list(p) self.assertEqual(len(l), 7) - self.assertEqual(l, [1, -4, 6, 7, 8, -6, 15]) + self.assertEqual(l, [1, -2, 4, 5, 6, -8, 15]) + self.assertTrue(p.has_prev) + self.assertTrue(p.has_next) p = Pagination(8, nitems, size) l = list(p) self.assertEqual(len(l), 7) self.assertEqual(l, [1, -5, 7, 8, 9, -5, 15]) + self.assertTrue(p.has_prev) + self.assertTrue(p.has_next) - p = Pagination(9, nitems, size) + p = Pagination(11, nitems, size) l = list(p) self.assertEqual(len(l), 7) - self.assertEqual(l, [1, -6, 8, 9, 10, -4, 15]) + self.assertEqual(l, [1, -8, 10, 11, 12, -2, 15]) + self.assertTrue(p.has_prev) + self.assertTrue(p.has_next) - p = Pagination(10, nitems, size) + # ellipsis just at the beginning + p = Pagination(12, nitems, size) l = list(p) self.assertEqual(len(l), 7) - self.assertEqual(l, [1, -7, 9, 10, 11, -3, 15]) + self.assertEqual(l, [1, -9, 11, 12, 13, 14, 15]) + self.assertTrue(p.has_prev) + self.assertTrue(p.has_next) - for i in range(12, 15): - p = Pagination(i, nitems, size) - l = list(p) - self.assertEqual(len(l), 7) - self.assertEqual(l, [1, -9, 11, 12, 13, 14, 15]) + p = Pagination(15, nitems, size) + l = list(p) + self.assertEqual(len(l), 7) + self.assertEqual(l, [1, -9, 11, 12, 13, 14, 15]) + self.assertTrue(p.has_prev) + self.assertFalse(p.has_next)