Skip to content

Commit

Permalink
Merge pull request #1179 from okfn/1179-package_list_limit_offset
Browse files Browse the repository at this point in the history
add limit/offset to package_list action
  • Loading branch information
domoritz committed Aug 13, 2013
2 parents 78ccca6 + 4bf71bd commit c78557a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
21 changes: 21 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -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
'''
Expand All @@ -72,13 +79,27 @@ 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)
query = _select([col])
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):
Expand Down
11 changes: 9 additions & 2 deletions ckan/tests/logic/test_action.py
Expand Up @@ -60,15 +60,22 @@ 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']
assert 'annakarenina' in res['result']
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
Expand Down

0 comments on commit c78557a

Please sign in to comment.