4 from sets import Set as set
6 from django import template
7 from django.http import Http404
8 from django.core.paginator import Paginator, InvalidPage
9 from django.conf import settings
11 register = template.Library()
13 DEFAULT_PAGINATION = getattr(settings, 'PAGINATION_DEFAULT_PAGINATION', 20)
14 DEFAULT_WINDOW = getattr(settings, 'PAGINATION_DEFAULT_WINDOW', 4)
15 DEFAULT_ORPHANS = getattr(settings, 'PAGINATION_DEFAULT_ORPHANS', 0)
16 INVALID_PAGE_RAISES_404 = getattr(settings,
17 'PAGINATION_INVALID_PAGE_RAISES_404', False)
18 DISPLAY_PAGE_LINKS = getattr(settings, 'PAGINATION_DISPLAY_PAGE_LINKS', True)
19 PREVIOUS_LINK_DECORATOR = getattr(settings, 'PAGINATION_PREVIOUS_LINK_DECORATOR', "‹‹ ")
20 NEXT_LINK_DECORATOR = getattr(settings, 'PAGINATION_NEXT_LINK_DECORATOR', " ››")
21 DISPLAY_DISABLED_PREVIOUS_LINK = getattr(settings, 'PAGINATION_DISPLAY_DISABLED_PREVIOUS_LINK', True)
22 DISPLAY_DISABLED_NEXT_LINK = getattr(settings, 'PAGINATION_DISPLAY_DISABLED_NEXT_LINK', True)
24 def do_autopaginate(parser, token):
26 Splits the arguments to the autopaginate tag and formats them correctly.
28 split = token.split_contents()
31 for i, bit in enumerate(split):
35 if as_index is not None:
37 context_var = split[as_index + 1]
39 raise template.TemplateSyntaxError("Context variable assignment " +
40 "must take the form of {%% %r object.example_set.all ... as " +
41 "context_var_name %%}" % split[0])
42 del split[as_index:as_index + 2]
44 return AutoPaginateNode(split[1])
46 return AutoPaginateNode(split[1], paginate_by=split[2],
47 context_var=context_var)
50 orphans = int(split[3])
52 raise template.TemplateSyntaxError(u'Got %s, but expected integer.'
54 return AutoPaginateNode(split[1], paginate_by=split[2], orphans=orphans,
55 context_var=context_var)
57 raise template.TemplateSyntaxError('%r tag takes one required ' +
58 'argument and one optional argument' % split[0])
60 class AutoPaginateNode(template.Node):
62 Emits the required objects to allow for Digg-style pagination.
64 First, it looks in the current context for the variable specified, and using
65 that object, it emits a simple ``Paginator`` and the current page object
66 into the context names ``paginator`` and ``page_obj``, respectively.
68 It will then replace the variable specified with only the objects for the
73 It is recommended to use *{% paginate %}* after using the autopaginate
74 tag. If you choose not to use *{% paginate %}*, make sure to display the
75 list of available pages, or else the application may seem to be buggy.
77 def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION,
78 orphans=DEFAULT_ORPHANS, context_var=None):
79 self.queryset_var = template.Variable(queryset_var)
80 if isinstance(paginate_by, int):
81 self.paginate_by = paginate_by
83 self.paginate_by = template.Variable(paginate_by)
84 self.orphans = orphans
85 self.context_var = context_var
87 def render(self, context):
88 key = self.queryset_var.var
89 value = self.queryset_var.resolve(context)
90 if isinstance(self.paginate_by, int):
91 paginate_by = self.paginate_by
93 paginate_by = self.paginate_by.resolve(context)
94 paginator = Paginator(value, paginate_by, self.orphans)
96 page_obj = paginator.page(context['request'].page)
98 if INVALID_PAGE_RAISES_404:
99 raise Http404('Invalid page requested. If DEBUG were set to ' +
100 'False, an HTTP 404 page would have been shown instead.')
102 context['invalid_page'] = True
104 if self.context_var is not None:
105 context[self.context_var] = page_obj.object_list
107 context[key] = page_obj.object_list
108 context['paginator'] = paginator
109 context['page_obj'] = page_obj
113 def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
115 Renders the ``pagination/pagination.html`` template, resulting in a
116 Digg-like display of the available pages, given the current page. If there
117 are too many pages to be displayed before and after the current page, then
118 elipses will be used to indicate the undisplayed gap between page numbers.
120 Requires one argument, ``context``, which should be a dictionary-like data
121 structure and must contain the following keys:
124 A ``Paginator`` or ``QuerySetPaginator`` object.
127 This should be the result of calling the page method on the
128 aforementioned ``Paginator`` or ``QuerySetPaginator`` object, given
131 This same ``context`` dictionary-like data structure may also include:
134 A dictionary of all of the **GET** parameters in the current request.
135 This is useful to maintain certain types of state, even when requesting
139 paginator = context['paginator']
140 page_obj = context['page_obj']
141 page_range = paginator.page_range
142 # Calculate the record range in the current page for display.
143 records = {'first': 1 + (page_obj.number - 1) * paginator.per_page}
144 records['last'] = records['first'] + paginator.per_page - 1
145 if records['last'] + paginator.orphans >= paginator.count:
146 records['last'] = paginator.count
147 # First and last are simply the first *n* pages and the last *n* pages,
148 # where *n* is the current window size.
149 first = set(page_range[:window])
150 last = set(page_range[-window:])
151 # Now we look around our current page, making sure that we don't wrap
153 current_start = page_obj.number-1-window
154 if current_start < 0:
156 current_end = page_obj.number-1+window
159 current = set(page_range[current_start:current_end])
161 # If there's no overlap between the first set of pages and the current
162 # set of pages, then there's a possible need for elusion.
163 if len(first.intersection(current)) == 0:
164 first_list = list(first)
166 second_list = list(current)
168 pages.extend(first_list)
169 diff = second_list[0] - first_list[-1]
170 # If there is a gap of two, between the last page of the first
171 # set and the first page of the current set, then we're missing a
174 pages.append(second_list[0] - 1)
175 # If the difference is just one, then there's nothing to be done,
176 # as the pages need no elusion and are correct.
179 # Otherwise, there's a bigger gap which needs to be signaled for
180 # elusion, by pushing a None value to the page list.
183 pages.extend(second_list)
185 unioned = list(first.union(current))
187 pages.extend(unioned)
188 # If there's no overlap between the current set of pages and the last
189 # set of pages, then there's a possible need for elusion.
190 if len(current.intersection(last)) == 0:
191 second_list = list(last)
193 diff = second_list[0] - pages[-1]
194 # If there is a gap of two, between the last page of the current
195 # set and the first page of the last set, then we're missing a
198 pages.append(second_list[0] - 1)
199 # If the difference is just one, then there's nothing to be done,
200 # as the pages need no elusion and are correct.
203 # Otherwise, there's a bigger gap which needs to be signaled for
204 # elusion, by pushing a None value to the page list.
207 pages.extend(second_list)
209 differenced = list(last.difference(current))
211 pages.extend(differenced)
213 'MEDIA_URL': settings.MEDIA_URL,
216 'page_obj': page_obj,
217 'paginator': paginator,
219 'is_paginated': paginator.count > paginator.per_page,
220 'display_page_links': DISPLAY_PAGE_LINKS,
221 'display_disabled_previous_link': DISPLAY_DISABLED_PREVIOUS_LINK,
222 'display_disabled_next_link': DISPLAY_DISABLED_NEXT_LINK,
223 'previous_link_decorator': PREVIOUS_LINK_DECORATOR,
224 'next_link_decorator': NEXT_LINK_DECORATOR,
226 if 'request' in context:
227 getvars = context['request'].GET.copy()
228 if 'page' in getvars:
230 if len(getvars.keys()) > 0:
231 to_return['getvars'] = "&%s" % getvars.urlencode()
233 to_return['getvars'] = ''
235 except KeyError, AttributeError:
238 register.inclusion_tag('pagination/pagination.html', takes_context=True)(
240 register.tag('autopaginate', do_autopaginate)