forked from ckan/ckan
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ckan#7971] docs, review suggestions
- Loading branch information
Showing
9 changed files
with
135 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
IDataDictionaryForm for extending and validating datastore data dictionary fields | ||
IDataDictionaryForm for extending and validating new keys in the `fields` | ||
dicts. Unlike the `info` free-form dict these new keys are possible to | ||
tightly control with a schema. The schema is built by combining schemas | ||
from from all plugins implementing this interface so plugins implementing | ||
different features may all contribute to the same schema. | ||
|
||
The underlying storage for data dictionary fields has changed. Use: | ||
`ckan datastore upgrade` after upgrading to this release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# encoding: utf-8 | ||
from __future__ import annotations | ||
|
||
from ckan.types import ( | ||
Context, FlattenDataDict, FlattenKey, FlattenErrorDict, | ||
) | ||
from ckan.plugins.toolkit import missing | ||
|
||
|
||
def to_datastore_plugin_data(plugin_key: str): | ||
""" | ||
Return a validator that will move values from data to | ||
context['plugin_data'][field_index][plugin_key][field_name] | ||
where field_index is the field number, plugin_key (passed to this | ||
function) is typically set to the plugin name and field name is the | ||
original field name being validated. | ||
""" | ||
|
||
def validator( | ||
key: FlattenKey, | ||
data: FlattenDataDict, | ||
errors: FlattenErrorDict, | ||
context: Context): | ||
value = data.pop(key) | ||
field_index = key[-2] | ||
field_name = key[-1] | ||
context['plugin_data'].setdefault( | ||
field_index, {}).setdefault( | ||
plugin_key, {})[field_name] = value | ||
return validator | ||
|
||
|
||
def datastore_default_current( | ||
key: FlattenKey, data: FlattenDataDict, | ||
errors: FlattenErrorDict, context: Context): | ||
'''default to currently stored value if empty or missing''' | ||
value = data[key] | ||
if value is not None and value != '' and value is not missing: | ||
return | ||
field_index = key[-2] | ||
field_name = key[-1] | ||
# current values for plugin_data are available as | ||
# context['plugin_data'][field_index]['_current'] | ||
current = context['plugin_data'].get(field_index, {}).get( | ||
'_current', {}).get('example_idatadictionaryform', {}).get( | ||
field_name) | ||
if current: | ||
data[key] = current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
============================================== | ||
Customizing the DataStore Data Dictionary Form | ||
============================================== | ||
|
||
Extensions can customize the Data Dictionary form, keys available and values | ||
stored for each column using the | ||
:py:class:`~ckanext.datastore.interfaces.IDataDictionaryForm` interface. | ||
|
||
.. autoclass:: ckanext.datastore.interfaces.IDataDictionaryForm | ||
:members: | ||
|
||
Let's add five new keys with custom validation rules to the data dictionary | ||
fields. | ||
|
||
With this plugin enabled each field in the Data Dictionary form will have | ||
an input for: | ||
|
||
- an integer value | ||
- a JSON object | ||
- a numeric value that can only be increased when edited | ||
- a "sticky" value that will not be removed if left blank | ||
- a secret value that will be stored but never displayed in the form. | ||
|
||
First extend the form template to render the form inputs: | ||
|
||
.. literalinclude:: ../../ckanext/example_idatadictionaryform/templates/datastore/snippets/dictionary_form.html | ||
|
||
We use the ``form.input`` macro to render the form fields. The name | ||
of each field starts with ``fields__`` and includes a ``position`` index | ||
because this block will be rendered once for every field in the data | ||
dictionary. | ||
|
||
The value for each input is set to either the value from ``data`` the text | ||
data passed when re-rendering a form containing errors, or ``field`` the | ||
json value (text, number, object etc.) currently stored in the data | ||
dictionary when rendering a form for the first time. | ||
|
||
The error for each field is set from ``errors``. | ||
|
||
Next we create a plugin to apply the template and validation rules for each | ||
data dictionary field key. | ||
|
||
.. literalinclude:: ../../ckanext/example_idatadictionaryform/plugin.py | ||
|
||
In ``update_datastore_create_schema`` the ``to_datastore_plugin_data`` factory | ||
generates a validator that will store our new keys as plugin data. | ||
The string passed is used to group keys for this plugin to allow multiple | ||
separate ``IDataDictionaryForm`` plugins to store data for Data Dictionary | ||
fields at the same time. It's possible to use multiple groups from the same | ||
plugin: here we use a different group for the ``secret`` key because we want | ||
to treat it differently. | ||
|
||
In ``update_datastore_info_field`` we can add keys stored as plugin data | ||
to the ``fields`` objects returned by ``datastore_info``. Here we add | ||
everything but the ``secret`` key. These values are also passed to the | ||
form template above as ``field``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters