|
5 | 5 |
|
6 | 6 |
|
7 | 7 | 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 | + """ |
21 | 21 | session = context['session'] |
22 | 22 | 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 | + |
25 | 36 | 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 | + ) |
28 | 42 | 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 | + |
30 | 45 | return result.id |
31 | 46 |
|
32 | 47 |
|
33 | 48 | def convert_package_name_or_id_to_id_for_type_dataset(package_name_or_id, |
34 | 49 | context): |
35 | 50 | return convert_package_name_or_id_to_id_for_type(package_name_or_id, |
36 | | - context, |
37 | | - package_type='dataset') |
| 51 | + context) |
38 | 52 |
|
39 | 53 |
|
40 | 54 | def convert_package_name_or_id_to_id_for_type_showcase(package_name_or_id, |
|
0 commit comments