1
1
from ...optimizely import Optimizely
2
2
3
+ from . import utils
4
+
3
5
4
6
class DjangoOptimizely (Optimizely ):
5
7
"""
@@ -13,98 +15,63 @@ class DjangoOptimizely(Optimizely):
13
15
don't accidentally get unnecessarily shared. Additional fields can be added in settings.
14
16
"""
15
17
16
- def _get_fields_for_attributes_for_model (self , model_class ):
17
- """
18
-
19
- :param model_class:
20
- :return:
21
- """
22
- return (model_class ._meta .pk ,)
23
-
24
- def _format_attribute_key_for_field (self , field ):
25
- return '{field.model._meta.label}:{field.name}' .format (field = field )
26
-
27
- def _get_model_instance_id_and_attributes (self , model_instance , attribute_overrides = None ):
28
- """
29
- Helper for forming id + attributes that get passed in.
30
-
31
- :param model_instance: Django model instance
32
- :param attribute_overrides dict
33
- :return: (str, dict)
34
- str will be of the form 'app_label.ModelClass:{model_instance.pk}'
35
- dict will be a generated dict of field names and values based on a list
36
- of attributes provided in settings.:
37
- {'users.User:id': 12345,
38
- 'users.User:first_name': 'Michael',
39
- 'users.User:last_name': 'Bluth',
40
- 'users.User:email': 'michael@thebluthcompany.com'}
41
- """
42
- formatted_user_id = '{instance._meta.label}:{instance.pk}' .format (instance = model_instance )
43
- attributes = {}
44
- for field in self ._get_fields_for_attributes_for_model (model_instance ):
45
- attribute_key = self ._format_attribute_key_for_field (field )
46
- attributes [attribute_key ] = getattr (model_instance , field .attname )
47
- if attribute_overrides :
48
- attributes .update (attribute_overrides )
49
- return formatted_user_id , attributes
50
-
51
18
def activate (self , experiment_key , user_id , attributes = None ):
52
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
19
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
53
20
return super (DjangoOptimizely , self ).activate (experiment_key , formated_user_id ,
54
21
attributes = formatted_attributes )
55
22
56
23
def track (self , event_key , user_id , attributes = None , event_tags = None ):
57
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
24
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
58
25
return super (DjangoOptimizely , self ).track (event_key , formated_user_id ,
59
26
attributes = formatted_attributes , event_tags = event_tags )
60
27
61
28
def is_feature_enabled (self , feature_key , user_id , attributes = None ):
62
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
29
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
63
30
return super (DjangoOptimizely , self ).is_feature_enabled (feature_key , formated_user_id ,
64
31
attributes = formatted_attributes )
65
32
66
33
def get_variation (self , experiment_key , user_id , attributes = None ):
67
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
34
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
68
35
return super (DjangoOptimizely , self ).get_variation (experiment_key , formated_user_id ,
69
36
attributes = formatted_attributes )
70
37
71
38
def get_enabled_features (self , user_id , attributes = None ):
72
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
39
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
73
40
return super (DjangoOptimizely , self ).get_enabled_features (formated_user_id , attributes = formatted_attributes )
74
41
75
42
def get_feature_variable (self , feature_key , variable_key , user_id , attributes = None ):
76
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
43
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
77
44
return super (DjangoOptimizely , self ).get_feature_variable (feature_key , variable_key ,
78
45
formated_user_id , attributes = formatted_attributes )
79
46
80
47
def get_feature_variable_boolean (self , feature_key , variable_key , user_id , attributes = None ):
81
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
48
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
82
49
return super (DjangoOptimizely , self ).get_feature_variable_boolean (feature_key , variable_key ,
83
50
formated_user_id ,
84
51
attributes = formatted_attributes )
85
52
86
53
def get_feature_variable_double (self , feature_key , variable_key , user_id , attributes = None ):
87
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
54
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
88
55
return super (DjangoOptimizely , self ).get_feature_variable_double (feature_key , variable_key ,
89
56
formated_user_id ,
90
57
attributes = formatted_attributes )
91
58
92
59
def get_feature_variable_integer (self , feature_key , variable_key , user_id , attributes = None ):
93
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
60
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
94
61
return super (DjangoOptimizely , self ).get_feature_variable_integer (feature_key , variable_key ,
95
62
formated_user_id ,
96
63
attributes = formatted_attributes )
97
64
98
65
def get_feature_variable_string (self , feature_key , variable_key , user_id , attributes = None ):
99
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id , attributes )
66
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id , attributes )
100
67
return super (DjangoOptimizely , self ).get_feature_variable_string (feature_key , variable_key ,
101
68
formated_user_id ,
102
69
attributes = formatted_attributes )
103
70
104
71
def get_forced_variation (self , experiment_key , user_id ):
105
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id )
72
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id )
106
73
return super (DjangoOptimizely , self ).get_forced_variation (experiment_key , formated_user_id )
107
74
108
75
def set_forced_variation (self , experiment_key , user_id , variation_key ):
109
- formated_user_id , formatted_attributes = self . _get_model_instance_id_and_attributes (user_id )
76
+ formated_user_id , formatted_attributes = utils . model_instance_id_and_attributes (user_id )
110
77
return super (DjangoOptimizely , self ).set_forced_variation (experiment_key , formated_user_id , variation_key )
0 commit comments