Skip to content

Commit

Permalink
Merge pull request #7719 from ckan/7718-correct-dataset-count
Browse files Browse the repository at this point in the history
fix dataset counts when using permission labels
  • Loading branch information
amercader committed Sep 21, 2023
2 parents 831ff5d + f96319a commit 83220e5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
1 change: 1 addition & 0 deletions changes/7718.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix dataset counts when using permission labels
36 changes: 16 additions & 20 deletions ckan/lib/dictization/model_dictize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import ckan.lib.munge as munge
import ckan.model as model
from ckan.types import Context
from ckan.common import config

## package save

Expand Down Expand Up @@ -64,7 +63,8 @@ def group_list_dictize(
if with_package_counts and 'dataset_counts' not in group_dictize_context:
# 'dataset_counts' will already be in the context in the case that
# group_list_dictize recurses via group_dictize (groups in groups)
group_dictize_context['dataset_counts'] = get_group_dataset_counts()
group_dictize_context['dataset_counts'] = get_group_dataset_counts(
logic.fresh_context(context))
if context.get('with_capacity'):
group_list = [
group_dictize(
Expand Down Expand Up @@ -323,14 +323,19 @@ def _get_members(context: Context, group: model.Group,
return q.all()


def get_group_dataset_counts() -> dict[str, Any]:
def get_group_dataset_counts(search_context: Context) -> dict[str, Any]:
'''For all public groups, return their dataset counts, as a SOLR facet'''
query = search.PackageSearchQuery()
q: dict[str, Any] = {'q': '',
'fl': 'groups', 'facet.field': ['groups', 'owner_org'],
'facet.limit': -1, 'rows': 1}
query.run(q)
return query.facets
q: dict[str, Any] = {
'facet.limit': -1,
'facet.field': ['groups', 'owner_org'],
'fl': 'groups',
'rows': 0,
'include_private': True,
}
# FIXME: layer violation, like get_packages_for_this_group below
search_results = logic.get_action('package_search')(
search_context, q)
return search_results['facets']


def group_dictize(group: model.Group, context: Context,
Expand Down Expand Up @@ -376,14 +381,7 @@ def get_packages_for_this_group(group_: model.Group,
q['fq'] = '+groups:"{0}"'.format(group_.name)

if group_.is_organization:
is_group_member = (context.get('user') and
authz.has_user_permission_for_group_or_org(
group_.id, context.get('user'), 'read'))
if is_group_member:
q['include_private'] = True
else:
if config.get('ckan.auth.allow_dataset_collaborators'):
q['include_private'] = True
q['include_private'] = True

if not just_the_count:
# package_search limits 'rows' anyway, so this is only if you
Expand All @@ -395,9 +393,7 @@ def get_packages_for_this_group(group_: model.Group,
else:
q['rows'] = packages_limit

search_context = cast(
Context, dict((k, v) for (k, v) in context.items()
if k != 'schema'))
search_context = logic.fresh_context(context)
search_results = logic.get_action('package_search')(
search_context, q)
return search_results['count'], search_results['results']
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/lib/dictization/test_model_dictize.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_group_dictize_with_package_count(self):
context = {
"model": model,
"session": model.Session,
"dataset_counts": model_dictize.get_group_dataset_counts(),
"dataset_counts": model_dictize.get_group_dataset_counts({}),
}

group = model_dictize.group_dictize(
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_group_dictize_for_org_with_package_count(self):
context = {
"model": model,
"session": model.Session,
"dataset_counts": model_dictize.get_group_dataset_counts(),
"dataset_counts": model_dictize.get_group_dataset_counts({}),
}

org = model_dictize.group_dictize(
Expand Down

0 comments on commit 83220e5

Please sign in to comment.