Package: django-flippy
django-flippyAFlipper Cloudpackage for Django apps.
(Not affiliated with the Flipper Cloud folks, just think they're a good idea.)
Feature flags for Django projects, because why should the Rails people have all the fun?Installation# pip install django-flippyNext opensettings.py.
You need to add Flippy to your installed apps, middleware, and template context processors.# settings.pyINSTALLED_APPS=[...,'flippy',# <-- the app]MIDDLEWARE=[...,'flippy.middleware.flippy_middleware',# <-- the middleware]TEMPLATES=[{...,'OPTIONS':{'context_processors':[...,'flippy.context.processor',# <-- the context processor],},},]Finally,./manage.py migrateto run the migrations.UsageRaw, end-to-end examplefromdjango.contrib.auth.modelsimportUserfromflippyimportFlippyfromflippy.backendsimportDjangoBackendf=Flippy(DjangoBackend())# create a featuref.create('my_cool_feature')# enable for half of all usersf.enable_percentage_of_actors('my_cool_feature',50)# check if a given user should get the feature# (depending on chance, the user may or may not be flagged in -- if so,# crank the percentage_of_actors up to 100 if you want to confirm it worked)u=User.objects.get(pk=1)iff.is_enabled('my_cool_feature',u):print('This user gets the cool new feature.')else:print('This user gets the old, less cool feature.')In a view# views.pydefindex(request):ifrequest.flippy.feature_exists('my_cool_feature')user_has_feature=request.flippy.is_enabled('my_cool_feature',request.user)ifuser_has_feature:returnrender(request,'cool_new_feature_index.html')returnrender(request,'index.html')In a template# views.pydefindex(request):returnrender(request,'index.html')<!-- index.html, abridged -->{% if flippy.my_cool_feature.for_user %}<p>You have access to the cool new feature!</p>{% else %}<p>Nothing new to see here.</p>{% endif %}Using Flipper CloudThe above recipes only use the local Django-based backend and does not connect you to
Flipper Cloud. There is aFlipperCloudBackendwhich expects a Flipper Cloud token
passed in the constructor. Be warned: this backend makes direct API calls for every
operation.The better way to use Flipper Cloud is to set yourself up to use the Django backend.
Then run this periodically:FLIPPER_CLOUD_TOKEN=mytoken python manage.py sync-from-cloudThat will sync your Flipper Cloud data down to your local Django backend.The raw wayfromflippyimportFlippyfromflippy.backendsimportFlipperCloudBackendf=Flippy(FlipperCloudBackend('MY-TOKEN-HERE'))Using settings.pyIn addition to the setup you did for Installation, add the following to yoursettings.py:# settings.pyFLIPPY_BACKEND='flippy.backends.FlipperCloudBackend'FLIPPY_ARGS=['MY-TOKEN-HERE']This will configure the Flipper Cloud backend everywhere, including the middleware (request.flippy) and context processor ({% if flippy.foo.for_user %}).TestingWe test withpytest.
By default, tests which require an active Flipper Cloud account aren't run.
You can run them withpytest -m flippercloud.
Note that you must setFLIPPER_CLOUD_TOKENin your environment for them to run.Creating and testing a new backendYou may want to store your feature data differently. You can model your backend
off offlippy.backends.MemoryBackend, extendingflippy.core.BaseBackendand
implementing all the methods. That interface is howFlippy(the user side) will
expect to interact with you. You can take a look attests/memory_backend_test.pyto see how to run a suite of tests against your implementation.