Merge pull request #48 from aarranz/feature/env-vars

Add support for configuring the plugin using environment variables
This commit is contained in:
Francisco de la Vega 2018-07-17 16:44:14 +02:00 committed by GitHub
commit cc776c68d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 13 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2014 - 2017 CoNWeT Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.
# This file is part of CKAN Private Dataset Extension.
@ -21,6 +22,7 @@ from __future__ import absolute_import
import importlib
import logging
import os
import ckan.plugins as plugins
@ -151,7 +153,7 @@ def _process_package(context, request_data):
plugins.toolkit.check_access(method, context, request_data)
# Get the parser from the configuration
class_path = plugins.toolkit.config.get(PARSER_CONFIG_PROP, '')
class_path = os.environ.get(PARSER_CONFIG_PROP.upper().replace('.', '_'), plugins.toolkit.config.get(PARSER_CONFIG_PROP, ''))
if class_path != '':
try:

8
ckanext/privatedatasets/helpers.py Executable file → Normal file
View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2015 CoNWeT Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.
# This file is part of CKAN Private Dataset Extension.
@ -20,6 +21,7 @@
from __future__ import absolute_import
import logging
import os
from ckan.common import request
import ckan.model as model
@ -64,9 +66,9 @@ def can_read(pkg_dict):
def get_config_bool_value(config_name, default_value=False):
value = tk.config.get(config_name, default_value)
value = value if type(value) == bool else value != 'False'
return value
env_name = config_name.upper().replace('.', '_')
value = os.environ.get(env_name, tk.config.get(config_name, default_value))
return value if type(value) == bool else value.strip().lower() in ('true', '1', 'on')
def show_acquire_url_on_create():

0
ckanext/privatedatasets/plugin.py Executable file → Normal file
View File

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.
# This file is part of CKAN Private Dataset Extension.
@ -20,7 +21,7 @@
import unittest
import ckanext.privatedatasets.helpers as helpers
from mock import MagicMock
from mock import patch, MagicMock
from parameterized import parameterized
@ -127,26 +128,60 @@ class HelpersTest(unittest.TestCase):
helpers.tk.check_access.assert_called_once_with('package_show', context, package)
@parameterized.expand([
(None, False),
('True', True),
('False', False)
(None, False, None),
('True', True, None),
('False', False, None),
('afa ', False, None),
(True, True, None),
(False, False, None),
(False, True , 'true'),
(False, True , 'on'),
(False, True , '1'),
(True, False, '0'),
(True, False, 'off'),
(True, False, 'fAlsE'),
(True, False, 'fAlsE'),
])
def test_show_acquire_url_on_create(self, config_value, expected_value):
@patch("ckanext.privatedatasets.helpers.os.environ", new={})
def test_show_acquire_url_on_create(self, config_value, expected_value, env_val):
# {} is shared between tests, so we have clear it each time
helpers.os.environ.clear()
if config_value is not None:
helpers.tk.config['ckan.privatedatasets.show_acquire_url_on_create'] = config_value
if env_val:
helpers.os.environ['CKAN_PRIVATEDATASETS_SHOW_ACQUIRE_URL_ON_CREATE'] = env_val
# Call the function
self.assertEquals(expected_value, helpers.show_acquire_url_on_create())
@parameterized.expand([
(None, False),
('True', True),
('False', False)
(None, False, None),
('True', True, None),
(' tRUe', True, None),
('False', False, None),
(True, True, None),
(False, False, None),
(False, True , 'trUe'),
(False, True , 'on'),
(False, True , '1'),
(True, False, '0'),
(True, False, 'off'),
(True, False, 'fAlsE'),
(True, False, 'potato'),
])
def test_show_acquire_url_on_edit(self, config_value, expected_value):
@patch("ckanext.privatedatasets.helpers.os.environ", new={})
def test_show_acquire_url_on_edit(self, config_value, expected_value, env_val):
# {} is shared between tests, so we have clear it each time
helpers.os.environ.clear()
if config_value is not None:
helpers.tk.config['ckan.privatedatasets.show_acquire_url_on_edit'] = config_value
if env_val:
helpers.os.environ['CKAN_PRIVATEDATASETS_SHOW_ACQUIRE_URL_ON_EDIT'] = env_val
# Call the function
self.assertEquals(expected_value, helpers.show_acquire_url_on_edit())