Last active
January 15, 2022 20:11
-
-
Save catermelon/9137101 to your computer and use it in GitHub Desktop.
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
from flask import current_app | |
class FeatureFlags(object): | |
def __init__(self, app=None): | |
if app is not None: | |
self.init_app(app) | |
def init_app(self, app): | |
app.config.setdefault('FEATURE_FLAGS’, {}) | |
if hasattr(app, "add_template_test"): | |
app.add_template_test(self.in_config, name='active_feature') | |
else: | |
app.jinja_env.tests['active_feature'] = self.in_config | |
app.extensions['FeatureFlags'] = self | |
def in_config(self, feature): | |
try: | |
return current_app.config['FEATURE_FLAGS'][feature] | |
except (AttributeError, KeyError): | |
return False | |
def is_active(feature): | |
feature_flagger = current_app.extensions['FeatureFlags'] | |
return feature_flagger.in_config(feature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment