Package: ob-dj-feature-flags
ob-dj-feature-flagsFeature flags are a powerful technique that allows you to enable/disable specific features in your application without deploying new code. This gives you the ability to control the behavior of your application dynamically and perform A/B testing, read more about feature flagshere.ob-dj-feature-flags provides a simple way to create and manage feature flags within your Django admin panel. It also provides decorators for views and viewsets to easily control access based on feature flags.FeaturesCreate and manage feature flags within your Django admin panel.Add decorators to views and viewsets to control access based on the created feature flags.Caching mechanism to reduce database hits when checking feature flag statuses.Feature flags endpoint to use the same feature flags in your client apps.Skip feature flags during tests or in the entire project.Setup & InstallationUse pip to install ob-dj-feature-flags:pipinstallob-dj-feature-flagsAdd "ob_dj_feature_flags" to yourINSTALLED_APPSsetting like this:# settings.pyINSTALLED_APPS=[..."ob_dj_feature_flags.core.flags",]If you plan to use the created feature flags in your client apps, add the feature flags endpoint to your project's urls.py:# urls.pyurlpatterns=[...path('ob-dj-feature-flags/',include('ob_dj_feature_flags.apis.flags.urls')),]Runpython manage.py migrateto create the feature flags table.UsageCreating Feature FlagsDefine feature flags in your Django project using the provided FeatureFlag admin. Each feature flag has a unique name and an active status.Decorating ViewsUse the@action_feature_flagdecorator to control access to individual views based on feature flags. Apply the decorator to the desired view functions or class-based views. Example:fromob_dj_feature_flags.utils.decoratorsimportaction_feature_flag@action_feature_flag('my_feature_flag')defmy_view(request):# Your view logic herepassThis will check if the 'my_feature_flag' is active before allowing access to the view. If the flag is inactive, a JSON response with an error message and status code 404 will be returned.Decorating ViewsetsUse the@class_feature_flagdecorator to control access to viewsets based on feature flags. Apply the decorator to the viewset classes. Example:fromob_dj_feature_flags.utils.decoratorsimportclass_feature_flag@class_feature_flag('my_feature_flag')classMyViewSet(viewsets.ModelViewSet):# Your viewset logic herepassThis will check if the 'my_feature_flag' is active before allowing access to any actions within the viewset. If the flag is inactive, a JSON response with an error message and status code 404 will be returned.Use in Your Client AppsIntegrating feature flags into your client apps allows you to control the behavior and enable/disable specific features dynamically. To leverage feature flags in your client app, follow these simple steps:Make a GET request to the feature flags endpoint of your backend API (preferably at startup)ob-dj-feature-flags/(check step 3 of Setup & Installation section).The endpoint will provide a JSON response containing the list of feature flags along with their statuses.Store the feature flags and their statuses somewhere in your client app.Use the feature flags to control the behavior of your client app (you can create a wrapper function or a custom hook to simplify this process).Skipping Feature Flags During TestsTo skip feature flags during tests, you can use the@skip_feature_flagsdecorator. This decorator can be applied to test functions or test methods to bypass the feature flag checks within the tested views or viewsets.To use theskip_feature_flagsdecorator, import it fromob_dj_feature_flags.utils.decoratorsand apply it to your test functions or methods. Here's an example:fromob_dj_feature_flags.utils.decoratorsimportskip_feature_flagsimportpytest@skip_feature_flagsdeftest_my_view():# Test logic herepassIn this example, the@skip_feature_flagsdecorator is applied to the test function,test_my_view. This will skip the feature flag checks within the tested view, allowing the test to proceed without considering the feature flags.Skipping Feature Flags In The projectIf you want to skip feature flags in the entire app, you can add a setting to your Django project's settings module. By setting theSKIP_FEATURE_FLAGSsetting toTrue, all feature flag checks will be skipped when running the project.ConfigurationThe package provides a caching mechanism to improve performance when checking feature flag status. By default, it uses Django's caching system. You can configure the cache backend and cache timeout in your Django project settings.Intergration with feature flag management servicesYou can create a webhook endpoint to receive feature flag updates from your favorite feature flag management service. The endpoint should accept POST requests with a JSON payload containing the feature flag name and active status. Example:{"name":"my_feature_flag","active":true}