Skip to content

Commit

Permalink
[#1208] Use search to get datasets on group_show
Browse files Browse the repository at this point in the history
Use the search rather than query the db.

Instead of returning the whole package dict, pick the fields to match
the ones currently returned by the API.
  • Loading branch information
amercader committed Dec 6, 2013
1 parent 6dde104 commit 07420ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
36 changes: 28 additions & 8 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -350,16 +350,36 @@ def group_dictize(group, context):

context['with_capacity'] = True

result_dict['packages'] = d.obj_list_dictize(
_get_members(context, group, 'packages'),
context)

query = search.PackageSearchQuery()
# Get group packages from the search index, but adapt the output to
# maintain backwards compatibility
q = {
'facet': 'false',
'rows': 1000, # Only the first 1000 datasets are returned
}
if group.is_organization:
q = {'q': 'owner_org:"%s" +capacity:public' % group.id, 'rows': 1}
q['fq'] = 'owner_org:"{0}"'.format(group.id)
else:
q = {'q': 'groups:"%s" +capacity:public' % group.name, 'rows': 1}
result_dict['package_count'] = query.run(q)['count']
q['fq'] = 'groups:"{0}"'.format(group.name)

is_group_member = (context.get('user') and
new_authz.has_user_permission_for_group_or_org(group.id, context.get('user'), 'read'))
if not is_group_member:
q['fq'] += ' +capacity:public'

search_results = logic.get_action('package_search')(context, q)

This comment has been minimized.

Copy link
@davidread

davidread Jan 3, 2014

Contributor

I think 'schema' needs removing from the context passed to package_search. See #1420

keys_to_keep = ['id', 'name', 'author', 'url', 'title',
'notes', 'owner_org', 'private', 'maintainer_email',
'author_email', 'state', 'version', 'license_id',
'maintainer', 'revision_id', 'type', 'metadata_modified',]
adapted_results = []
for pkg in search_results['results']:
new_pkg = {}
for key in keys_to_keep:
new_pkg[key] = pkg.get(key)
adapted_results.append(new_pkg)

result_dict['packages'] = adapted_results
result_dict['package_count'] = search_results['count']

result_dict['tags'] = tag_list_dictize(
_get_members(context, group, 'tags'),
Expand Down
3 changes: 3 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -993,6 +993,8 @@ def group_show(context, data_dict):
:rtype: dictionary
.. note:: Only its first 1000 datasets are returned
'''
return _group_or_org_show(context, data_dict)

Expand All @@ -1004,6 +1006,7 @@ def organization_show(context, data_dict):
:rtype: dictionary
.. note:: Only its first 1000 datasets are returned
'''
return _group_or_org_show(context, data_dict, is_org=True)

Expand Down
4 changes: 0 additions & 4 deletions ckan/tests/lib/test_dictization.py
Expand Up @@ -958,16 +958,13 @@ def test_16_group_dictized(self):
'name': u'annakarenina3',
'notes': u'Some test notes\n\n### A 3rd level heading\n\n**Some bolded text.**\n\n*Some italicized text.*\n\nForeign characters:\nu with umlaut \xfc\n66-style quote \u201c\nforeign word: th\xfcmb\n\nNeeds escaping:\nleft arrow <\n\n<http://ckan.net/>\n\n',
'state': u'active',
'capacity' : 'in',
'title': u'A Novel By Tolstoy',
'private': False,
'creator_user_id': None,
'owner_org': None,
'url': u'http://www.annakarenina.com',
'version': u'0.7a'},
{'author': None,
'author_email': None,
'capacity' : 'public',
'title': u'A Novel By Tolstoy',
'license_id': u'other-open',
'maintainer': None,
Expand All @@ -978,7 +975,6 @@ def test_16_group_dictized(self):
'state': u'active',
'title': u'A Novel By Tolstoy',
'private': False,
'creator_user_id': None,
'owner_org': None,
'url': u'http://www.annakarenina.com',
'version': u'0.7a'}],
Expand Down

0 comments on commit 07420ae

Please sign in to comment.