Skip to content

Commit

Permalink
Merge branch 'master' of github.com:okfn/ckan
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jul 3, 2012
2 parents 08cba31 + 51dd38f commit ebc0452
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
14 changes: 12 additions & 2 deletions ckan/lib/search/query.py
Expand Up @@ -150,16 +150,26 @@ def run(self, query=None, terms=[], fields={}, facet_by=[], options=None, **kwar

class TagSearchQuery(SearchQuery):
"""Search for tags."""
def run(self, query=[], fields={}, options=None, **kwargs):
def run(self, query=None, fields=None, options=None, **kwargs):
query = [] if query is None else query
fields = {} if fields is None else fields

if options is None:
options = QueryOptions(**kwargs)
else:
options.update(kwargs)

if isinstance(query, basestring):
query = [query]

query = query[:] # don't alter caller's query list.
for field, value in fields.items():
if field in ('tag', 'tags'):
query.append(value)

context = {'model': model, 'session': model.Session}
data_dict = {
'query': query,
'fields': fields,
'offset': options.get('offset'),
'limit': options.get('limit')
}
Expand Down
20 changes: 12 additions & 8 deletions ckan/logic/action/get.py
Expand Up @@ -1362,10 +1362,14 @@ def resource_search(context, data_dict):
def _tag_search(context, data_dict):
model = context['model']

query = data_dict.get('query') or data_dict.get('q')
if query:
query = query.strip()
terms = [query] if query else []
terms = data_dict.get('query') or data_dict.get('q') or []
if isinstance(terms, basestring):
terms = [terms]
terms = [ t.strip() for t in terms if t.strip() ]

if 'fields' in data_dict:
log.warning('"fields" parameter is deprecated. '
'Use the "query" parameter instead')

fields = data_dict.get('fields', {})
offset = data_dict.get('offset')
Expand Down Expand Up @@ -1410,12 +1414,12 @@ def tag_search(context, data_dict):
searched. If the ``vocabulary_id`` argument is given then only tags
belonging to that vocabulary will be searched instead.
:param query: the string to search for
:type query: string
:param query: the string(s) to search for
:type query: string or list of strings
:param vocabulary_id: the id or name of the tag vocabulary to search in
(optional)
:type vocabulary_id: string
:param fields:
:param fields: deprecated
:type fields: dictionary
:param limit: the maximum number of tags to return
:type limit: int
Expand Down Expand Up @@ -1451,7 +1455,7 @@ def tag_autocomplete(context, data_dict):
:param vocabulary_id: the id or name of the tag vocabulary to search in
(optional)
:type vocabulary_id: string
:param fields:
:param fields: deprecated
:type fields: dictionary
:param limit: the maximum number of tags to return
:type limit: int
Expand Down
5 changes: 5 additions & 0 deletions ckan/tests/lib/test_tag_search.py
Expand Up @@ -26,6 +26,11 @@ def test_good_search_query(self):
assert 'russian' in result['results'], result
assert 'tolstoy' in result['results'], result

def test_good_search_queries(self):
result = search.query_for(model.Tag).run(query=[u'ru', u's'])
assert result['count'] == 1, result
assert 'russian' in result['results'], result

def test_bad_search_query(self):
result = search.query_for(model.Tag).run(query=u'asdf')
assert result['count'] == 0, result
Expand Down
10 changes: 10 additions & 0 deletions ckan/tests/logic/test_tag.py
Expand Up @@ -202,6 +202,16 @@ def test_15a_tag_search_with_one_match(self):
assert len(tag_dicts) == 1
assert tag_dicts[0]['name'] == 'russian'

def test_15a_tag_search_with_one_match_using_fields_parameter(self):
paramd = {'fields': {'tags': 'russ'} }
params = json.dumps(paramd)
res = self.app.post('/api/action/tag_search', params=params)
assert res.json['success'] is True
assert res.json['result']['count'] == 1
tag_dicts = res.json['result']['results']
assert len(tag_dicts) == 1
assert tag_dicts[0]['name'] == 'russian'

def test_15a_tag_search_with_many_matches(self):
paramd = {'q': 'tol' }
params = json.dumps(paramd)
Expand Down
8 changes: 2 additions & 6 deletions doc/apiv3.rst
Expand Up @@ -81,17 +81,13 @@ will result in the following parameters being sent to the

This interface is *slightly* more limited than the POST interface because it
doesn't allow passing nested dicts into the action be accessed. As a
consequence of this, currently the *resource_search*, *tag_search* and
*tag_autocomplete* actions are **limited** in their functionality.
consequence of this, currently the *resource_search* action is **limited** in
its functionality when accessed with a GET request.

`resource_search`:
This action is not currently usable via a GET request as it relies upon
a nested dict of fields.

`tag_search` and `tag_autocomplete`:
The `fields` argument is not available when accessing this action with a
GET request.

Also, it is worth bearing this limitation in mind when creating your own
actions via the `IActions` interface.

Expand Down

0 comments on commit ebc0452

Please sign in to comment.