diff --git a/src/postorius/utils.py b/src/postorius/utils.py index b87cd75..37a4011 100644 --- a/src/postorius/utils.py +++ b/src/postorius/utils.py @@ -18,6 +18,7 @@ import logging from django.conf import settings +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.shortcuts import render_to_response, redirect from django.template import RequestContext from mailmanclient import Client @@ -41,3 +42,17 @@ {'error': "Mailman REST API not available. " "Please start Mailman core."}, context_instance=RequestContext(request)) + +def paginate(request, collection, count=20): + # count is the number of items per page + paginator = Paginator(collection, count) + page = request.GET.get('page') + try: + results = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + results = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + results = paginator.page(paginator.num_pages) + return results diff --git a/src/postorius/views/list.py b/src/postorius/views/list.py index de97723..ec65272 100644 --- a/src/postorius/views/list.py +++ b/src/postorius/views/list.py @@ -30,7 +30,6 @@ from django.core.exceptions import ValidationError from django.utils.decorators import method_decorator from django.utils.translation import gettext as _ -from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger try: from urllib2 import HTTPError except ImportError: @@ -395,17 +394,7 @@ @method_decorator(list_moderator_required) def get(self, request, *args, **kwargs): - # Paginate - paginator = Paginator(self.mailing_list.held, 20) # Show 20 messages per page - page = request.GET.get('page') - try: - held_messages = paginator.page(page) - except PageNotAnInteger: - # If page is not an integer, deliver first page. - held_messages = paginator.page(1) - except EmptyPage: - # If page is out of range (e.g. 9999), deliver last page of results. - held_messages = paginator.page(paginator.num_pages) + held_messages = utils.paginate(request, self.mailing_list.held, 20) return render_to_response( 'postorius/lists/held_messages.html', { 'list': self.mailing_list, @@ -534,24 +523,11 @@ logger.debug(lists) except MailmanApiError: return utils.render_api_error(request) - - # Paginate - paginator = Paginator(lists, 15) # Show 15 lists per page - page = request.GET.get('page') - try: - lists = paginator.page(page) - except PageNotAnInteger: - # If page is not an integer, deliver first page. - lists = paginator.page(1) - except EmptyPage: - # If page is out of range (e.g. 9999), deliver last page of results. - lists = paginator.page(paginator.num_pages) - choosable_domains = _get_choosable_domains(request) return render_to_response( template, { 'error': error, - 'lists': lists, + 'lists': utils.paginate(request, lists, 15), 'domain_count': len(choosable_domains), }, context_instance=RequestContext(request))