diff --git a/README.rst b/README.rst index d7325e5b..ee7b1295 100644 --- a/README.rst +++ b/README.rst @@ -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 ----------------------------------------------- diff --git a/ckanext/showcase/logic/validators.py b/ckanext/showcase/logic/validators.py index b27885a9..ebd7bc26 100644 --- a/ckanext/showcase/logic/validators.py +++ b/ckanext/showcase/logic/validators.py @@ -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, diff --git a/ckanext/showcase/utils.py b/ckanext/showcase/utils.py index e078f3c4..b9a959ae 100644 --- a/ckanext/showcase/utils.py +++ b/ckanext/showcase/utils.py @@ -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