From f6e5aa773cda8960788301ec051364479142a192 Mon Sep 17 00:00:00 2001 From: amercader Date: Fri, 6 Dec 2013 17:31:59 +0000 Subject: [PATCH] [#1208] Do not query datasets on group_show from the controller 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 --- ckan/controllers/group.py | 7 +++ ckan/lib/dictization/model_dictize.py | 63 ++++++++++++++------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index 8d4a90d4b7a..060d99b45ca 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -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: @@ -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.') @@ -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: diff --git a/ckan/lib/dictization/model_dictize.py b/ckan/lib/dictization/model_dictize.py index d4248cfb794..3ef562a0543 100644 --- a/ckan/lib/dictization/model_dictize.py +++ b/ckan/lib/dictization/model_dictize.py @@ -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 @@ -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'),