Flippy gives you a simple way to add feature flags to your Django application using a simple syntax:
# define a flag
flag_enable_chat = Flag(id="chat", name="Enable the chat feature")
# check the flag value for a specific user
chat_enabled = flag_enable_chat.get_state_for_request(request)
if chat_enabled:
...
This lets you release your new feature to your users in a slow, controlled manner.
The flags are defined in code with initial value, and later enabled via Django Admin for specific subgroups of users, with a good degree of flexibility.
Flippy tries to assume as little as possible about the type of feature flags you write. This means you can do things like:
enable_chat
.enable_chat
initially equal False
for everyone, then enable it for a group of beta-testers, then if nothing explodes - roll it out to 25% of users, then 50% users and finally for everyone! Each such action is considered a separate Rollout. flippy
in your app:pip install git+https://github.com/kos/flippy.git@master
flippy
to your INSTALLED_APPS
setting:INSTALLED_APPS = [
...
'flippy',
]
Run python manage.py migrate
to create the Rollout
model.
Create a file flags.py
somewhere in your application and define your first flag:
from flippy import Flag
flag_enable_chat = Flag(id="chat", name="Chat Feature")
from .flags import flag_enable_chat
def some_page(request):
chat_enabled = flag_enable_chat.get_state_for_request(request)
if chat_enabled:
...
Now the flag is defined in code, but it will be always False
because we haven't rolled the feature out yet.
New flags can be added very easily by instantiating the Flag
class.
The initial state of each flag can be controlled using the default
parameter:
enable_logging = Flag(id="logging", default=True)
Hint: For local development, it's convenient to have the flags enabled by default, but at the same time you'd like them to be default-off on production until rolled out. There's a simple pattern that accomplishes this:
from django.conf import settings
enable_beta_features = Flag(..., default=settings.DEBUG)
Flippy comes with some generic subjects (Django users, IP addresses) for the most common use case, but you can get the most value by defining your own subjects.
For example, assume your application has users with various subscription levels:
class User(...):
subscription_level_choices = [
(0, "Trial"),
(1, "Standard"),
(2, "Premium"),
]
subscription_level = models.IntegerField(choices=subscription_level_choices)
Here's an example Subject
implementation that lets you roll out features to all (or a percentage of) Premium users:
from flippy import Subject
class PremiumUserSubject(Subject):
def get_identifier_for_request(self, request: HttpRequest) -> Optional[str]:
user = request.user
return (
str(user.pk)
if user.is_authenticated and user.subscription_level == 2
else None
)
def __str__(self):
return "Premium Users"
As you can see, the method get_identifier_for_request
returns either None
if the subject doesn't match the request (there's no user or the user is not premium), or otherwise, the user's ID. In fact, any string is acceptable here, as long as it's different between each premium user. (The value is later used as a part of a hash, in order to ensure that percentage-based rollouts are deterministic for each user.)
The next step is to register this subject in your Django settings, so that it becomes available as an option:
# settings.py
FLIPPY_SUBJECTS = [
# standard subjects
"flippy.subject.IpAddressSubject",
"flippy.subject.UserSubject",
# custom subjects
"your_app.subjects.PremiumUserSubject",
]
Of course you're not limited to users. If your application features multi-user Accounts, you can use the same approach and write an Account
subject. In that case, the function should return a group ID instead of user ID.
Sometimes you need to query a feature flag somewhere deep in the code where you don't have access to the request
variable, because:
request
is not accessible (like models or utils) and it's not practical to pass the value all the way from the viewThis can be addressed using typed flags:
enable_sudoku = TypedFlag[User]("enable_sudoku", "Sudoku")
A typed flag, in addition to querying by request, can also be queried with an object of its declared type:
sudoku_enabled = enable_sudoku.get_state_for_object(user)
However, there's a limitation: a typed flag can only be rolled out for compatible subjects that match their type. This means that this flag can be rolled out to flippy.subject.UserSubject"
but not flippy.subject.IpAddressSubject
.
Django Admin will forbid you from creating a mismatched Rollout.
Here's an example custom Subject that could be used together with TypedFlag[Account]
in order to roll features to a given percentage of Accounts (your example custom model):
class AccountSubject(TypedSubject["Account"]):
def get_identifier_for_request(self, request: HttpRequest) -> Optional[str]:
user = request.user
if user.is_anonymous or user.account is None:
return None
return self.get_identifier_for_object(user.account)
def get_identifier_for_object(self, account) -> Optional[str]:
return str(account.pk)
def is_supported_type(self, type) -> bool:
from your_app import Account
return issubclass(type, Account)
def __str__(self):
return "Accounts"
Note that in this case, in addition to get_identifier_for_request
you also need to implement get_identifier_for_object
. It's convenient to define one in terms of the other. The method is_supported_type
is required for validation (so that Flippy can ensure the subject will be only used with matching flags).
Alpha. You mileage may vary, things may and will break. The API can change in future versions. I'm gathering feedback, so please try it out, open issues and describe what's broken or missing.