Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/225.canada.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added capability for the `dataset.read` view function to "search" a package's resources via the URI arg `resource_query`. It only searches by the resource names.
17 changes: 17 additions & 0 deletions ckan/views/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from typing_extensions import TypeAlias
from urllib.parse import urlencode
from typing import Any, Iterable, Optional, Union, cast
# (canada fork only): resource search
# TODO: upstream contrib??
from html import escape as html_escape

from flask import Blueprint
from flask.views import MethodView
Expand Down Expand Up @@ -489,6 +492,20 @@ def read(package_type: str, id: str) -> Union[Response, str]:
return h.redirect_to(u'{}.read'.format(package_type),
id=pkg_dict['name'])

# (canada fork only): resource search
# TODO: upstream contrib??
resource_query = request.args.get("resource_query")
if resource_query:
resource_query = html_escape(resource_query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed. Flask has already converted any url-encoded characters here and the resource name we're comparing against is not escaped.

Suggested change
resource_query = html_escape(resource_query)

filtered_resources = []
for res_dict in pkg_dict['resources']:
res_name = h.get_translated(res_dict, 'name')
if resource_query.lower() not in res_name.lower():
continue
filtered_resources.append(res_dict)
pkg_dict['resources'] = filtered_resources
Comment on lines +500 to +506
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to save some steps and temporary variables:

Suggested change
filtered_resources = []
for res_dict in pkg_dict['resources']:
res_name = h.get_translated(res_dict, 'name')
if resource_query.lower() not in res_name.lower():
continue
filtered_resources.append(res_dict)
pkg_dict['resources'] = filtered_resources
pkg_dict['resources'] = [
r for r in pkg_dict['resources']
if resource_query.lower() in h.get_translated(r, 'name').lower()
]

g.resource_query = resource_query

# can the resources be previewed?
for resource in pkg_dict[u'resources']:
resource_views = get_action(u'resource_view_list')(
Expand Down
Loading