Commits

Mikhail Korobov  committed 615c46c

Non-decorator version of paginator

  • Participants
  • Parent commits 36e6826

Comments (0)

Files changed (1)

File simplepagination/__init__.py

     """
     Class based decorator.
 
-    SimplePagination decorator must be used along with 'render_to' 
+    SimplePagination decorator must be used along with 'render_to'
     decorator from django-annoying application
     http://bitbucket.org/offline/django-annoying/wiki/Home
     """
 
-    def __init__(self, key=None, style=None, per_page=None, 
+    def __init__(self, key=None, style=None, per_page=None,
                 frame_size=None, template=None, anchor=None):
         """
-        Decorator parameters 
+        Decorator parameters
 
         key - Name of the variable with objects that we paginate.
         style - name of pagination backend.
         self.template = template or self.backend.TEMPLATE or 'paginator_%s.html' % self.style
         self.user_per_page_allowed = self.backend.USER_PER_PAGE_ALLOWED
         self.user_per_page_max = self.backend.USER_PER_PAGE_MAX
-        
+
     def __call__(self, function):
         """
         Receive decorated function and return
         """
         decorated = partial(self.decorate, function)
         return update_wrapper(decorated, self.decorate)
-    
+
     def decorate(self, function, request, *args, **kwargs):
 
-        # execute view 
+        # execute view
         output = function(request, *args, **kwargs)
-        
+
         # only try to paginate if view returned dictionary,
         # in all other cases just return view output.
         if not isinstance(output, dict):
             return output
-        
+
         params = request.GET.copy()
-        
+
         try:
             current_page = int(params.pop('page')[0])
         except (ValueError, KeyError):
             current_page = 1
-        
+
         # we dont modify self.per_page because it's decorator
         # and it initialize only once.
-        per_page = self.per_page 
+        per_page = self.per_page
 
         # per_page should change from GET parameters only if this
         # is allowed in settings or backend, also it must be lower
 
         elif 'per_page' in params:
             params.pop('per_page')
-        
-        # we will paginate value of self.key, original object will be replaced 
+
+        # we will paginate value of self.key, original object will be replaced
         # by items that should be only in current page.
         try:
             paginate_qs = output.pop(self.key)
 
         # create django built in paginator object
         paginator = Paginator(paginate_qs, per_page)
-        
+
         try:
             # check that asked page is exists
             page = paginator.page(current_page)
 
         # replace paginated items by only items we should see.
         output[self.key] = page.object_list
-        
+
         # extra data that we may need to build links
         data = {}
         data['current_page'] = current_page # active page number
 
         # your view now have extra key 'paginator' with all extra data inside.
         output['paginator'] = data
-        
+
         return output
 
-    
+
 paginate = SimplePaginator
+
+def simple_paginate(queryset, request, *args, **kwargs):
+    @paginate(*args, **kwargs)
+    def inner(request):
+        return {'object_list': queryset}
+    data = inner(request)
+    data['paginator']['object_list'] = data['object_list']
+    return data['paginator']