Skip to content

Commit

Permalink
[#1208] Return whole dicts on package listing for group_show
Browse files Browse the repository at this point in the history
The reason is that this is consistent with other package listings
(tag_show, package_search) and at the same time is backwards compatible
as it keeps all the existing fields.

Users would be able to choose not to return the packages list.
  • Loading branch information
amercader committed Dec 9, 2013
1 parent f6e5aa7 commit eeb6e65
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 56 deletions.
47 changes: 20 additions & 27 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -349,40 +349,33 @@ def group_dictize(group, context):

context['with_capacity'] = True

if context.get('include_group_packages', True):
# 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
}
include_packages = context.get('include_group_packages', True)

q = {
'facet': 'false',
'rows': 0,
}

if include_packages:

q['rows'] = 1000 # Only the first 1000 datasets are returned

if group.is_organization:
q['fq'] = 'owner_org:"{0}"'.format(group.id)
else:
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)
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']
else:
result_dict['packages'] = None
result_dict['package_count'] = None
if is_group_member:
context['ignore_capacity_check'] = True

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

if include_packages:
result_dict['packages'] = search_results['results']

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

result_dict['tags'] = tag_list_dictize(
_get_members(context, group, 'tags'),
Expand Down
169 changes: 140 additions & 29 deletions ckan/tests/lib/test_dictization.py
Expand Up @@ -949,35 +949,146 @@ def test_16_group_dictized(self):
'image_display_url': u'',
'package_count': 2,
'is_organization': False,
'packages': [{'author': None,
'author_email': None,
'license_id': u'other-open',
'maintainer': None,
'maintainer_email': None,
'type': u'dataset',
'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',
'title': u'A Novel By Tolstoy',
'private': False,
'owner_org': None,
'url': u'http://www.annakarenina.com',
'version': u'0.7a'},
{'author': None,
'author_email': None,
'title': u'A Novel By Tolstoy',
'license_id': u'other-open',
'maintainer': None,
'maintainer_email': None,
'type': u'dataset',
'name': u'annakarenina2',
'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',
'title': u'A Novel By Tolstoy',
'private': False,
'owner_org': None,
'url': u'http://www.annakarenina.com',
'version': u'0.7a'}],
'packages': [{u'author': None,
u'author_email': None,
u'creator_user_id': None,
u'extras': [],
u'groups': [{u'approval_status': u'approved',
u'capacity': u'public',
u'description': u'',
u'image_url': u'',
u'is_organization': False,
u'name': u'help',
u'state': u'active',
u'title': u'help',
u'type': u'group'}],
u'isopen': True,
u'license_id': u'other-open',
u'license_title': u'Other (Open)',
u'maintainer': None,
u'maintainer_email': None,
u'name': u'annakarenina2',
u'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',
u'num_resources': 0,
u'num_tags': 0,
u'organization': None,
u'owner_org': None,
u'private': False,
u'relationships_as_object': [],
u'relationships_as_subject': [],
u'resources': [],
u'state': u'active',
u'tags': [],
u'title': u'A Novel By Tolstoy',
u'tracking_summary': {u'recent': 0, u'total': 0},
u'type': u'dataset',
u'url': u'http://www.annakarenina.com',
u'version': u'0.7a'},
{u'author': None,
u'author_email': None,
u'creator_user_id': None,
u'extras': [{u'key': u'genre',
u'state': u'active',
u'value': u'romantic novel'},
{u'key': u'original media',
u'state': u'active',
u'value': u'book'}],
u'groups': [{u'approval_status': u'approved',
u'capacity': u'in',
u'description': u'',
u'image_url': u'',
u'is_organization': False,
u'name': u'help',
u'state': u'active',
u'title': u'help',
u'type': u'group'},
{u'approval_status': u'approved',
u'capacity': u'public',
u'description': u'These are books that David likes.',
u'image_url': u'',
u'is_organization': False,
u'name': u'david',
u'state': u'active',
u'title': u"Dave's books",
u'type': u'group'},
{u'approval_status': u'approved',
u'capacity': u'public',
u'description': u'Roger likes these books.',
u'image_url': u'',
u'is_organization': False,
u'name': u'roger',
u'state': u'active',
u'title': u"Roger's books",
u'type': u'group'}],
u'isopen': True,
u'license_id': u'other-open',
u'license_title': u'Other (Open)',
u'maintainer': None,
u'maintainer_email': None,
u'name': u'annakarenina3',
u'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',
u'num_resources': 2,
u'num_tags': 3,
u'organization': None,
u'owner_org': None,
u'private': False,
u'relationships_as_object': [],
u'relationships_as_subject': [],
u'resources': [{u'alt_url': u'alt123',
u'cache_last_updated': None,
u'cache_url': None,
u'description': u'Full text. Needs escaping: " Umlaut: \xfc',
u'format': u'plain text',
u'hash': u'abc123',
u'last_modified': None,
u'mimetype': None,
u'mimetype_inner': None,
u'name': None,
u'position': 0,
u'resource_type': None,
u'size': None,
u'size_extra': u'123',
u'state': u'active',
u'tracking_summary': {u'recent': 0, u'total': 0},
u'url': u'http://www.annakarenina.com/download/x=1&y=2',
u'url_type': None,
u'webstore_last_updated': None,
u'webstore_url': None},
{u'alt_url': u'alt345',
u'cache_last_updated': None,
u'cache_url': None,
u'description': u'Index of the novel',
u'format': u'JSON',
u'hash': u'def456',
u'last_modified': None,
u'mimetype': None,
u'mimetype_inner': None,
u'name': None,
u'position': 1,
u'resource_type': None,
u'size': None,
u'size_extra': u'345',
u'state': u'active',
u'tracking_summary': {u'recent': 0, u'total': 0},
u'url': u'http://www.annakarenina.com/index.json',
u'url_type': None,
u'webstore_last_updated': None,
u'webstore_url': None}],
u'state': u'active',
u'tags': [{u'display_name': u'Flexible \u30a1',
u'name': u'Flexible \u30a1',
u'state': u'active'},
{u'display_name': u'russian',
u'name': u'russian',
u'state': u'active'},
{u'display_name': u'tolstoy',
u'name': u'tolstoy',
u'state': u'active'}],
u'title': u'A Novel By Tolstoy',
u'tracking_summary': {u'recent': 0, u'total': 0},
u'type': u'dataset',
u'url': u'http://www.annakarenina.com',
u'version': u'0.7a'}],
'state': u'active',
'approval_status': u'approved',
'title': u'help',
Expand Down

0 comments on commit eeb6e65

Please sign in to comment.