pagination.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import urllib
  17. import copy
  18. from math import ceil, floor
  19. from itertools import count
  20. from werkzeug.datastructures import MultiDict
  21. from six.moves import zip
  22. PAGINATION_DEFAULT_PER_PAGE = 30
  23. class Pagination(object):
  24. """
  25. Pagination class for database queries.
  26. Initialization through __init__(self, cursor, page=1, per_page=2),
  27. get actual data slice through __call__().
  28. """
  29. def __init__(self, page, cursor, per_page=PAGINATION_DEFAULT_PER_PAGE,
  30. jump_to_id=False):
  31. """
  32. Initializes Pagination
  33. Args:
  34. - page: requested page
  35. - per_page: number of objects per page
  36. - cursor: db cursor
  37. - jump_to_id: object id, sets the page to the page containing the
  38. object with id == jump_to_id.
  39. """
  40. self.page = page
  41. self.per_page = per_page
  42. self.cursor = cursor
  43. self.total_count = self.cursor.count()
  44. self.active_id = None
  45. if jump_to_id:
  46. cursor = copy.copy(self.cursor)
  47. for (doc, increment) in list(zip(cursor, count(0))):
  48. if doc.id == jump_to_id:
  49. self.page = 1 + int(floor(increment / self.per_page))
  50. self.active_id = jump_to_id
  51. break
  52. def __call__(self):
  53. """
  54. Returns slice of objects for the requested page
  55. """
  56. # TODO, return None for out of index so templates can
  57. # distinguish between empty galleries and out-of-bound pages???
  58. return self.cursor.slice(
  59. (self.page - 1) * self.per_page,
  60. self.page * self.per_page)
  61. @property
  62. def pages(self):
  63. return int(ceil(self.total_count / float(self.per_page)))
  64. @property
  65. def has_prev(self):
  66. return self.page > 1
  67. @property
  68. def has_next(self):
  69. return self.page < self.pages
  70. def iter_pages(self, left_edge=2, left_current=2,
  71. right_current=5, right_edge=2):
  72. last = 0
  73. for num in xrange(1, self.pages + 1):
  74. if num <= left_edge or \
  75. (num > self.page - left_current - 1 and \
  76. num < self.page + right_current) or \
  77. num > self.pages - right_edge:
  78. if last + 1 != num:
  79. yield None
  80. yield num
  81. last = num
  82. def get_page_url_explicit(self, base_url, get_params, page_no):
  83. """
  84. Get a page url by adding a page= parameter to the base url
  85. """
  86. if isinstance(get_params, MultiDict):
  87. new_get_params = get_params.to_dict()
  88. else:
  89. new_get_params = dict(get_params) or {}
  90. new_get_params['page'] = page_no
  91. return "%s?%s" % (
  92. base_url, urllib.urlencode(new_get_params))
  93. def get_page_url(self, request, page_no):
  94. """
  95. Get a new page url based of the request, and the new page number.
  96. This is a nice wrapper around get_page_url_explicit()
  97. """
  98. return self.get_page_url_explicit(
  99. request.full_path, request.GET, page_no)