get and set enrollments from views.
chrisvxd opened this issue · comments
Chris Villa commented
Currently lacking an easy way of handling experiments in views. The easiest existing method is:
import random
from experiments.models import Experiment
from experiments.utils import WebUser
experiment = Experiment.objects.get(name="helloworld")
user = WebUser(req)
# to get an enrollment
user.get_enrollment(experiment)
# to set a random enrollment
user.set_enrollment(experiment, random.choice(experiment.alternatives.keys())
Want a dedicated function in views, or somewhere. Like record_goal()
from experiments.views import get_enrollment, set_enrollment
get_enrollment("helloworld", request)
set_enrollment("helloworld", request)
Could also supply an is_enrolled()
function:
from experiments.views import is_enrolled
is_enrolled(request, 'helloworld', 'control')
This should blow up loudly if the alternative doesn't exist.
Chris Villa commented
@matclayton what are your thoughts on this? Theo was interested in this functionality, especially is_enrolled
.
Roger Barnes commented
I've written a similar set_enrollment function to put a user into a specific alternative. I agree that this would belong in experiments.utils
def set_enrollment(request, experiment, alternative):
experiment_user = WebUser(request)
experiment_user.set_enrollment(experiment, alternative)
Theo Spears commented
The new python accessible API resolves this.