Skip to content
Merged
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
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ If you want to use the WYSIWYG editor instead of Markdown to write the content o

ckanext.showcase.editor = ckeditor

Set the dataset types to show in the showcase. By default it is set to
`dataset`, but you can change it to `dataset dataset_series custom_dataset_type`::

ckanext.showcase.show_dataset_types = dataset dataset_series custom_dataset_type

-----------------------------------------------
Migrating Showcases Notes from Markdown to HTML
-----------------------------------------------
Expand Down
54 changes: 34 additions & 20 deletions ckanext/showcase/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,50 @@


def convert_package_name_or_id_to_id_for_type(package_name_or_id,
context, package_type='dataset'):
'''
Return the id for the given package name or id. Only works with packages
of type package_type.

Also validates that a package with the given name or id exists.

:returns: the id of the package with the given name or id
:rtype: string
:raises: ckan.lib.navl.dictization_functions.Invalid if there is no
package with the given name or id

'''
context, package_type=None):
"""Convert package name or ID to ID for a specific type.

This function retrieves the package ID based on the provided name or ID,
ensuring it matches the specified package type if provided. If no package
type is specified, it defaults to checking for any package type.

:param package_name_or_id: The name or ID of the package.
:param context: The context containing the session and model.
:param package_type: The type of the package to filter by (optional).
:returns: The ID of the package.
:raises: Invalid if no package is found with the given name or ID.
"""
session = context['session']
model = context['model']
result = session.query(model.Package) \
.filter_by(id=package_name_or_id, type=package_type).first()

dataset_types = tk.aslist(
package_type or tk.config.get('ckanext.showcase.show_dataset_types', 'dataset')
)
if not dataset_types:
result = model.Package.get(package_name_or_id)
else:
result = (
session.query(model.Package)
.filter(model.Package.id == package_name_or_id,
model.Package.type.in_(dataset_types)).first()
)

if not result:
result = session.query(model.Package) \
.filter_by(name=package_name_or_id, type=package_type).first()
result = (
session.query(model.Package)
.filter(model.Package.name == package_name_or_id,
model.Package.type.in_(dataset_types)).first()
)
if not result:
raise Invalid('%s: %s' % (_('Not found'), _('Dataset')))
raise Invalid('No package is found with the given name or ID')

return result.id


def convert_package_name_or_id_to_id_for_type_dataset(package_name_or_id,
context):
return convert_package_name_or_id_to_id_for_type(package_name_or_id,
context,
package_type='dataset')
context)


def convert_package_name_or_id_to_id_for_type_showcase(package_name_or_id,
Expand Down
29 changes: 10 additions & 19 deletions ckanext/showcase/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,16 @@ def pager_url(q=None, page=None):
'auth_user_obj': tk.g.userobj
}

# Unless changed via config options, don't show other dataset
# types any search page. Potential alternatives are do show them
# on the default search page (dataset) or on one other search page
search_all_type = tk.config.get('ckan.search.show_all_types')
search_all = False

try:
# If the "type" is set to True or False, convert to bool
# and we know that no type was specified, so use traditional
# behaviour of applying this only to dataset type
search_all = tk.asbool(search_all_type)
search_all_type = 'dataset'
# Otherwise we treat as a string representing a type
except ValueError:
search_all = True

if not search_all or package_type != search_all_type:
# Only show datasets of this particular type
fq += ' +dataset_type:{type}'.format(type=package_type)
# Search for packages of the configured package types or of type 'dataset'
# for the Showcase
search_package_types = tk.aslist(
tk.config.get('ckanext.showcase.show_dataset_types', 'dataset'))
if search_package_types:
fq += ' +dataset_type:({types})'.format(
types=' OR '.join(search_package_types))
else:
fq += ' +dataset_type:{type}'.format(
type=package_type)

# Only search for packages that aren't already associated with the
# Showcase
Expand Down