UI: table pagination logic encapsulation

* following https://review.openstack.org/#/c/266826/ suggestion: API requests
  that required UI pagination will be passed via generic handler.
* At first I've written a decorator - which I didn't like,
  feel faulty by design - too many returns and over complicated blocks...

Partially implements blueprint: mistral-dashboard-executions-screen

Change-Id: Ie131bf74a74b816d2779888c6c23e7bcfa4b8f22
This commit is contained in:
Gal Margalit 2016-02-11 09:30:50 +00:00
parent e9fa1ef7e5
commit 40222407bf
4 changed files with 23 additions and 20 deletions

View File

@ -46,19 +46,14 @@ def mistralclient(request):
)
def execution_create(request, **data):
"""Creates new execution."""
return mistralclient(request).executions.create(**data)
@handle_errors(_("Unable to retrieve executions."), [])
def execution_list(request, marker='', sort_keys='',
sort_dirs='', paginate=False):
"""Retrieve a listing of executions.
@handle_errors(_("Unable to retrieve list"), [])
def pagination_list(entity, request, marker='', sort_keys='',
sort_dirs='', paginate=False):
"""Retrieve a listing of specific entity and handles pagination.
:param entity: Requested entity (String)
:param request: Request data
:param marker: Pagination marker for large data sets: execution id
:param marker: Pagination marker for large data sets: entity id
:param sort_keys: Columns to sort results by. Default: created_at
:param sort_dirs: Sorting Directions (asc/desc). Default:asc
:param paginate: If true will perform pagination based on settings
@ -72,17 +67,18 @@ def execution_list(request, marker='', sort_keys='',
else:
request_size = limit
executions_iter = mistralclient(request).executions.list(
request = mistralclient(request)
entities_iter = (eval('request.%s' % entity)).list(
marker, limit, sort_keys, sort_dirs
)
has_prev_data = has_more_data = False
if paginate:
executions = list(itertools.islice(executions_iter, request_size))
entities = list(itertools.islice(entities_iter, request_size))
# first and middle page condition
if len(executions) > page_size:
executions.pop(-1)
if len(entities) > page_size:
entities.pop(-1)
has_more_data = True
# middle page condition
if marker is not None:
@ -94,9 +90,15 @@ def execution_list(request, marker='', sort_keys='',
elif marker is not None:
has_prev_data = True
else:
executions = list(executions_iter)
entities = list(entities_iter)
return executions, has_more_data, has_prev_data
return entities, has_more_data, has_prev_data
def execution_create(request, **data):
"""Creates new execution."""
return mistralclient(request).executions.create(**data)
def execution_get(request, execution_id):

View File

@ -34,7 +34,7 @@
</p>
<hr />
<p>
<strong class="block">
<strong>
{% trans "Please note" %}:
</strong>
<br/>

View File

@ -26,7 +26,7 @@ class ExecutionsTest(test.TestCase):
def test_index(self):
with contextlib.nested(
mock.patch('mistraldashboard.api.execution_list',
mock.patch('mistraldashboard.api.pagination_list',
return_value=self.mistralclient_executions.list()),):
res = self.client.get(INDEX_URL)

View File

@ -98,7 +98,8 @@ class IndexView(tables.DataTableView):
)
try:
executions, self._more, self._prev = api.execution_list(
executions, self._more, self._prev = api.pagination_list(
entity="executions",
request=self.request,
marker=marker,
sort_dirs=sort_dir,