@@ -123,33 +123,31 @@ def __init__(self, project_config):
123
123
124
124
'''
125
125
Merging typed_audiences with audiences from project_config.
126
- The typed_audiences has higher presidence .
126
+ The typed_audiences has higher precidence .
127
127
'''
128
128
129
129
typed_audiences = project_config .typed_audiences [:]
130
130
optly_typed_audiences = []
131
+ id_lookup_dict = {}
131
132
for typed_audience in typed_audiences :
132
133
optly_audience = OptimizelyAudience (
133
134
typed_audience .get ('id' ),
134
135
typed_audience .get ('name' ),
135
136
typed_audience .get ('conditions' )
136
137
)
137
138
optly_typed_audiences .append (optly_audience )
139
+ id_lookup_dict [typed_audience .get ('id' )] = typed_audience .get ('id' )
138
140
139
141
for old_audience in project_config .audiences :
140
142
# check if old_audience.id exists in new_audiences.id from typed_audiences
141
- if len ([new_audience for new_audience in project_config .typed_audiences
142
- if new_audience .get ('id' ) == old_audience .get ('id' )]) == 0 :
143
- if old_audience .get ('id' ) == "$opt_dummy_audience" :
144
- continue
145
- else :
146
- # Convert audiences lists to OptimizelyAudience array
147
- optly_audience = OptimizelyAudience (
148
- old_audience .get ('id' ),
149
- old_audience .get ('name' ),
150
- old_audience .get ('conditions' )
151
- )
152
- optly_typed_audiences .append (optly_audience )
143
+ if old_audience .get ('id' ) not in id_lookup_dict and old_audience .get ('id' ) != "$opt_dummy_audience" :
144
+ # Convert audiences lists to OptimizelyAudience array
145
+ optly_audience = OptimizelyAudience (
146
+ old_audience .get ('id' ),
147
+ old_audience .get ('name' ),
148
+ old_audience .get ('conditions' )
149
+ )
150
+ optly_typed_audiences .append (optly_audience )
153
151
154
152
self .audiences = optly_typed_audiences
155
153
@@ -193,13 +191,14 @@ def stringify_conditions(self, conditions, audiences_map):
193
191
list of conditions.
194
192
'''
195
193
ARGS = ConditionOperatorTypes .operators
196
- condition = 'OR'
194
+ operand = 'OR'
197
195
conditions_str = ''
198
196
length = len (conditions )
199
197
198
+ # Edge cases for lengths 0, 1 or 2
200
199
if length == 0 :
201
200
return ''
202
- if length == 1 :
201
+ if length == 1 and conditions [ 0 ] not in ARGS :
203
202
return '"' + self .lookup_name_from_id (conditions [0 ], audiences_map ) + '"'
204
203
if length == 2 and conditions [0 ] in ARGS and \
205
204
type (conditions [1 ]) is not list and \
@@ -209,24 +208,31 @@ def stringify_conditions(self, conditions, audiences_map):
209
208
else :
210
209
return conditions [0 ].upper () + \
211
210
' "' + self .lookup_name_from_id (conditions [1 ], audiences_map ) + '"'
211
+ # If length is 2 (where the one elemnt is a list) or greater
212
212
if length > 1 :
213
213
for i in range (length ):
214
+ # Operand is handled here and made Upper Case
214
215
if conditions [i ] in ARGS :
215
- condition = conditions [i ].upper ()
216
+ operand = conditions [i ].upper ()
216
217
else :
218
+ # Check if element is a list or not
217
219
if type (conditions [i ]) == list :
220
+ # Check if at the end or not to determine where to add the operand
221
+ # Recursive call to call stringify on embedded list
218
222
if i + 1 < length :
219
223
conditions_str += '(' + self .stringify_conditions (conditions [i ], audiences_map ) + ') '
220
224
else :
221
- conditions_str += condition + \
225
+ conditions_str += operand + \
222
226
' (' + self .stringify_conditions (conditions [i ], audiences_map ) + ')'
227
+ # Not a list so we handle as and ID to lookup no recursion needed
223
228
else :
224
229
audience_name = self .lookup_name_from_id (conditions [i ], audiences_map )
225
230
if audience_name is not None :
231
+ # Below handles all cases for one ID or greater
226
232
if i + 1 < length - 1 :
227
- conditions_str += '"' + audience_name + '" ' + condition + ' '
233
+ conditions_str += '"' + audience_name + '" ' + operand + ' '
228
234
elif i + 1 == length :
229
- conditions_str += condition + ' "' + audience_name + '"'
235
+ conditions_str += operand + ' "' + audience_name + '"'
230
236
else :
231
237
conditions_str += '"' + audience_name + '" '
232
238
0 commit comments