Skip to content

Commit

Permalink
[#2429] config_option_show action.
Browse files Browse the repository at this point in the history
Sysadmins can call this action with a key (eg. 'ckan.site_title') to
retrieve a config value. The value is returned either from the
system_info table of the database, or secondly, from the config object
as defined in the ini file. Only config options in the schema
whitelisted as editable will be returned. Non-editable options, or non
existing options, will return a ValidationError.
  • Loading branch information
brew committed May 20, 2015
1 parent 8ccc6b2 commit 9784aba
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
40 changes: 40 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -3361,3 +3361,43 @@ def help_show(context, data_dict):
raise NotFound('Action function not found')

return function.__doc__


def config_option_show(context, data_dict):
'''Show the current value of a particular configuration option.
Only return config options that can be updated with the
`config_option_update` action.
:param id: The configuration option key
:type id: string
:returns: The value of the config option from either the system_info table
or ini file.
:rtype: string
:raises: :class:`ckan.logic.ValidationError`: if config option is not in
the schema (whitelisted as editable).
'''

model = context['model']

_check_access('config_option_show', context, data_dict)

key = _get_or_bust(data_dict, 'key')

schema = ckan.logic.schema.default_show_configuration_schema()

This comment has been minimized.

Copy link
@amercader

amercader May 20, 2015

Member

@brew We want to check which config options can be edited to decide which ones to show, so I wouldn't create a dedicated show schema, as it will be confusing. Also we should now use ckan.logic.schema.update_config_schema, which will incorporate changes from extensions.


# Only return whitelisted keys
if key not in schema:
raise ValidationError(
'Configuration option \'{0}\' can not be shown'.format(key))

# return the value from system_info or config
default_value = config.get(key, None)
if key in schema.keys():

This comment has been minimized.

Copy link
@amercader

amercader May 20, 2015

Member

@brew If everything went right (which it should) config and the db should have the same value, as both are set when updating a config option and on startup. Also this check is always true isn't it? You've already check that key is in schema.

This comment has been minimized.

Copy link
@brew

brew May 20, 2015

Author Member

Yes, you're right I don't need the key check there. I think I still need to get the default_value from the config in cases where this value isn't in the system_info table.

This comment has been minimized.

Copy link
@brew

brew May 20, 2015

Author Member

Or just return it from the config, and skip getting it from the table altogether?

This comment has been minimized.

Copy link
@amercader

amercader May 20, 2015

Member

+1

value = model.get_system_info(key, default=default_value)
else:
value = default_value

return value
5 changes: 5 additions & 0 deletions ckan/logic/auth/get.py
Expand Up @@ -309,3 +309,8 @@ def request_reset(context, data_dict):

def help_show(context, data_dict):
return {'success': True}


def config_option_show(context, data_dict):
'''Show configuration option. Only sysadmins.'''
return {'success': False}
5 changes: 5 additions & 0 deletions ckan/logic/schema.py
Expand Up @@ -679,3 +679,8 @@ def default_update_configuration_schema():
validators.insert(0, ignore_missing)

return schema


def default_show_configuration_schema():
schema = default_update_configuration_schema()
return schema
2 changes: 1 addition & 1 deletion ckan/tests/config/test_environment.py
Expand Up @@ -55,7 +55,7 @@ def test_update_config_env_vars(self):
'my-site')

def test_update_config_db_url_precidence(self):
'''CKAN_SQLALCHEMY_URL in the env takes precidence over CKAN_DB'''
'''CKAN_SQLALCHEMY_URL in the env takes precedence over CKAN_DB'''
os.environ.setdefault('CKAN_DB', 'postgresql://mydeprectatesqlurl/')
os.environ.setdefault('CKAN_SQLALCHEMY_URL',
'postgresql://mynewsqlurl/')
Expand Down
32 changes: 31 additions & 1 deletion ckan/tests/logic/action/test_get.py
Expand Up @@ -2,7 +2,6 @@

import ckan.logic as logic
import ckan.plugins as p
import ckan.lib.search as search
import ckan.tests.helpers as helpers
import ckan.tests.factories as factories

Expand Down Expand Up @@ -1487,6 +1486,37 @@ def test_help_show_not_found(self):
helpers.call_action, 'help_show', name=function_name)


class TestConfigOptionShow(helpers.FunctionalTestBase):

@helpers.change_config('ckan.site_title', 'My Test CKAN')
def test_config_option_show_in_config_not_in_db(self):
'''config_option_show returns value from config when value on in
system_info table.'''

title = helpers.call_action('config_option_show',
key='ckan.site_title')
nose.tools.assert_equal(title, 'My Test CKAN')

@helpers.change_config('ckan.site_title', 'My Test CKAN')
def test_config_option_show_in_config_and_in_db(self):
'''config_option_show returns value from db when value is in both
config and system_info table.'''

factories.SystemInfo(key='ckan.site_title', value='Site Title in DB')

title = helpers.call_action('config_option_show',
key='ckan.site_title')
nose.tools.assert_equal(title, 'Site Title in DB')

@helpers.change_config('ckan.not.editable', 'My non editable option')
def test_config_option_show_not_whitelisted_key(self):
'''config_option_show raises exception if key is not a whitelisted
config option.'''

nose.tools.assert_raises(logic.ValidationError, helpers.call_action,
'config_option_show', key='ckan.not.editable')


def remove_pseudo_users(user_list):
pseudo_users = set(('logged_in', 'visitor'))
user_list[:] = [user for user in user_list
Expand Down

0 comments on commit 9784aba

Please sign in to comment.