From 7510734bc9ff949a4d2eca225123356fbec2ff87 Mon Sep 17 00:00:00 2001 From: Fuhu Xia Date: Thu, 21 Nov 2024 09:15:11 -0500 Subject: [PATCH] add count_collection_package to tell collection --- ckanext/geodatagov/helpers.py | 17 ++++++++++-- ckanext/geodatagov/plugin.py | 1 + .../geodatagov/templates/package/read.html | 27 ++++++++++++++++--- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/ckanext/geodatagov/helpers.py b/ckanext/geodatagov/helpers.py index ec4d1fd0..7dcdc99e 100644 --- a/ckanext/geodatagov/helpers.py +++ b/ckanext/geodatagov/helpers.py @@ -60,12 +60,25 @@ def get_harvest_source_config(harvester_id): return source_config -def get_collection_package(source_id, ispartof): +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:{ispartof}', + 'fq': f'harvest_source_id:{source_id} identifier:{identifier}', 'rows': 1, } diff --git a/ckanext/geodatagov/plugin.py b/ckanext/geodatagov/plugin.py index f0883057..ae8fd709 100644 --- a/ckanext/geodatagov/plugin.py +++ b/ckanext/geodatagov/plugin.py @@ -547,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 6a5db1f3..f5c922d6 100644 --- a/ckanext/geodatagov/templates/package/read.html +++ b/ckanext/geodatagov/templates/package/read.html @@ -3,13 +3,33 @@ {% set pkg_dict = c.pkg_dict %} {% block collection_resources %} + +{% 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', '') %} -{% set collection_info = collection_sourceid ~ ' ' ~ collection_ispartof %} -{% if collection_ispartof %} -{% set collection_package = h.get_collection_package(collection_sourceid, collection_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:') }}

{% endif %} {% endblock %}