1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import math
- from django.http import Http404
- def make_pagination(page, total, per):
- pagination = {'start': 0, 'stop': 0, 'prev':-1, 'next':-1}
- page = int(page)
- if page == 1:
-
-
-
-
-
- raise Http404()
- if page > 0:
- pagination['start'] = (page - 1) * per
-
- pagination['page'] = int(pagination['start'] / per) + 1
- pagination['total'] = int(math.ceil(total / float(per)))
-
- if pagination['start'] > total:
- pagination['start'] = 0
-
- if total > per:
- if pagination['page'] > 1:
- pagination['prev'] = pagination['page'] - 1
- if pagination['page'] < pagination['total']:
- pagination['next'] = pagination['page'] + 1
-
- if not pagination['total']:
- pagination['total'] = 1
-
- pagination['stop'] = pagination['start'] + per
-
- if pagination['page'] + 1 == pagination['total']:
- last_page = per + total - (pagination['total'] * per)
- cutoff = int(per / 5)
- if cutoff > 1 and last_page < cutoff:
- pagination['stop'] += last_page
- pagination['total'] -= 1
- pagination['next'] = -1
-
- if pagination['page'] > pagination['total']:
- raise Http404()
-
- return pagination
- def page_number(item, total, per):
- page_item = int(math.ceil(item / float(per)))
- pages_total = int(math.ceil(total / float(per)))
- last_page = total - ((pages_total - 1) * per)
- cutoff = int(per / 5)
- if cutoff > 1 and cutoff > last_page and pages_total == page_item and pages_total > 1:
- page_item -= 1
- return page_item
|