From cab2d8765bf703ee16b2abadbda8c8cfcd93ca3c Mon Sep 17 00:00:00 2001 From: joetsoi Date: Sun, 11 Aug 2013 16:06:14 +0100 Subject: [PATCH 1/3] [#1179] add offset/limit to package_list action --- ckan/logic/action/get.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index c0fe9639764..6e1955ef4a6 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -72,6 +72,12 @@ def package_list(context, data_dict): _check_access('package_list', context, data_dict) + schema = context.get('schema', logic.schema.default_pagination_schema()) + data_dict, errors = _validate(data_dict, schema, context) + if errors: + raise ValidationError(errors) + + package_revision_table = model.package_revision_table col = (package_revision_table.c.id if api == 2 else package_revision_table.c.name) @@ -79,6 +85,14 @@ def package_list(context, data_dict): query = query.where(_and_(package_revision_table.c.state=='active', package_revision_table.c.current==True)) query = query.order_by(col) + + limit = data_dict.get('limit') + if limit: + query = query.limit(limit) + + offset = data_dict.get('offset') + if offset: + query = query.offset(offset) return list(zip(*query.execute())[0]) def current_package_list_with_resources(context, data_dict): From b2387881834dbd2780420b6c4979a7c041511f83 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Tue, 13 Aug 2013 12:41:25 +0200 Subject: [PATCH 2/3] [#1179] Add test for limit in package_list --- ckan/tests/logic/test_action.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ckan/tests/logic/test_action.py b/ckan/tests/logic/test_action.py index 73a3b838f0c..860eb6245e7 100644 --- a/ckan/tests/logic/test_action.py +++ b/ckan/tests/logic/test_action.py @@ -60,8 +60,8 @@ def _add_basic_package(self, package_name=u'test_package', **kwargs): return json.loads(res.body)['result'] def test_01_package_list(self): - postparams = '%s=1' % json.dumps({}) - res = json.loads(self.app.post('/api/action/package_list', params=postparams).body) + res = json.loads(self.app.post('/api/action/package_list', + headers={'content-type': 'application/json'}).body) assert res['success'] is True assert len(res['result']) == 2 assert 'warandpeace' in res['result'] @@ -69,6 +69,13 @@ def test_01_package_list(self): assert res['help'].startswith( "Return a list of the names of the site's datasets (packages).") + postparams = '%s=1' % json.dumps({'limit': 1}) + res = json.loads(self.app.post('/api/action/package_list', + params=postparams).body) + assert res['success'] is True + assert len(res['result']) == 1 + assert 'warandpeace' in res['result'] or 'annakarenina' in res['result'] + # Test GET request res = json.loads(self.app.get('/api/action/package_list').body) assert len(res['result']) == 2 From 7c9509474c2a2fefd6c3d7692f2164d26654b6b4 Mon Sep 17 00:00:00 2001 From: joetsoi Date: Tue, 13 Aug 2013 15:02:55 +0100 Subject: [PATCH 3/3] [#1179] doc strings for package_list --- ckan/logic/action/get.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index 6e1955ef4a6..c9ff5224d9a 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -64,6 +64,13 @@ def site_read(context,data_dict=None): def package_list(context, data_dict): '''Return a list of the names of the site's datasets (packages). + :param limit: if given, the list of datasets will be broken into pages of + at most ``limit`` datasets per page and only one page will be returned + at a time (optional) + :type limit: int + :param offset: when ``limit`` is given, the offset to start returning packages from + :type offset: int + :rtype: list of strings '''