diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dcc79f7f33b..9f0fd5caa09 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -410,6 +410,10 @@ export enum FeatureFlag { } ``` +`superset/config.py` contains `DEFAULT_FEATURE_FLAGS` which will be overwritten by +those specified under FEATURE_FLAGS in `superset_config.py`. For example, `DEFAULT_FEATURE_FLAGS = { 'FOO': True, 'BAR': False }` in `superset/config.py` and `FEATURE_FLAGS = { 'BAR': True, 'BAZ': True }` in `superset_config.py` will result +in combined feature flags of `{ 'FOO': True, 'BAR': True, 'BAZ': True }`. + ## Linting Lint the project with: diff --git a/superset/__init__.py b/superset/__init__.py index 791e20bcadd..500bcaa855e 100644 --- a/superset/__init__.py +++ b/superset/__init__.py @@ -208,6 +208,18 @@ security_manager = appbuilder.sm results_backend = app.config.get('RESULTS_BACKEND') +# Merge user defined feature flags with default feature flags +feature_flags = app.config.get('DEFAULT_FEATURE_FLAGS') +feature_flags.update(app.config.get('FEATURE_FLAGS') or {}) + + +def is_feature_enabled(feature): + """ + Utility function for checking whether a feature is turned on + """ + return feature_flags.get(feature) + + # Registering sources module_datasource_map = app.config.get('DEFAULT_MODULE_DS_MAP') module_datasource_map.update(app.config.get('ADDITIONAL_MODULE_DS_MAP')) diff --git a/superset/config.py b/superset/config.py index a598d7609e0..93720c339f9 100644 --- a/superset/config.py +++ b/superset/config.py @@ -187,9 +187,12 @@ LANGUAGES = { # --------------------------------------------------- # Feature flags # --------------------------------------------------- -# Feature flags that are on by default go here. Their -# values can be overridden by those in super_config.py -FEATURE_FLAGS = {} +# Feature flags that are set by default go here. Their values can be +# overwritten by those specified under FEATURE_FLAGS in super_config.py +# For example, DEFAULT_FEATURE_FLAGS = { 'FOO': True, 'BAR': False } here +# and FEATURE_FLAGS = { 'BAR': True, 'BAZ': True } in superset_config.py +# will result in combined feature flags of { 'FOO': True, 'BAR': True, 'BAZ': True } +DEFAULT_FEATURE_FLAGS = {} # --------------------------------------------------- # Image and file configuration diff --git a/superset/views/base.py b/superset/views/base.py index 6b71ff0c922..f0f5fbf7e62 100644 --- a/superset/views/base.py +++ b/superset/views/base.py @@ -31,7 +31,7 @@ from flask_babel import lazy_gettext as _ import simplejson as json import yaml -from superset import conf, db, security_manager +from superset import conf, db, feature_flags, security_manager from superset.exceptions import SupersetException, SupersetSecurityException from superset.translations.utils import get_language_pack from superset.utils import core as utils @@ -157,7 +157,7 @@ class BaseSupersetView(BaseView): 'conf': {k: conf.get(k) for k in FRONTEND_CONF_KEYS}, 'locale': locale, 'language_pack': get_language_pack(locale), - 'feature_flags': conf.get('FEATURE_FLAGS'), + 'feature_flags': feature_flags, } diff --git a/tests/base_tests.py b/tests/base_tests.py index f3029c020cf..1f3b4c5a95a 100644 --- a/tests/base_tests.py +++ b/tests/base_tests.py @@ -19,10 +19,10 @@ import json import unittest from flask_appbuilder.security.sqla import models as ab_models -from mock import Mock +from mock import Mock, patch import pandas as pd -from superset import app, db, security_manager +from superset import app, db, is_feature_enabled, security_manager from superset.connectors.druid.models import DruidCluster, DruidDatasource from superset.connectors.sqla.models import SqlaTable from superset.models import core as models @@ -185,3 +185,11 @@ class SupersetTestCase(unittest.TestCase): if raise_on_error and 'error' in resp: raise Exception('run_sql failed') return resp + + @patch.dict('superset.feature_flags', {'FOO': True}, clear=True) + def test_existing_feature_flags(self): + self.assertTrue(is_feature_enabled('FOO')) + + @patch.dict('superset.feature_flags', {}, clear=True) + def test_nonexistent_feature_flags(self): + self.assertFalse(is_feature_enabled('FOO'))