Skip to content

Commit

Permalink
[#1208] Do not query datasets on group_show from the controller
Browse files Browse the repository at this point in the history
When dictizing the group as requested from the controller, don't query
Solr for datasets as they will be ignored and get requested on the
controller later on anyway
  • Loading branch information
amercader committed Dec 6, 2013
1 parent 07420ae commit f6e5aa7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
7 changes: 7 additions & 0 deletions ckan/controllers/group.py
Expand Up @@ -178,6 +178,9 @@ def read(self, id, limit=20):
q = c.q = request.params.get('q', '')

try:
# Do not query for the group datasets when dictizing, as they will
# be ignored and get requested on the controller anyway
context['include_group_packages'] = False
c.group_dict = self._action('group_show')(context, data_dict)
c.group = context['group']
except NotFound:
Expand Down Expand Up @@ -327,6 +330,7 @@ def pager_url(q=None, page=None):
items_per_page=limit
)

c.group_dict['package_count'] = query['count']
c.facets = query['facets']
maintain.deprecate_context_item('facets',
'Use `c.search_facets` instead.')
Expand Down Expand Up @@ -369,6 +373,9 @@ def bulk_process(self, id):
data_dict = {'id': id}

try:
# Do not query for the group datasets when dictizing, as they will
# be ignored and get requested on the controller anyway
context['include_group_packages'] = False
c.group_dict = self._action('group_show')(context, data_dict)
c.group = context['group']
except NotFound:
Expand Down
63 changes: 33 additions & 30 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -340,7 +340,6 @@ def _get_members(context, group, member_type):


def group_dictize(group, context):
model = context['model']
result_dict = d.table_dictize(group, context)

result_dict['display_name'] = group.display_name
Expand All @@ -350,36 +349,40 @@ def group_dictize(group, context):

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

result_dict['tags'] = tag_list_dictize(
_get_members(context, group, 'tags'),
Expand Down

0 comments on commit f6e5aa7

Please sign in to comment.