+ self.force_sort = force_sort
+ self.prefix = prefix
+
+ def preferences(self, request):
+ if request.user.is_authenticated():
+ if request.user.prop.pagination:
+ preferences = request.user.prop.pagination.get(self.id, {})
+ else:
+ preferences = {}
+ else:
+ preferences = request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
+
+ return preferences
+
+ def set_preferences(self, request, preferences):
+ if request.user.is_authenticated():
+ all_preferences = request.user.prop.pagination or {}
+ all_preferences[self.id] = preferences
+ request.user.prop.pagination = all_preferences
+ else:
+ request.session['paginator_%s%s' % (self.prefix, self.id)] = preferences
+
+ def pagesize(self, request, session_prefs=None):
+ if not session_prefs:
+ session_prefs = self.preferences(request)
+
+
+ if self.has_pagesize:
+ if request.GET.get(self.PAGESIZE, None):
+ try:
+ pagesize = int(request.GET[self.PAGESIZE])
+ except ValueError:
+ logging.error('Found invalid page size "%s", loading %s, refered by %s' % (
+ request.GET.get(self.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+ ))
+ raise Http404()
+
+ session_prefs[self.PAGESIZE] = pagesize
+ else:
+ pagesize = session_prefs.get(self.PAGESIZE, self.default_pagesize)
+
+ if not pagesize in self.pagesizes:
+ pagesize = self.default_pagesize
+ else:
+ pagesize = 30
+
+ return pagesize
+
+ def page(self, request):
+ try:
+ return int(request.GET.get(self.PAGE, "1").strip())
+ except ValueError:
+ logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
+ request.GET.get(self.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+ ))
+ raise Http404()
+
+ def sort(self, request, session_prefs=None):
+ if not session_prefs:
+ session_prefs = self.preferences(request)
+
+ sort = None
+ sticky = request.user.is_authenticated() and request.user.prop.preferences and request.user.prop.preferences.get('sticky_sorts', False)
+
+ if self.has_sort:
+ if request.GET.get(self.SORT, None):
+ sort = request.GET[self.SORT]
+
+ if sticky:
+ session_prefs[self.SORT] = sort
+ else:
+ sort = self.force_sort or (sticky and session_prefs.get(self.SORT, None)) or self.default_sort
+
+ if not sort in self.sort_methods:
+ sort = self.default_sort
+
+ return sort
+
+ def sorted(self, objects, request, session_prefs=None):
+ sort = self.sort(request, session_prefs)
+
+ if sort:
+ objects = self.sort_methods[sort].apply(objects)