File tree Expand file tree Collapse file tree 5 files changed +60
-1
lines changed
optimizely/integrations/django Expand file tree Collapse file tree 5 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 1
1
from django .apps import AppConfig
2
2
3
+ from ... import integrations
3
4
from . import sdk
4
5
from .settings import optimizely_settings
5
6
6
7
7
8
class OptimizelyAppConfig (AppConfig ):
8
- name = 'optimizely'
9
+ name = 'optimizely.integrations.django'
10
+
11
+ def __init__ (self , app_name , app_module ):
12
+ super (OptimizelyAppConfig , self ).__init__ (app_name , app_module )
13
+ self .path = integrations .django .__path__ [0 ]
9
14
10
15
def ready (self ):
11
16
optimizely_sdk .refresh ()
Original file line number Diff line number Diff line change
1
+ from django .core .management .base import BaseCommand , CommandError
2
+ import requests
3
+
4
+ from ...settings import optimizely_settings
5
+ from ... import utils
6
+
7
+ class Command (BaseCommand ):
8
+ def handle (self , * args , ** options ):
9
+ auth_headers = {'Authorization' : 'Bearer {}' .format (optimizely_settings .PERSONAL_ACCESS_TOKEN )}
10
+ resp = requests .get ('https://api.optimizely.com/v2/attributes' ,
11
+ headers = auth_headers ,
12
+ params = {'project_id' : optimizely_settings .PROJECT_ID ,
13
+ 'per_page' : 100 })
14
+ # TODO: account for multiple pages of this in the future
15
+ keys = {attribute ['key' ] for attribute in resp .json ()}
16
+ for feature_flag_model in optimizely_settings .FEATURE_FLAG_MODELS .keys ():
17
+ fields = utils .fields_for_attributes_for_model (feature_flag_model )
18
+ for field in fields :
19
+ attribute_key = utils .attribute_key_for_field (field )
20
+ if attribute_key not in keys :
21
+ data = {'project_id' : int (optimizely_settings .PROJECT_ID ),
22
+ 'key' : attribute_key ,
23
+ 'archived' : False ,
24
+ 'name' : '{}: {}' .format (feature_flag_model ._meta .verbose_name .capitalize (),
25
+ field .verbose_name .capitalize ())}
26
+ resp = requests .post ('https://api.optimizely.com/v2/attributes' ,
27
+ headers = auth_headers ,
28
+ json = data )
29
+ resp .raise_for_status ()
Original file line number Diff line number Diff line change
1
+ from importlib import import_module
1
2
import os
3
+
4
+ from django .apps import apps
2
5
from django .conf import settings
3
6
4
7
8
11
9
12
DEFAULTS = {
10
13
'SDK_KEY' : os .environ .get ('OPTIMIZELY_SDK_KEY' ),
14
+ 'PROJECT_ID' : os .environ .get ('OPTIMIZELY_PROJECT_ID' ),
15
+ 'PERSONAL_ACCESS_TOKEN' : os .environ .get ('OPTIMIZELY_PERSONAL_ACCESS_TOKEN' ),
16
+ 'FEATURE_FLAG_MODELS' : {
17
+ settings .AUTH_USER_MODEL : {},
18
+ },
19
+ 'STORAGE_STRATEGY' : 'optimizely.integrations.django.storage.DjangoORMDatafileStorage' ,
11
20
}
12
21
22
+ IMPORT_STRINGS = (
23
+ 'STORAGE_STRATEGY' ,
24
+ )
25
+
26
+
27
+ def import_from_string (path ):
28
+ module_path , class_name = path .rsplit ('.' , 1 )
29
+ module = import_module (module_path )
30
+ return getattr (module , class_name )
31
+
13
32
14
33
class OptimizelySettings (object ):
15
34
def __init__ (self ):
@@ -26,6 +45,12 @@ def __getattr__(self, attr):
26
45
except KeyError :
27
46
val = DEFAULTS [attr ]
28
47
48
+ if attr in IMPORT_STRINGS :
49
+ val = import_from_string (val )
50
+
51
+ if attr == 'FEATURE_FLAG_MODELS' :
52
+ val = {apps .get_model (k ): v for k , v in val .items ()}
53
+
29
54
setattr (self , attr , val )
30
55
return val
31
56
You can’t perform that action at this time.
0 commit comments