Skip to content

Commit

Permalink
Allow comma separated string in fields in search api
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 5, 2012
1 parent 357f022 commit e579e75
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ckanext/datastore/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,10 @@ def search_data(context, data_dict):
fields = data_dict.get('fields')

if fields:
field_ids = fields
if type(fields) in [str, unicode]:
field_ids = map(lambda x: x.strip(), fields.split(','))
else:
field_ids = fields

for field in field_ids:
if not field in all_field_ids:
Expand Down
4 changes: 2 additions & 2 deletions ckanext/datastore/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def datastore_search(context, data_dict):
:type limit: int
:param offset: offset the number of rows
:type offset: int
:param fields: ordered list of fields to return
:param fields: fields to return
(default: all fields in original order)
:type fields: list of dictionaries
:type fields: list or comma separated string
:param sort: comma separated field names with ordering
eg: "fieldname1, fieldname2 desc"
:type sort: string
Expand Down
13 changes: 13 additions & 0 deletions ckanext/datastore/tests/test_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,19 @@ def test_search_fields(self):
assert result['records'] == [{u'b\xfck': 'annakarenina'},
{u'b\xfck': 'warandpeace'}], result['records']

data = {'resource_id': self.data['resource_id'],
'fields': u'b\xfck, author'}
postparams = '%s=1' % json.dumps(data)
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/datastore_search', params=postparams,
extra_environ=auth)
res_dict = json.loads(res.body)
assert res_dict['success'] is True
result = res_dict['result']
assert result['total'] == len(self.data['records'])
assert result['records'] == [{u'b\xfck': 'annakarenina', 'author': 'tolstoy'},
{u'b\xfck': 'warandpeace', 'author': 'tolstoy'}], result['records']

def test_search_filters(self):
data = {'resource_id': self.data['resource_id'],
'filters': {u'b\xfck': 'annakarenina'}}
Expand Down

0 comments on commit e579e75

Please sign in to comment.