From e204592e883551fd88fb065318b8a88314e535c1 Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Fri, 5 Aug 2016 18:24:17 -0400 Subject: [PATCH] [#3191] package_search: s/ignore_capacity_check/include_private --- ckan/controllers/group.py | 8 +++---- ckan/lib/cli.py | 4 ++-- ckan/lib/dictization/model_dictize.py | 2 +- ckan/logic/action/get.py | 30 ++++++++++++++++++--------- ckan/tests/logic/action/test_get.py | 11 +++++----- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index fb05af7a05f..087209f07c4 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -294,13 +294,12 @@ def pager_url(q=None, page=None): else: search_extras[param] = value - fq = 'capacity:"public"' + include_private = False user_member_of_orgs = [org['id'] for org in h.organizations_available('read')] if (c.group and c.group.id in user_member_of_orgs): - fq = '' - context['ignore_capacity_check'] = True + include_private = True facets = OrderedDict() @@ -327,7 +326,8 @@ def pager_url(q=None, page=None): data_dict = { 'q': q, - 'fq': fq, + 'fq': '', + 'include_private': include_private, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, diff --git a/ckan/lib/cli.py b/ckan/lib/cli.py index cc752679082..5752edd80e1 100644 --- a/ckan/lib/cli.py +++ b/ckan/lib/cli.py @@ -2367,6 +2367,7 @@ def _search_datasets(self, page=1, view_types=[]): 'q': '', 'fq': '', 'fq_list': [], + 'include_private': True, 'rows': n, 'start': n * (page - 1), } @@ -2390,8 +2391,7 @@ def _search_datasets(self, page=1, view_types=[]): search_data_dict['q'] = '*:*' query = p.toolkit.get_action('package_search')( - {'ignore_capacity_check': True}, - search_data_dict) + {}, search_data_dict) return query diff --git a/ckan/lib/dictization/model_dictize.py b/ckan/lib/dictization/model_dictize.py index 0bbfd46d940..f3914cbd3ff 100644 --- a/ckan/lib/dictization/model_dictize.py +++ b/ckan/lib/dictization/model_dictize.py @@ -386,7 +386,7 @@ def get_packages_for_this_group(group_, just_the_count=False): authz.has_user_permission_for_group_or_org( group_.id, context.get('user'), 'read')) if is_group_member: - context['ignore_capacity_check'] = True + q['include_private'] = True if not just_the_count: # Is there a packages limit in the context? diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index 451eb9fe1b1..14c216cf655 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -180,10 +180,9 @@ def current_package_list_with_resources(context, data_dict): _check_access('current_package_list_with_resources', context, data_dict) - is_sysadmin = authz.is_sysadmin(user) - q = '+capacity:public' if not is_sysadmin else '*:*' - context['ignore_capacity_check'] = True - search = package_search(context, {'q': q, 'rows': limit, 'start': offset}) + search = package_search(context, { + 'q': '', 'rows': limit, 'start': offset, + 'include_private': authz.is_sysadmin(user) }) return search.get('results', []) @@ -1453,8 +1452,9 @@ def user_show(context, data_dict): search_dict = {'rows': 50} if include_private_and_draft_datasets: - context['ignore_capacity_check'] = True - search_dict.update({'include_drafts': True}) + search_dict.update({ + 'include_private': True, + 'include_drafts': True}) search_dict.update({'fq': fq}) @@ -1823,16 +1823,26 @@ def package_search(context, data_dict): # return a list of package ids data_dict['fl'] = 'id {0}'.format(data_source) - # If this query hasn't come from a controller that has set this flag - # then we should remove any mention of capacity from the fq and + # we should remove any mention of capacity from the fq and # instead set it to only retrieve public datasets fq = data_dict.get('fq', '') - if not context.get('ignore_capacity_check', False): + include_private = asbool(data_dict.pop('include_private', False)) + if not include_private or not user: fq = ' '.join(p for p in fq.split() if 'capacity:' not in p) data_dict['fq'] = fq + ' capacity:"public"' + elif not authz.is_sysadmin(user): + fq = ' '.join(p for p in fq.split() if 'capacity:' not in p) + orgs = logic.get_action('organization_list_for_user')( + {'user': user}, {'permission': 'member'}) + if orgs: + data_dict['fq'] = (fq + ' (capactiy:"public"' + ' OR owner_org:({0}))'.format(' OR '.join( + org['id'] for org in orgs))) + else: + data_dict['fq'] = fq + ' capacity:"public"' # Solr doesn't need 'include_drafts`, so pop it. - include_drafts = data_dict.pop('include_drafts', False) + include_drafts = asbool(data_dict.pop('include_drafts', False)) fq = data_dict.get('fq', '') if include_drafts: user_id = authz.get_user_id_for_username(user, allow_none=True) diff --git a/ckan/tests/logic/action/test_get.py b/ckan/tests/logic/action/test_get.py index 54a1fd2ab70..8c99b05eedf 100644 --- a/ckan/tests/logic/action/test_get.py +++ b/ckan/tests/logic/action/test_get.py @@ -1192,10 +1192,10 @@ def test_package_search_with_fq_for_create_user_id_will_include_drafts_for_other nose.tools.assert_true(dataset['name'] not in names) nose.tools.assert_true(draft_dataset['name'] in names) - def test_package_search_private_with_ignore_capacity_check(self): + def test_package_search_private_with_include_private(self): ''' package_search() can return private datasets when - `ignore_capacity_check` present in context. + `include_private=True` ''' user = factories.User() org = factories.Organization(user=user) @@ -1204,9 +1204,10 @@ def test_package_search_private_with_ignore_capacity_check(self): factories.Dataset(user=user, state='draft') private_dataset = factories.Dataset(user=user, private=True, owner_org=org['name']) - fq = '+capacity:"private"' - results = helpers.call_action('package_search', fq=fq, - context={'ignore_capacity_check': True})['results'] + results = helpers.call_action( + 'package_search', + include_private=True, + context={'user': user['name']})['results'] eq(len(results), 1) eq(results[0]['name'], private_dataset['name'])