pagination.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import math
  2. from django.http import Http404
  3. def make_pagination(page, total, per):
  4. pagination = {'start': 0, 'stop': 0, 'prev':-1, 'next':-1}
  5. page = int(page)
  6. if page == 1:
  7. # This is fugly abuse of "wrong page" handling
  8. # It's done to combat "duplicate content" errors
  9. # If page is 1 instead of 0, that suggests user came
  10. # to page from somewhere/1/ instead of somewhere/
  11. # when this happens We raise 404 to drop /1/ part from url
  12. raise Http404()
  13. if page > 0:
  14. pagination['start'] = (page - 1) * per
  15. # Set page and total stat
  16. pagination['page'] = int(pagination['start'] / per) + 1
  17. pagination['total'] = int(math.ceil(total / float(per)))
  18. # Fix too large offset
  19. if pagination['start'] > total:
  20. pagination['start'] = 0
  21. # Allow prev/next?
  22. if total > per:
  23. if pagination['page'] > 1:
  24. pagination['prev'] = pagination['page'] - 1
  25. if pagination['page'] < pagination['total']:
  26. pagination['next'] = pagination['page'] + 1
  27. # Fix empty pagers
  28. if not pagination['total']:
  29. pagination['total'] = 1
  30. # Set stop offset
  31. pagination['stop'] = pagination['start'] + per
  32. # Put 1/5 of last page on current page...
  33. if pagination['page'] + 1 == pagination['total']:
  34. last_page = per + total - (pagination['total'] * per)
  35. cutoff = int(per / 5)
  36. if cutoff > 1 and last_page < cutoff:
  37. pagination['stop'] += last_page
  38. pagination['total'] -= 1
  39. pagination['next'] = -1
  40. # Raise 404 if page is out of range
  41. if pagination['page'] > pagination['total']:
  42. raise Http404()
  43. # Return complete pager
  44. return pagination
  45. def page_number(item, total, per):
  46. item += 1
  47. page_item = int(item / per) + 1
  48. pages_total = int(math.ceil(total / float(per)))
  49. last_page = total - ((pages_total - 1) * per)
  50. cutoff = int(per / 5)
  51. if cutoff > 1 and cutoff > last_page and item > (total - last_page):
  52. page_item -= 1
  53. return page_item