Skip to content

Commit

Permalink
[#3191] package_search: s/ignore_capacity_check/include_private
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Aug 5, 2016
1 parent 60134e8 commit e204592
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
8 changes: 4 additions & 4 deletions ckan/controllers/group.py
Expand Up @@ -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()

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions ckan/lib/cli.py
Expand Up @@ -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),
}
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/dictization/model_dictize.py
Expand Up @@ -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?
Expand Down
30 changes: 20 additions & 10 deletions ckan/logic/action/get.py
Expand Up @@ -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', [])


Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions ckan/tests/logic/action/test_get.py
Expand Up @@ -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)
Expand All @@ -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'])
Expand Down

0 comments on commit e204592

Please sign in to comment.