Login

BetterPaginator

Author:
spankalee
Posted:
May 7, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

This paginator remembers state and generates links

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# BetterPaginator by spankalee
from django.core.paginator import ObjectPaginator, InvalidPage

class BetterPaginator(ObjectPaginator):
	def __init__(self, query_set, page_size, link_template, total='?'):
		self.current_page = 1
		self.page_size = page_size
		self.link_template = link_template
		self.total = total
		ObjectPaginator.__init__(self, query_set, page_size)
		
	def set_page(self, page_num):
		self.current_page = int(page_num)
	
	def has_next_page(self, page_num=None):
		if page_num is None:
			return ObjectPaginator.has_next_page(self, self.current_page-1)
		else:
			return ObjectPaginator.has_next_page(self,page_num)

	def has_previous_page(self, page_num=None):
		if page_num is None:
			return ObjectPaginator.has_previous_page(self, self.current_page-1)
		else:
			return ObjectPaginator.has_previous_page(self,page_num)

	def get_page(self, page_num=None):
		if page_num is None:
			return ObjectPaginator.get_page(self, self.current_page-1)
		else:
			return ObjectPaginator.get_page(self, page_num)
	
	def previous_link(self):
		if ObjectPaginator.has_previous_page(self, self.current_page-1):
			return self.link_template % (self.current_page - 1)
		else:
			return None
			
	def next_link(self):
		if ObjectPaginator.has_next_page(self, self.current_page-1):
			return self.link_template % (self.current_page + 1)
		else:
			return None
			
	def start_index(self):
		return (self.current_page-1) * self.page_size + 1
		
	def end_index(self):
		return min(self.current_page * self.page_size, self.hits)

	def make_page_links	(self, start, end):
		return [(p+1, self.link_template % (p+1), (p+1 == self.current_page)) for p in range(start, end)]

	def page_links(self):
		return self.make_page_links(0, self.pages)

	def windowed_page_links(self, window_size=10):
		links = []
		if self.pages <= 12:
			links = [self.page_links()]
		elif self.current_page - window_size/2 <= 3:
			links = [self.make_page_links(0, window_size), self.make_page_links(self.pages-2, self.pages)]
		elif self.current_page + window_size/2 > self.pages - 2:
			links = [self.make_page_links(0, 2), self.make_page_links(self.pages-window_size, self.pages)]
		else:
			links = [self.make_page_links(0, 2), 
							 self.make_page_links(self.current_page-window_size/2-1, self.current_page+window_size/2-1),
							 self.make_page_links(self.pages-2, self.pages)]
		return links

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

rtconner (on July 18, 2007):

Better than what? The builting Django paginator. I've never used either, but why is this better?

#

iapain (on August 26, 2007):

This one looks better but you have done more .. and quite less was required to make in Generic.

#

Please login first before commenting.