Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#847] Add docs for extensions custom config settings
And example with tests
- Loading branch information
Sean Hammond
committed
Dec 13, 2013
1 parent
dcf11d9
commit 86d90ee
Showing
8 changed files
with
175 additions
and
28 deletions.
There are no files selected for viewing
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
ckanext/example_iauthfunctions/plugin_v5_custom_config_setting.py
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,30 @@ | ||
import pylons.config as config | ||
|
||
import ckan.plugins as plugins | ||
import ckan.plugins.toolkit as toolkit | ||
|
||
|
||
def group_create(context, data_dict=None): | ||
|
||
# Get the value of the ckan.iauthfunctions.users_can_create_groups | ||
# setting from the CKAN config file as a string, or False if the setting | ||
# isn't in the config file. | ||
users_can_create_groups = config.get( | ||
'ckan.iauthfunctions.users_can_create_groups', False) | ||
|
||
# Convert the value from a string to a boolean. | ||
users_can_create_groups = toolkit.asbool(users_can_create_groups) | ||
|
||
if users_can_create_groups: | ||
return {'success': True} | ||
else: | ||
return {'success': False, | ||
'msg': 'Only sysadmins can create groups'} | ||
|
||
|
||
class ExampleIAuthFunctionsPlugin(plugins.SingletonPlugin): | ||
plugins.implements(plugins.IAuthFunctions) | ||
|
||
def get_auth_functions(self): | ||
return {'group_create': group_create} | ||
|
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,42 @@ | ||
========================================== | ||
Using custom config settings in extensions | ||
========================================== | ||
|
||
Extensions can define their own custom config settings that users can add to | ||
their CKAN config files to configure the behavior of the extension. | ||
|
||
Continuing with the :py:class:`~ckan.plugins.interfaces.IAuthFunctions` example | ||
from :doc:`tutorial`, let's make an alternative version of the extension that | ||
allows users to create new groups if a new config setting | ||
``ckan.iauthfunctions.users_can_create_groups`` is ``True``: | ||
|
||
.. literalinclude:: ../../ckanext/example_iauthfunctions/plugin_v5_custom_config_setting.py | ||
|
||
The ``group_create`` authorization function in this plugin uses | ||
:py:obj:`pylons.config` to read the setting from the config file, then calls | ||
:py:func:`ckan.plugins.toolkit.asbool` to convert the value from a string | ||
(all config settings values are strings, when read from the file) to a boolean. | ||
|
||
.. note:: | ||
|
||
There are also :py:func:`~ckan.plugins.toolkit.asint` and | ||
:py:func:`~ckan.plugins.toolkit.aslist` functions in the plugins toolkit. | ||
|
||
With this plugin enabled, you should find that users can create new groups if | ||
you have ``ckan.iauthfunctions.users_can_create_groups = True`` in the | ||
``[app:main]`` section of your CKAN config file. Otherwise, only sysadmin users | ||
will be allowed to create groups. | ||
|
||
.. note:: | ||
|
||
Names of config settings provided by extensions should include the name | ||
of the extension, to avoid conflicting with core config settings or with | ||
config settings from other extensions. | ||
See :ref:`extension config setting names best practice`. | ||
|
||
.. note:: | ||
|
||
The users still need to be logged-in to create groups. | ||
In general creating, updating or deleting content in CKAN requires the user | ||
to be logged-in to a registered user account, no matter what the relevant | ||
authorization function says. |
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
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