|
1 | 1 | # ob-dj-feature-flags
|
2 | 2 |
|
3 |
| -ob-dj-feature-flags is a Django package for managing feature flags and controlling access to Django views & API endpoints using decorators. |
| 3 | +Feature 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 flags [here](https://www.atlassian.com/continuous-delivery/principles/feature-flags). |
| 4 | + |
| 5 | +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. |
4 | 6 |
|
5 | 7 | ## Features
|
6 | 8 |
|
7 | 9 | - Create and manage feature flags within your Django admin panel.
|
8 |
| -- Decorators for views and viewsets to easily control access based on feature flags. |
9 |
| -- Caching mechanism to improve performance when checking feature flag status. |
10 |
| -- Management command to scan viewsets and actions to populate the feature flags automatically (coming soon). |
11 |
| - |
12 |
| -### Overview |
| 10 | +- Add decorators to views and viewsets to control access based on the created feature flags. |
| 11 | +- Caching mechanism to reduce database hits when checking feature flag statuses. |
| 12 | +- Feature flags endpoint to use the same feature flags in your client apps. |
| 13 | +- Skip feature flags during tests or in the entire project. |
13 | 14 |
|
14 |
| -```python |
15 |
| -from ob_dj_feature_flags.utils.decorators import class_feature_flag, action_feature_flag |
16 |
| - |
17 |
| -@class_feature_flag("todos") |
18 |
| -class TodosViewSet( |
19 |
| - mixins.CreateModelMixin, |
20 |
| - mixins.ListModelMixin, |
21 |
| - viewsets.GenericViewSet, |
22 |
| -): |
23 |
| - permission_classes = [ |
24 |
| - permissions.AllowAny, |
25 |
| - ] |
26 |
| - queryset = Todo.objects.all() |
27 |
| - serializer_class = TodosSerializer |
28 |
| - |
29 |
| - @action_feature_flag("todos_list") |
30 |
| - def list(self, request, *args, **kwargs): |
31 |
| - return super().list(request, *args, **kwargs) |
32 |
| - |
33 |
| - @action_feature_flag("todos_create") |
34 |
| - def create(self, request, *args, **kwargs): |
35 |
| - return super().create(request, *args, **kwargs) |
36 |
| -``` |
| 15 | +## Setup & Installation |
37 | 16 |
|
38 |
| -In this example, the TodosViewSet class is decorated with `@class_feature_flag("todos")`, which checks if the 'todos' feature flag is active before allowing access to any actions within the viewset. |
| 17 | +1. Use pip to install ob-dj-feature-flags: |
39 | 18 |
|
40 |
| -Additionally, the list() method is decorated with `@action_feature_flag("todos_list")`, which checks if the 'todos_list' feature flag is active before allowing access to the list action. Similarly, the create() method is decorated with `@action_feature_flag("todos_create")`, which checks if the 'todos_create' feature flag is active before allowing access to the create action. |
| 19 | +```shell |
| 20 | +pip install ob-dj-feature-flags |
| 21 | +``` |
41 | 22 |
|
42 |
| -By combining both class-level and action-level feature flags, you can control access to the entire viewset based on the 'todos' flag, as well as control access to specific actions within the viewset based on the corresponding flags ('todos_list' and 'todos_create' in this example). |
| 23 | +2. Add "ob_dj_feature_flags" to your `INSTALLED_APPS` setting like this: |
43 | 24 |
|
44 |
| -## Installation |
| 25 | +```python |
| 26 | + # settings.py |
| 27 | + INSTALLED_APPS = [ |
| 28 | + ... |
| 29 | + "ob_dj_feature_flags.core.flags", |
| 30 | + ] |
| 31 | +``` |
45 | 32 |
|
46 |
| -Use pip to install ob-dj-feature-flags: |
| 33 | +3. If you plan to use the created feature flags in your client apps, add the feature flags endpoint to your project's urls.py: |
47 | 34 |
|
48 |
| -```shell |
49 |
| -pip install ob-dj-feature-flags |
| 35 | +```python |
| 36 | + # urls.py |
| 37 | + urlpatterns = [ |
| 38 | + ... |
| 39 | + path('ob-dj-feature-flags/', include('ob_dj_feature_flags.apis.flags.urls')), |
| 40 | + ] |
50 | 41 | ```
|
51 | 42 |
|
| 43 | +4. Run `python manage.py migrate` to create the feature flags table. |
| 44 | + |
52 | 45 | ## Usage
|
53 | 46 |
|
54 | 47 | ### Creating Feature Flags
|
55 | 48 |
|
56 | 49 | Define feature flags in your Django project using the provided FeatureFlag admin. Each feature flag has a unique name and an active status.
|
57 | 50 |
|
58 |
| - |
59 | 51 | ### Decorating Views
|
60 | 52 |
|
61 | 53 | Use the `@action_feature_flag` decorator to control access to individual views based on feature flags. Apply the decorator to the desired view functions or class-based views. Example:
|
@@ -90,9 +82,9 @@ This will check if the 'my_feature_flag' is active before allowing access to any
|
90 | 82 |
|
91 | 83 | Integrating 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:
|
92 | 84 |
|
93 |
| -1. At the startup time of your client app make a GET request to the feature flags endpoint of your backend API (`/flags/` by default). |
| 85 | +1. 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). |
94 | 86 | 2. The endpoint will provide a JSON response containing the list of feature flags along with their statuses.
|
95 |
| -3. Store the feature flags and their statuses in your client app. |
| 87 | +3. Store the feature flags and their statuses somewhere in your client app. |
96 | 88 | 4. 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).
|
97 | 89 |
|
98 | 90 | ## Skipping Feature Flags During Tests
|
|
0 commit comments