Skip to content

Commit

Permalink
add count_collection_package to tell collection
Browse files Browse the repository at this point in the history
  • Loading branch information
FuhuXia committed Nov 21, 2024
1 parent cca4ec7 commit 7510734
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
17 changes: 15 additions & 2 deletions ckanext/geodatagov/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions ckanext/geodatagov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions ckanext/geodatagov/templates/package/read.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<section class="module-content">
<h3>{{ _('Collection') }}</h3>
{% if count_collection %}
{% set collection_info = collection_sourceid ~ ' ' ~ identifier %}
<p>This dataset is a collection of {{ count_collection }} other datasets.</p>
<p><a href="{{ h.url_for('search', collection_info=collection_info) }}" class="btn-collection">{{ _('Search datasets within this collection') }}</a></p>
{% 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 %}
<p>{{ _('This dataset is part of the following collection:') }}</p>
<ul class="dataset-list unstyled">
Expand All @@ -19,6 +39,7 @@ <h3>{{ _('Collection') }}</h3>
<p>{{ _('This dataset is part of a deleted collection.') }}</p>
<p><a href="{{ h.url_for('search', collection_info=collection_info) }}" class="btn-collection">{{ _('Search other datasets within the same collection') }}</a></p>
{% endif %}
{% endif %}
</section>
{% endif %}
{% endblock %}

0 comments on commit 7510734

Please sign in to comment.