Store Optimizely datafile in a Django model
Project description
dj-optimizely
Introduction
Store optimizely datafile in a Django model
WORK IN PROGRESS
This app is in the very early stages and I'll be fleshing it out over the coming weeks. Though it is currently in use.
Quickstart
Install:
pip install dj-optimizely
Add djoptimizely
to INSTALLED_APPS
:
INSTALLED_APPS = [
...
'djoptimizely',
...
]
Add djoptimizely.middleware.optimizely_middleware
to MIDDLEWARE
:
MIDDLEWARE = [
...
'djoptimizely.middleware.optimizely_middleware',
...
]
Specify additional required settings:
OPTIMIZELY_ENVIRONMENT = 'staging'
OPTIMIZELY_WEBHOOK_SECRET = os.getenv('OPTIMIZELY_WEBHOOK_SECRET')
OPTIMIZELY_DATAFILE_URL = os.getenv('OPTIMIZELY_DATAFILE_URL')
Specify optional callbacks. Both functions take the request
as the first parameter:
OPTIMIZELY_USER_ID_CALLBACK = 'myapp.module.get_user_id'
OPTIMIZELY_USER_ATTRIBS_CALLBACK = 'myapp.module.get_user_attribs'
Default callbacks:
def get_user_id_default(request):
if request.user.is_authenticated:
return request.user.pk
else:
return request.session._get_or_create_session_key()
def get_user_attribs_default(request):
attribs = {
'is_authenticated': False,
'is_staff': False
}
if request.user.is_authenticated:
attribs['is_authenticated'] = True
if request.user.is_staff:
attribs['is_staff'] = True
return attribs
Add the webhook to urls.py:
path('webhooks/', include('djoptimizely.urls')),
Check if a feature should be enabled:
from djoptimizely.services import get_feature_enabled
if get_feature_enabled(request, 'cool_stuff'):
print('Cool!')
Check in a template
{% load djoptimizely_tags %}
{% show_feature 'cool_stuff' as show_cool_stuff %}
{% if show_cool_stuff %}
<p>COOL Stuff!</p>
{% endif %}
Generic view mixin:
If the specified feature_key
is not enabled
for request.user
return a 404
from djoptimizely.mixins import OptimizelyFeatureViewMixin
from django.views.generic.base import TemplateView
class CoolView(OptimizelyFeatureViewMixin, TemplateView):
feature_key = 'cool_stuff'
...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for dj_optimizely-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d0670111423d2b50cf711964a74df0831d404ad4d75b7d5a0ca079122c2a832 |
|
MD5 | b52372300e1c2fd4d6d47247d255ad89 |
|
BLAKE2b-256 | bd193633f041c537001cdf15972731c45b01564c37cf9980fd9788b3f7c2f1bf |