Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
views.py in django app that runs planout
from django.shortcuts import render, HttpResponse
from django.http import HttpResponseNotAllowed, HttpResponseBadRequest
import logging
from planout.experiment import SimpleExperiment
from planout.experiment import DefaultExperiment
from planout.namespace import Namespace
from planout.namespace import SimpleNamespace
from planout.ops.random import *
import json
logger = logging.getLogger(__name__)
class ExcerptExperiment(SimpleExperiment):
def assign(self, params, userid):
params.name = 'excerpt'
params.show_excerpt = UniformChoice(
choices=[True, False],
unit=userid)
params.data = { 'show_excerpt': params.show_excerpt };
class ReadingHabitsFeedbackExperiment(SimpleExperiment):
def assign(self, params, userid):
params.name = 'reading-habits'
params.icon = UniformChoice(
choices=['2713', '2717'],
unit=userid)
params.color = UniformChoice(
choices=['#B8B8CB', '#898989'],
unit=userid)
params.bg_color = UniformChoice(
choices=[[223, 223, 223], [199, 199, 199]],
unit=userid)
params.data = { 'icon': params.icon,
'color': params.color,
'bg_color': params.bg_color}
class DefaultDerpExperiment(DefaultExperiment):
def get_default_params(self):
return {'name': 'default', 'data': {} }
class TheOneTrueNamespace(SimpleNamespace):
def setup(self):
self.name = 'the_one_true_namespace'
self.primary_unit = 'userid'
self.num_segments = 200
self.default_experiment_class = DefaultDerpExperiment
def setup_experiments(self):
self.add_experiment('excerpt', ExcerptExperiment, 100)
self.add_experiment('reading-habits', ReadingHabitsFeedbackExperiment, 100)
def home(request):
if request.method == 'GET':
user_id = request.GET.get('user_id', '')
if user_id == '':
response = HttpResponseBadRequest(
'Please include a user_id (string) query parameter.')
else:
exp = TheOneTrueNamespace(userid=user_id)
response_data = { 'name': exp.get('name'),
'in_experiment': exp.in_experiment,
'data': exp.get('data')
}
response = HttpResponse(json.dumps(response_data), content_type="application/json")
else:
response = HttpResponseNotAllowed('Only GET requests, please.')
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.