ckanext-create-typed-package


Nameckanext-create-typed-package JSON
Version 0.5.0.post1 PyPI version JSON
download
home_pageNone
SummarySelect dataset type when adding new data
upload_time2025-09-09 17:15:00
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseAGPL
keywords ckan
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            [![Tests](https://github.com/DataShades/ckanext-create_typed_package/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/DataShades/ckanext-create_typed_package/actions)

# ckanext-create_typed_package


Add dataset type selector to the "Add dataset" button.

![Preview](https://github.com/DataShades/ckanext-create_typed_package/raw/master/selector.png)


## Installation

To install ckanext-create_typed_package:

1. Install the ckanext-create_typed_package Python package

		pip install ckanext-create-typed-package

1. Add ``create_typed_package`` to the ``ckan.plugins`` setting in your CKAN
   config file.


## Usage

This plugin adds a widget/page for selecting dataset type when user trying to
create the new dataset. Note, the plugin itself does not register any custom
dataset types neither it introduces any tools for registering dataset types. It
means, you need to create multiple dataset types manually before using this
plugin. Check [CKAN
documentation](https://docs.ckan.org/en/2.11/extensions/remote-config-update.html)
or [ckanext-scheming](https://github.com/ckan/ckanext-scheming) if you don't
know how to create dataset types.

For simplicity, we'll use ckanext-scheming in this guide. Assuming you don't
have any custom dataset types, let's create them right now. First, install
ckanext-scheming:

```sh
pip install ckanext-scheming
```

Then add `scheming_datasets` to the list of enabled plugins, alongside with
`create_typed_package`:

```ini
ckan.plugins = scheming_datasets create_typed_package
```

And now enable in the CKAN config file three custom dataset types that are used
by the current extension for tests:

```ini
scheming.dataset_schemas =
                         ckanext.create_typed_package.tests:schemas/first.yaml
                         ckanext.create_typed_package.tests:schemas/second.yaml
                         ckanext.create_typed_package.tests:schemas/third.yaml
```

Restart CKAN. Because of custom datset types, apart from the standard dataset
listing available at `/dataset/`, you also have 3 new endpoints, one per custom
dataset type:

* `/first-type`
  ![First type listing](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/first-type-listing.png)
* `/second-type`
  ![First type listing](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/second-type-listing.png)

* `/third-type`
  ![Third type listing](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/third-type-listing.png)


On every custom page you can click **Add ???-type** and dataset creation form
with corresponding metadata fields will be rendered.

![First type form](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/first-type-form.png)

But if you go back to main `/dataset` page and click **Add dataset** there,
you'll see the modal where you can select the exact type you want to create:

![Selector inside modal](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/modal.png)

That's the main feature provided by the current plugin.

Note, if you don't see the modal, check the JavaScript console for errors. If
you see no errors or cannot fix them, thre is an alternative way.

Modify the templates and add linkg to `/dataset/select-type`. This page does
not exist yet and in order to fix it, add
`create_typed_package.use_separate_route = true` to the CKAN config file. Then
reload the application and now you'll see dataset type selector on this new
route.

![Selector on separate page](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/separate-route.png)

This method is more predictable but it requires modification of templates,
that's why modal is chosen as a default.

### Dynamic options

If you need to show different options for different pages, for example, only
type A1 and A2 for organization AAA, only type B1 and B2 for organization BBB,
etc, you need to add [chained
action](https://docs.ckan.org/en/2.11/extensions/plugins-toolkit.html#ckan.plugins.toolkit.ckan.plugins.toolkit.chained_action)
`ctp_list_types`.

In your own extension that implements
[IAction](https://docs.ckan.org/en/2.11/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IActions)
interface, register action `ctp_list_types` with the following code:

```py
import ckan.plugins.toolkit as tk
from ckan.types import Context

@tk.chained_action
@tk.side_effect_free
def ctp_list_types(next_action: Any, context: Context, data_dict: dict[str, Any]):

    if tk.get_endpoint() == ("organization", "read"):
        organization_id = tk.request.view_args.id
        allowed_types = ... # compute types depending on organization_id

        return [
            {"name": item.VALUE, "label": item.LABEL}
            for item in allowed_types
        ]

    return next_action(context, data_dict)
```

Here we check current endpoint using `tk.get_endpoint()`. If we are on the
organization page, instead of all available types we return only subset of
`allowed_types`. Note, response must be in format of list that contains
dictionaries with `name`(actual dataset type) and `label`(human readable label
for `select` tag). If action returns more than one item, user sees a modal upon
clicking **Add Dataset**. If action returns *exactly* one item, user is
redirected to the corresponding form immediately after the click on **Add
Dataset**.

## Config settings

    # Build list of package types using ckanext-scheming API instead of
	# internal CKAN's package_type registry
	# (optional, default: false).
	create_typed_package.use_scheming = true

	# Additional types that are not are not automatically added to the
	# list for some reason
	# (optional, default: []).
	create_typed_package.additional_types = custom_type another_type

	# Package types that need to be excluded from the list of available
	# types
	# (optional, default: []).
	create_typed_package.exclude_types = custom_type another_type

	# After clicking on "Add datset" button redirect user to special
	# page with dataset type selector instead of using in-place modal
	# (optional, default: false).
	create_typed_package.use_separate_route = true

	# URL where the special page with dataset type selector will be registered.
	# (optional, default: /dataset/select-type).
	create_typed_package.route_path = /create-package/select-type

	# Custom label for dataset type. It will be used by `ctp_list_types`
	# action and, as result, by the type-picker UI widget. Labels provided in a
	# form `create_typed_package.label_for.<TYPE>`, where machine-name for a type
	# is used instead of `<TYPE>`.
	# (optional, default: tk._(type_machine_name)).
	create_typed_package.label_for.dataset = Publication

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "ckanext-create-typed-package",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "DataShades <datashades@linkdigital.com.au>",
    "keywords": "CKAN",
    "author": null,
    "author_email": "DataShades <datashades@linkdigital.com.au>, Sergey Motornyuk <sergey.motornyuk@linkdigital.com.au>",
    "download_url": "https://files.pythonhosted.org/packages/f0/72/449e27e4ef18e84adb940a2fa488b0158512aa677e1bc828972ba6e08f17/ckanext_create_typed_package-0.5.0.post1.tar.gz",
    "platform": null,
    "description": "[![Tests](https://github.com/DataShades/ckanext-create_typed_package/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/DataShades/ckanext-create_typed_package/actions)\n\n# ckanext-create_typed_package\n\n\nAdd dataset type selector to the \"Add dataset\" button.\n\n![Preview](https://github.com/DataShades/ckanext-create_typed_package/raw/master/selector.png)\n\n\n## Installation\n\nTo install ckanext-create_typed_package:\n\n1. Install the ckanext-create_typed_package Python package\n\n\t\tpip install ckanext-create-typed-package\n\n1. Add ``create_typed_package`` to the ``ckan.plugins`` setting in your CKAN\n   config file.\n\n\n## Usage\n\nThis plugin adds a widget/page for selecting dataset type when user trying to\ncreate the new dataset. Note, the plugin itself does not register any custom\ndataset types neither it introduces any tools for registering dataset types. It\nmeans, you need to create multiple dataset types manually before using this\nplugin. Check [CKAN\ndocumentation](https://docs.ckan.org/en/2.11/extensions/remote-config-update.html)\nor [ckanext-scheming](https://github.com/ckan/ckanext-scheming) if you don't\nknow how to create dataset types.\n\nFor simplicity, we'll use ckanext-scheming in this guide. Assuming you don't\nhave any custom dataset types, let's create them right now. First, install\nckanext-scheming:\n\n```sh\npip install ckanext-scheming\n```\n\nThen add `scheming_datasets` to the list of enabled plugins, alongside with\n`create_typed_package`:\n\n```ini\nckan.plugins = scheming_datasets create_typed_package\n```\n\nAnd now enable in the CKAN config file three custom dataset types that are used\nby the current extension for tests:\n\n```ini\nscheming.dataset_schemas =\n                         ckanext.create_typed_package.tests:schemas/first.yaml\n                         ckanext.create_typed_package.tests:schemas/second.yaml\n                         ckanext.create_typed_package.tests:schemas/third.yaml\n```\n\nRestart CKAN. Because of custom datset types, apart from the standard dataset\nlisting available at `/dataset/`, you also have 3 new endpoints, one per custom\ndataset type:\n\n* `/first-type`\n  ![First type listing](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/first-type-listing.png)\n* `/second-type`\n  ![First type listing](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/second-type-listing.png)\n\n* `/third-type`\n  ![Third type listing](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/third-type-listing.png)\n\n\nOn every custom page you can click **Add ???-type** and dataset creation form\nwith corresponding metadata fields will be rendered.\n\n![First type form](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/first-type-form.png)\n\nBut if you go back to main `/dataset` page and click **Add dataset** there,\nyou'll see the modal where you can select the exact type you want to create:\n\n![Selector inside modal](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/modal.png)\n\nThat's the main feature provided by the current plugin.\n\nNote, if you don't see the modal, check the JavaScript console for errors. If\nyou see no errors or cannot fix them, thre is an alternative way.\n\nModify the templates and add linkg to `/dataset/select-type`. This page does\nnot exist yet and in order to fix it, add\n`create_typed_package.use_separate_route = true` to the CKAN config file. Then\nreload the application and now you'll see dataset type selector on this new\nroute.\n\n![Selector on separate page](https://github.com/DataShades/ckanext-create_typed_package/raw/master/img/separate-route.png)\n\nThis method is more predictable but it requires modification of templates,\nthat's why modal is chosen as a default.\n\n### Dynamic options\n\nIf you need to show different options for different pages, for example, only\ntype A1 and A2 for organization AAA, only type B1 and B2 for organization BBB,\netc, you need to add [chained\naction](https://docs.ckan.org/en/2.11/extensions/plugins-toolkit.html#ckan.plugins.toolkit.ckan.plugins.toolkit.chained_action)\n`ctp_list_types`.\n\nIn your own extension that implements\n[IAction](https://docs.ckan.org/en/2.11/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IActions)\ninterface, register action `ctp_list_types` with the following code:\n\n```py\nimport ckan.plugins.toolkit as tk\nfrom ckan.types import Context\n\n@tk.chained_action\n@tk.side_effect_free\ndef ctp_list_types(next_action: Any, context: Context, data_dict: dict[str, Any]):\n\n    if tk.get_endpoint() == (\"organization\", \"read\"):\n        organization_id = tk.request.view_args.id\n        allowed_types = ... # compute types depending on organization_id\n\n        return [\n            {\"name\": item.VALUE, \"label\": item.LABEL}\n            for item in allowed_types\n        ]\n\n    return next_action(context, data_dict)\n```\n\nHere we check current endpoint using `tk.get_endpoint()`. If we are on the\norganization page, instead of all available types we return only subset of\n`allowed_types`. Note, response must be in format of list that contains\ndictionaries with `name`(actual dataset type) and `label`(human readable label\nfor `select` tag). If action returns more than one item, user sees a modal upon\nclicking **Add Dataset**. If action returns *exactly* one item, user is\nredirected to the corresponding form immediately after the click on **Add\nDataset**.\n\n## Config settings\n\n    # Build list of package types using ckanext-scheming API instead of\n\t# internal CKAN's package_type registry\n\t# (optional, default: false).\n\tcreate_typed_package.use_scheming = true\n\n\t# Additional types that are not are not automatically added to the\n\t# list for some reason\n\t# (optional, default: []).\n\tcreate_typed_package.additional_types = custom_type another_type\n\n\t# Package types that need to be excluded from the list of available\n\t# types\n\t# (optional, default: []).\n\tcreate_typed_package.exclude_types = custom_type another_type\n\n\t# After clicking on \"Add datset\" button redirect user to special\n\t# page with dataset type selector instead of using in-place modal\n\t# (optional, default: false).\n\tcreate_typed_package.use_separate_route = true\n\n\t# URL where the special page with dataset type selector will be registered.\n\t# (optional, default: /dataset/select-type).\n\tcreate_typed_package.route_path = /create-package/select-type\n\n\t# Custom label for dataset type. It will be used by `ctp_list_types`\n\t# action and, as result, by the type-picker UI widget. Labels provided in a\n\t# form `create_typed_package.label_for.<TYPE>`, where machine-name for a type\n\t# is used instead of `<TYPE>`.\n\t# (optional, default: tk._(type_machine_name)).\n\tcreate_typed_package.label_for.dataset = Publication\n",
    "bugtrack_url": null,
    "license": "AGPL",
    "summary": "Select dataset type when adding new data",
    "version": "0.5.0.post1",
    "project_urls": {
        "Homepage": "https://github.com/datashades/ckanext-create_typed_package"
    },
    "split_keywords": [
        "ckan"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9ca2e3bf62de546128118865e01ec1827271c9e6ee81f25133ded4ba2e0abdee",
                "md5": "946b9ca9d5db5c0e5323b238e6473e3f",
                "sha256": "cec6fbe24ceefd6ae107b7097dad25e2ecaba9b6a271d551416ef9f10ccb8546"
            },
            "downloads": -1,
            "filename": "ckanext_create_typed_package-0.5.0.post1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "946b9ca9d5db5c0e5323b238e6473e3f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16337,
            "upload_time": "2025-09-09T17:14:59",
            "upload_time_iso_8601": "2025-09-09T17:14:59.342407Z",
            "url": "https://files.pythonhosted.org/packages/9c/a2/e3bf62de546128118865e01ec1827271c9e6ee81f25133ded4ba2e0abdee/ckanext_create_typed_package-0.5.0.post1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f072449e27e4ef18e84adb940a2fa488b0158512aa677e1bc828972ba6e08f17",
                "md5": "6542639f7884f682c48229e04f01f0d3",
                "sha256": "7a017659ae83668e6563712e214e52d3ae4d897adf6dde407b439636db60a916"
            },
            "downloads": -1,
            "filename": "ckanext_create_typed_package-0.5.0.post1.tar.gz",
            "has_sig": false,
            "md5_digest": "6542639f7884f682c48229e04f01f0d3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 15139,
            "upload_time": "2025-09-09T17:15:00",
            "upload_time_iso_8601": "2025-09-09T17:15:00.675346Z",
            "url": "https://files.pythonhosted.org/packages/f0/72/449e27e4ef18e84adb940a2fa488b0158512aa677e1bc828972ba6e08f17/ckanext_create_typed_package-0.5.0.post1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-09 17:15:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "datashades",
    "github_project": "ckanext-create_typed_package",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "ckanext-create-typed-package"
}
        
Elapsed time: 0.94631s