diff --git a/ckanext/geodatagov/helpers.py b/ckanext/geodatagov/helpers.py index 5ab56f81..7dcdc99e 100644 --- a/ckanext/geodatagov/helpers.py +++ b/ckanext/geodatagov/helpers.py @@ -1,9 +1,10 @@ import json import logging +from ckan import model from ckan import plugins as p from ckanext.harvest.model import HarvestSource -from ckan.logic import NotFound, NotAuthorized +from ckan.logic import NotFound, NotAuthorized, get_action log = logging.getLogger(__name__) @@ -59,12 +60,45 @@ def get_harvest_source_config(harvester_id): return source_config -def get_collection_package(collection_package_id): - try: - package = p.toolkit.get_action('package_show')({}, {'id': collection_package_id}) - return package - except (NotFound, NotAuthorized): - pass +def count_collection_package(source_id, identifier): + context = {'model': model, 'session': model.Session} + package_search = get_action('package_search') + search_params = { + 'fq': f'harvest_source_id:{source_id} isPartOf:{identifier} include_collection:true', + 'rows': 0, + } + + search_result = package_search(context, search_params) + + return search_result['count'] if search_result['count'] else 0 + + +def get_collection_package(source_id, identifier): + context = {'model': model, 'session': model.Session} + + package_search = get_action('package_search') + search_params = { + 'fq': f'harvest_source_id:{source_id} identifier:{identifier}', + 'rows': 1, + } + + search_result = package_search(context, search_params) + + ret = None + + if search_result['results']: + collection_package_id = search_result['results'][0]['id'] + + try: + package = p.toolkit.get_action('package_show')( + context, + {'id': collection_package_id} + ) + ret = package + except (NotFound, NotAuthorized): + pass + + return ret def string(value): diff --git a/ckanext/geodatagov/plugin.py b/ckanext/geodatagov/plugin.py index 4026ab63..ae8fd709 100644 --- a/ckanext/geodatagov/plugin.py +++ b/ckanext/geodatagov/plugin.py @@ -499,11 +499,22 @@ def before_dataset_search(self, search_params): pattern = r'collection_info:"([^"]+?) ([^"]+)"' fq = re.sub(pattern, r'harvest_source_id:"\1" isPartOf:"\2"', fq) log.info('FQ changed for collection_info') - elif 'bulk_process' not in path: + elif 'bulk_process' not in path and 'include_collection' not in fq: # hide collection's children datasets from regular search fq += ' -isPartOf:["" TO *]' log.info('Added FQ to hide collection') + # fq comes in as a string such as '(a:1 b:"2" c:["" to *])' + # remove string include_collection=true from fq, if found. + # Other values of include_collection will end up with a search term that return no results + pattern = r'include_collection:"?([^",\s)]+)"?' + match = re.search(pattern, fq, re.IGNORECASE) + if match and match.group(1).lower() == 'true': + fq = re.sub(pattern, '', fq, flags=re.IGNORECASE).strip() + # if include_collection=true is the only fq, we could end up with a set of parentheses. + if fq == "()": + fq = "" + search_params['fq'] = fq return search_params @@ -536,6 +547,7 @@ def get_helpers(self): 'get_harvest_source_type': geodatagov_helpers.get_harvest_source_type, 'get_harvest_source_config': geodatagov_helpers.get_harvest_source_config, 'get_collection_package': geodatagov_helpers.get_collection_package, + 'count_collection_package': geodatagov_helpers.count_collection_package, } # IActions diff --git a/ckanext/geodatagov/templates/package/read.html b/ckanext/geodatagov/templates/package/read.html index 96d1a34e..f5c922d6 100644 --- a/ckanext/geodatagov/templates/package/read.html +++ b/ckanext/geodatagov/templates/package/read.html @@ -3,17 +3,33 @@ {% set pkg_dict = c.pkg_dict %} {% block collection_resources %} -{% set collection_package_id = h.get_pkg_dict_extra(c.pkg_dict, 'collection_package_id', '') %} -{% if h.get_pkg_dict_extra(c.pkg_dict, 'collection_metadata', '') %} -
-

{{ _('Collection') }}

-

{{ _('This dataset is a collection of other datasets.') }}

-

{{ _('Search datasets within this collection') }}

-
-{% elif collection_package_id %} -{% set collection_package = h.get_collection_package(collection_package_id) %} + +{% set identifier = h.get_pkg_dict_extra(c.pkg_dict, 'Identifier', '') %} +{% set collection_sourceid = h.get_pkg_dict_extra(c.pkg_dict, 'harvest_source_id', '') %} +{% set collection_ispartof = h.get_pkg_dict_extra(c.pkg_dict, 'isPartOf', '') %} + +{# + 1. we check each dataset to see if it is a collection parent using its identifier. + if count_collection returns a value other than 0, then we know that this dataset is a collection parent. + + 2. we use collection_ispartof to tell if this dataset is a collection child. + if collection_package returns a dataset, then we know its collection parent is found. +#} + +{% set count_collection = h.count_collection_package(collection_sourceid, identifier) %} + +{% if count_collection or collection_ispartof %}

{{ _('Collection') }}

+ {% if count_collection %} + {% set collection_info = collection_sourceid ~ ' ' ~ identifier %} +

This dataset is a collection of {{ count_collection }} other datasets.

+

{{ _('Search datasets within this collection') }}

+ {% endif %} + + {% if collection_ispartof %} + {% set collection_info = collection_sourceid ~ ' ' ~ collection_ispartof %} + {% set collection_package = h.get_collection_package(collection_sourceid, collection_ispartof) %} {% if collection_package %}

{{ _('This dataset is part of the following collection:') }}

{% else %}

{{ _('This dataset is part of a deleted collection.') }}

-

{{ _('Search other datasets within the same collection') }}

+

{{ _('Search other datasets within the same collection') }}

+ {% endif %} {% endif %}
{% endif %}