Skip to content

Commit

Permalink
use solr search to get_collection_package
Browse files Browse the repository at this point in the history
  • Loading branch information
FuhuXia committed Nov 21, 2024
1 parent 800c6dc commit cca4ec7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
35 changes: 28 additions & 7 deletions ckanext/geodatagov/helpers.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -59,12 +60,32 @@ 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 get_collection_package(source_id, ispartof):
context = {'model': model, 'session': model.Session}

package_search = get_action('package_search')
search_params = {
'fq': f'harvest_source_id:{source_id} identifier:{ispartof}',
'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):
Expand Down
16 changes: 6 additions & 10 deletions ckanext/geodatagov/templates/package/read.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
{% 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', '') %}
<section class="module-content">
<h3>{{ _('Collection') }}</h3>
<p>{{ _('This dataset is a collection of other datasets.') }}</p>
<p><a href="{{ h.url_for('search', collection_package_id=pkg_dict.id) }}" class="btn-collection">{{ _('Search datasets within this collection') }}</a></p>
</section>
{% elif collection_package_id %}
{% set collection_package = h.get_collection_package(collection_package_id) %}
{% 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) %}
<section class="module-content">
<h3>{{ _('Collection') }}</h3>
{% if collection_package %}
Expand All @@ -21,7 +17,7 @@ <h3>{{ _('Collection') }}</h3>
</ul>
{% else %}
<p>{{ _('This dataset is part of a deleted collection.') }}</p>
<p><a href="{{ h.url_for('search', collection_package_id=collection_package_id) }}" class="btn-collection">{{ _('Search other datasets within the same collection') }}</a></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 %}
</section>
{% endif %}
Expand Down

0 comments on commit cca4ec7

Please sign in to comment.