Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 5dbff32

Browse files
committed
Add ET_Filter to create more complex queries
1 parent 8b2e94a commit 5dbff32

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

FuelSDK/rest.py

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,62 @@ def __init__(self, auth_stub, obj_type, props = None, update = False, delete = F
137137
#self.message = 'Describe: ' + obj_type
138138
super(ET_Configure, self).__init__(response)
139139

140+
141+
########
142+
##
143+
## Class for creating filters based on an auth_stub
144+
##
145+
########
146+
class ET_Filter(object):
147+
def __init__(self, auth_stub):
148+
self.auth_stub = auth_stub
149+
150+
151+
def additionalOperandToSimpleFilter(self, operand):
152+
simpleFilter = self.auth_stub.soap_client.factory.create('SimpleFilterPart')
153+
for k, v in additional_operand.items():
154+
simpleFilter[k] = v
155+
return simpleFilter
156+
157+
158+
def complexFilter(self, leftOperand, rightOperand, logicalOperator, additionalOperands):
159+
complexFilter = self.auth_stub.soap_client.factory.create('ComplexFilterPart')
160+
161+
complexFilter["LeftOperand"] = self.filterFor(self.auth_stub, leftOperand)
162+
complexFilter["RightOperand"] = self.filterFor(self.auth_stub, rightOperand)
163+
complexFilter["LogicalOperator"] = logicalOperator
164+
165+
for op in additionalOperands:
166+
additionalOperandFilter = self.additionalOperandToSimpleFilter(op)
167+
complexFilter.AdditionalOperands.Operand.append(additionalOperandFilter)
168+
169+
return complexFilter
170+
171+
172+
def simpleFilter(self, operand):
173+
simpleFilter = self.auth_stub.soap_client.factory.create('SimpleFilterPart')
174+
for prop in simpleFilter:
175+
propertyKey = prop[0]
176+
if propertyKey in operand:
177+
simpleFilter[propertyKey] = operand[propertyKey]
178+
179+
return simpleFilter
180+
181+
182+
def filterFor(self, operand):
183+
result = None
184+
if operand.has_key('LogicalOperator'):
185+
result = self.complexFilter(self.auth_stub,
186+
operand["LeftOperand"],
187+
operand["RightOperand"],
188+
operand["LogicalOperator"],
189+
operand.get('AdditionalOperands', []))
190+
else:
191+
result = self.simpleFilter(self.auth_stub, operand)
192+
193+
return result
194+
195+
140196
########
141197
##
142198
## Get call to a web service
@@ -162,35 +218,8 @@ def __init__(self, auth_stub, obj_type, props = None, search_filter = None, opti
162218
ws_retrieveRequest.Properties = props
163219

164220
if search_filter is not None:
165-
if search_filter.has_key('LogicalOperator'):
166-
ws_simpleFilterPartLeft = auth_stub.soap_client.factory.create('SimpleFilterPart')
167-
for prop in ws_simpleFilterPartLeft:
168-
#print prop[0]
169-
if prop[0] in search_filter['LeftOperand']:
170-
ws_simpleFilterPartLeft[prop[0]] = search_filter['LeftOperand'][prop[0]]
171-
172-
ws_simpleFilterPartRight = auth_stub.soap_client.factory.create('SimpleFilterPart')
173-
for prop in ws_simpleFilterPartRight:
174-
if prop[0] in search_filter['RightOperand']:
175-
ws_simpleFilterPartRight[prop[0]] = search_filter['RightOperand'][prop[0]]
176-
177-
ws_complexFilterPart = auth_stub.soap_client.factory.create('ComplexFilterPart')
178-
ws_complexFilterPart.LeftOperand = ws_simpleFilterPartLeft
179-
ws_complexFilterPart.RightOperand = ws_simpleFilterPartRight
180-
ws_complexFilterPart.LogicalOperator = search_filter['LogicalOperator']
181-
for additional_operand in search_filter.get('AdditionalOperands', []):
182-
ws_simpleFilterPart = auth_stub.soap_client.factory.create('SimpleFilterPart')
183-
for k, v in additional_operand.items():
184-
ws_simpleFilterPart[k] = v
185-
ws_complexFilterPart.AdditionalOperands.Operand.append(ws_simpleFilterPart)
186-
187-
ws_retrieveRequest.Filter = ws_complexFilterPart
188-
else:
189-
ws_simpleFilterPart = auth_stub.soap_client.factory.create('SimpleFilterPart')
190-
for prop in ws_simpleFilterPart:
191-
if prop[0] in search_filter:
192-
ws_simpleFilterPart[prop[0]] = search_filter[prop[0]]
193-
ws_retrieveRequest.Filter = ws_simpleFilterPart
221+
newFilter = ET_Filter(auth_stub).filterFor(search_filter)
222+
ws_retrieveRequest.Filter = newFilter
194223

195224
if options is not None:
196225
for key, value in options.iteritems():

0 commit comments

Comments
 (0)