Skip to content

Commit 73ce544

Browse files
committed
Manage certain dataset types
1 parent 47e5052 commit 73ce544

File tree

4 files changed

+53
-39
lines changed

4 files changed

+53
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Ckanext-showcase CHANGELOG
22

3+
## v1.9.0 2025-07-24
4+
5+
* Change the logic of managing datasets of certain types in showcase
6+
37
## v1.8.4 2025-07-22
48

59
* Remove unused add_datasets.html template (#186)

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ If you want to use the WYSIWYG editor instead of Markdown to write the content o
165165

166166
ckanext.showcase.editor = ckeditor
167167

168+
Set the dataset types to show in the showcase. By default it is set to
169+
`dataset`, but you can change it to `dataset dataset_series custom_dataset_type`::
170+
171+
ckanext.showcase.show_dataset_types = dataset dataset_series custom_dataset_type
172+
168173
-----------------------------------------------
169174
Migrating Showcases Notes from Markdown to HTML
170175
-----------------------------------------------

ckanext/showcase/logic/validators.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,50 @@
55

66

77
def convert_package_name_or_id_to_id_for_type(package_name_or_id,
8-
context, package_type='dataset'):
9-
'''
10-
Return the id for the given package name or id. Only works with packages
11-
of type package_type.
12-
13-
Also validates that a package with the given name or id exists.
14-
15-
:returns: the id of the package with the given name or id
16-
:rtype: string
17-
:raises: ckan.lib.navl.dictization_functions.Invalid if there is no
18-
package with the given name or id
19-
20-
'''
8+
context, package_type=None):
9+
"""Convert package name or ID to ID for a specific type.
10+
11+
This function retrieves the package ID based on the provided name or ID,
12+
ensuring it matches the specified package type if provided. If no package
13+
type is specified, it defaults to checking for any package type.
14+
15+
:param package_name_or_id: The name or ID of the package.
16+
:param context: The context containing the session and model.
17+
:param package_type: The type of the package to filter by (optional).
18+
:returns: The ID of the package.
19+
:raises: Invalid if no package is found with the given name or ID.
20+
"""
2121
session = context['session']
2222
model = context['model']
23-
result = session.query(model.Package) \
24-
.filter_by(id=package_name_or_id, type=package_type).first()
23+
24+
dataset_types = tk.aslist(
25+
package_type or tk.config.get('ckanext.showcase.show_dataset_types', 'dataset')
26+
)
27+
if not dataset_types:
28+
result = model.Package.get(package_name_or_id)
29+
else:
30+
result = (
31+
session.query(model.Package)
32+
.filter(model.Package.id == package_name_or_id,
33+
model.Package.type.in_(dataset_types)).first()
34+
)
35+
2536
if not result:
26-
result = session.query(model.Package) \
27-
.filter_by(name=package_name_or_id, type=package_type).first()
37+
result = (
38+
session.query(model.Package)
39+
.filter(model.Package.id == package_name_or_id,
40+
model.Package.type.in_(dataset_types)).first()
41+
)
2842
if not result:
29-
raise Invalid('%s: %s' % (_('Not found'), _('Dataset')))
43+
raise Invalid('No package is found with the given name or ID')
44+
3045
return result.id
3146

3247

3348
def convert_package_name_or_id_to_id_for_type_dataset(package_name_or_id,
3449
context):
3550
return convert_package_name_or_id_to_id_for_type(package_name_or_id,
36-
context,
37-
package_type='dataset')
51+
context)
3852

3953

4054
def convert_package_name_or_id_to_id_for_type_showcase(package_name_or_id,

ckanext/showcase/utils.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -286,25 +286,16 @@ def pager_url(q=None, page=None):
286286
'auth_user_obj': tk.g.userobj
287287
}
288288

289-
# Unless changed via config options, don't show other dataset
290-
# types any search page. Potential alternatives are do show them
291-
# on the default search page (dataset) or on one other search page
292-
search_all_type = tk.config.get('ckan.search.show_all_types')
293-
search_all = False
294-
295-
try:
296-
# If the "type" is set to True or False, convert to bool
297-
# and we know that no type was specified, so use traditional
298-
# behaviour of applying this only to dataset type
299-
search_all = tk.asbool(search_all_type)
300-
search_all_type = 'dataset'
301-
# Otherwise we treat as a string representing a type
302-
except ValueError:
303-
search_all = True
304-
305-
if not search_all or package_type != search_all_type:
306-
# Only show datasets of this particular type
307-
fq += ' +dataset_type:{type}'.format(type=package_type)
289+
# Search for packages of the configured package types or of type 'dataset'
290+
# for the Showcase
291+
search_package_types = tk.aslist(
292+
tk.config.get('ckanext.showcase.show_dataset_types', 'dataset'))
293+
if search_package_types:
294+
fq += ' +dataset_type:({types})'.format(
295+
types=' OR '.join(search_package_types))
296+
else:
297+
fq += ' +dataset_type:{type}'.format(
298+
type=package_type)
308299

309300
# Only search for packages that aren't already associated with the
310301
# Showcase

0 commit comments

Comments
 (0)