From 7dac1c05cd40f89a5af34763e4d5614c750575c2 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 13 Nov 2017 12:34:56 -0800 Subject: [PATCH] Improved pagination Closes #78 --- datasette/app.py | 20 ++++++++++---------- datasette/templates/table.html | 4 ++-- tests/test_app.py | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 1ddc9e67..e94c7442 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -381,17 +381,17 @@ class TableView(BaseView): where_clauses = [] params = {} - after = special_args.get('_after') - if after: + next = special_args.get('_next') + if next: if use_rowid: where_clauses.append( 'rowid > :p{}'.format( len(params), ) ) - params['p{}'.format(len(params))] = after + params['p{}'.format(len(params))] = next else: - pk_values = compound_pks_from_path(after) + pk_values = compound_pks_from_path(next) if len(pk_values) == len(pks): param_counter = len(params) for pk, value in zip(pks, pk_values): @@ -423,11 +423,11 @@ class TableView(BaseView): rows = list(rows) info = self.ds.inspect() table_rows = info[name]['tables'].get(table) - after = None - after_link = None + next = None + next_url = None if len(rows) > self.page_size: - after = path_from_row_pks(rows[-2], pks, use_rowid) - after_link = path_with_added_args(request, {'_after': after}) + next = path_from_row_pks(rows[-2], pks, use_rowid) + next_url = urllib.parse.urljoin(request.url, path_with_added_args(request, {'_next': next})) return { 'database': name, 'table': table, @@ -443,13 +443,13 @@ class TableView(BaseView): 'sql': sql, 'params': params, }, - 'after': after, + 'next': next, + 'next_url': next_url, }, lambda: { 'database_hash': hash, 'use_rowid': use_rowid, 'row_link': lambda row: path_from_row_pks(row, pks, use_rowid), 'display_columns': display_columns, - 'after_link': after_link, } diff --git a/datasette/templates/table.html b/datasette/templates/table.html index 95463d1c..f1539ee3 100644 --- a/datasette/templates/table.html +++ b/datasette/templates/table.html @@ -52,8 +52,8 @@ -{% if after_link %} -

Next page

+{% if next_url %} +

Next page

{% endif %} {% if table_definition %} diff --git a/tests/test_app.py b/tests/test_app.py index 7a711b2d..ad5b9386 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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): _, response = app_client.get( '/test_tables.jsono?sql=select+content+from+no_primary_key'