Improved pagination

Closes #78
This commit is contained in:
Simon Willison 2017-11-13 12:34:56 -08:00
commit 7dac1c05cd
3 changed files with 27 additions and 12 deletions

View file

@ -381,17 +381,17 @@ class TableView(BaseView):
where_clauses = [] where_clauses = []
params = {} params = {}
after = special_args.get('_after') next = special_args.get('_next')
if after: if next:
if use_rowid: if use_rowid:
where_clauses.append( where_clauses.append(
'rowid > :p{}'.format( 'rowid > :p{}'.format(
len(params), len(params),
) )
) )
params['p{}'.format(len(params))] = after params['p{}'.format(len(params))] = next
else: else:
pk_values = compound_pks_from_path(after) pk_values = compound_pks_from_path(next)
if len(pk_values) == len(pks): if len(pk_values) == len(pks):
param_counter = len(params) param_counter = len(params)
for pk, value in zip(pks, pk_values): for pk, value in zip(pks, pk_values):
@ -423,11 +423,11 @@ class TableView(BaseView):
rows = list(rows) rows = list(rows)
info = self.ds.inspect() info = self.ds.inspect()
table_rows = info[name]['tables'].get(table) table_rows = info[name]['tables'].get(table)
after = None next = None
after_link = None next_url = None
if len(rows) > self.page_size: if len(rows) > self.page_size:
after = path_from_row_pks(rows[-2], pks, use_rowid) next = path_from_row_pks(rows[-2], pks, use_rowid)
after_link = path_with_added_args(request, {'_after': after}) next_url = urllib.parse.urljoin(request.url, path_with_added_args(request, {'_next': next}))
return { return {
'database': name, 'database': name,
'table': table, 'table': table,
@ -443,13 +443,13 @@ class TableView(BaseView):
'sql': sql, 'sql': sql,
'params': params, 'params': params,
}, },
'after': after, 'next': next,
'next_url': next_url,
}, lambda: { }, lambda: {
'database_hash': hash, 'database_hash': hash,
'use_rowid': use_rowid, 'use_rowid': use_rowid,
'row_link': lambda row: path_from_row_pks(row, pks, use_rowid), 'row_link': lambda row: path_from_row_pks(row, pks, use_rowid),
'display_columns': display_columns, 'display_columns': display_columns,
'after_link': after_link,
} }

View file

@ -52,8 +52,8 @@
</tbody> </tbody>
</table> </table>
{% if after_link %} {% if next_url %}
<p><a href="{{ after_link }}">Next page</a></p> <p><a href="{{ next_url }}">Next page</a></p>
{% endif %} {% endif %}
{% if table_definition %} {% if table_definition %}

View file

@ -122,6 +122,21 @@ def test_table_with_slashes_in_name(app_client):
}] }]
def test_paginate(app_client):
path = '/test_tables/no_primary_key.jsono'
fetched = []
count = 0
while path:
_, response = app_client.get(path)
count += 1
fetched.extend(response.json['rows'])
path = response.json['next_url']
if path:
assert path.endswith(response.json['next'])
assert 201 == len(fetched)
assert 5 == count
def test_max_returned_rows(app_client): def test_max_returned_rows(app_client):
_, response = app_client.get( _, response = app_client.get(
'/test_tables.jsono?sql=select+content+from+no_primary_key' '/test_tables.jsono?sql=select+content+from+no_primary_key'