Fix pagination of notifications

Pagination of notifications are corrupted by this change:
I112725c0e2ef6ceceb2d7be31ed36defa9d77d50
And this patch also fixes page count.

Change-Id: I56581d62480f05bd9c1a91b3d37911e720a82f40
This commit is contained in:
Shinya Kawabata 2017-01-24 20:11:25 +09:00
parent 6463b258fd
commit f7b02ef4b7

View File

@ -20,6 +20,7 @@ from django.utils.translation import ugettext as _ # noqa
from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon.utils import functions as utils
from monitoring.notifications import constants
from monitoring.notifications import forms as notification_forms
@ -28,7 +29,6 @@ from monitoring import api
from openstack_dashboard import policy
LIMIT = 10
PREV_PAGE_LIMIT = 100
@ -44,9 +44,10 @@ class IndexView(tables.DataTableView):
results = []
if page_offset is None:
page_offset = 0
limit = utils.get_page_size(self.request)
try:
results = api.monitor.notification_list(self.request, page_offset, LIMIT)
paginator = Paginator(results, LIMIT)
results = api.monitor.notification_list(self.request, page_offset, limit)
paginator = Paginator(results, limit)
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
@ -70,12 +71,16 @@ class IndexView(tables.DataTableView):
if page_offset is None:
page_offset = 0
prev_page_stack = []
else:
page_offset = int(page_offset)
limit = utils.get_page_size(self.request)
try:
# To judge whether there is next page, get LIMIT + 1
results = api.monitor.notification_list(self.request, page_offset,
LIMIT + 1)
limit + 1)
num_results = len(results)
paginator = Paginator(results, LIMIT)
paginator = Paginator(results, limit)
contacts = paginator.page(1)
except EmptyPage:
contacts = paginator.page(paginator.num_pages)
@ -85,10 +90,10 @@ class IndexView(tables.DataTableView):
context["contacts"] = contacts
if num_results < LIMIT + 1:
if num_results < limit + 1:
context["page_offset"] = None
else:
context["page_offset"] = contacts.object_list[-1]["id"]
context["page_offset"] = page_offset + limit
if page_offset in prev_page_stack:
index = prev_page_stack.index(page_offset)
@ -100,7 +105,7 @@ class IndexView(tables.DataTableView):
if len(prev_page_stack) > PREV_PAGE_LIMIT:
del prev_page_stack[0]
prev_page_stack.append(str(page_offset))
prev_page_stack.append(page_offset)
self.request.session['prev_page_stack'] = prev_page_stack
return context