<s> from __future__ import absolute_import <EOL> import logging <EOL> l = logging . getLogger ( __name__ ) <EOL> from django import template <EOL> from django_lean . experiments . models import Experiment <EOL> from django_lean . experiments . utils import WebUserFactory <EOL> register = template . Library ( ) <EOL> class BaseExperimentNode ( template . Node ) : <EOL> def __init__ ( self , user_factory = WebUserFactory ( ) ) : <EOL> self . __user_factory = user_factory <EOL> def create_user ( self , context ) : <EOL> return self . __user_factory . create_user ( context ) <EOL> def get_user ( self , context ) : <EOL> request = context . get ( '<STR_LIT>' , None ) <EOL> if request is None : <EOL> return self . create_user ( context ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . experiment_user = self . create_user ( context ) <EOL> return request . experiment_user <EOL> class ExperimentNode ( BaseExperimentNode ) : <EOL> def __init__ ( self , node_list , experiment_name , group_name , user_factory ) : <EOL> BaseExperimentNode . __init__ ( self , user_factory ) <EOL> self . node_list = node_list <EOL> self . experiment_name = experiment_name <EOL> self . group_name = group_name <EOL> def render ( self , context ) : <EOL> user = self . get_user ( context ) <EOL> should_render = False <EOL> if self . group_name == "<STR_LIT:test>" : <EOL> should_render = Experiment . test ( self . experiment_name , user ) <EOL> elif self . group_name == "<STR_LIT>" : <EOL> should_render = Experiment . control ( self . experiment_name , user ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % <EOL> self . group_name ) <EOL> if should_render : <EOL> return self . node_list . render ( context ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def experiment ( parser , token , user_factory = WebUserFactory ( ) ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> tag_name , experiment_name , group_name = token . split_contents ( ) <EOL> node_list = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return ExperimentNode ( node_list , experiment_name , group_name , user_factory ) <EOL> class ClientSideExperimentNode ( BaseExperimentNode ) : <EOL> CONTEXT_KEY = "<STR_LIT>" <EOL> def __init__ ( self , experiment_name , user_factory ) : <EOL> BaseExperimentNode . __init__ ( self , user_factory ) <EOL> self . experiment_name = experiment_name <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . CONTEXT_KEY not in context : <EOL> context [ self . CONTEXT_KEY ] = { } <EOL> if self . experiment_name not in context [ self . CONTEXT_KEY ] : <EOL> user = self . create_user ( context ) <EOL> group = None <EOL> if Experiment . test ( self . experiment_name , user ) : <EOL> group = "<STR_LIT:test>" <EOL> elif Experiment . control ( self . experiment_name , user ) : <EOL> group = "<STR_LIT>" <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % <EOL> self . experiment_name ) <EOL> context [ self . CONTEXT_KEY ] [ self . experiment_name ] = group <EOL> return "<STR_LIT>" <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def clientsideexperiment ( parser , token , user_factory = WebUserFactory ( ) ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> tag_name , experiment_name = token . split_contents ( ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return ClientSideExperimentNode ( experiment_name , user_factory ) </s>
