From 5dbff32f9e93fa79bf5d0da8e19993e350456e6b Mon Sep 17 00:00:00 2001 From: Francis Carr Date: Wed, 23 Dec 2015 16:55:08 +0000 Subject: [PATCH 1/2] Add ET_Filter to create more complex queries --- FuelSDK/rest.py | 87 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 29 deletions(-) diff --git a/FuelSDK/rest.py b/FuelSDK/rest.py index 51bf2c4..ec50a78 100644 --- a/FuelSDK/rest.py +++ b/FuelSDK/rest.py @@ -137,6 +137,62 @@ def __init__(self, auth_stub, obj_type, props = None, update = False, delete = F #self.message = 'Describe: ' + obj_type super(ET_Configure, self).__init__(response) + +######## +## +## Class for creating filters based on an auth_stub +## +######## +class ET_Filter(object): + def __init__(self, auth_stub): + self.auth_stub = auth_stub + + + def additionalOperandToSimpleFilter(self, operand): + simpleFilter = self.auth_stub.soap_client.factory.create('SimpleFilterPart') + for k, v in additional_operand.items(): + simpleFilter[k] = v + return simpleFilter + + + def complexFilter(self, leftOperand, rightOperand, logicalOperator, additionalOperands): + complexFilter = self.auth_stub.soap_client.factory.create('ComplexFilterPart') + + complexFilter["LeftOperand"] = self.filterFor(self.auth_stub, leftOperand) + complexFilter["RightOperand"] = self.filterFor(self.auth_stub, rightOperand) + complexFilter["LogicalOperator"] = logicalOperator + + for op in additionalOperands: + additionalOperandFilter = self.additionalOperandToSimpleFilter(op) + complexFilter.AdditionalOperands.Operand.append(additionalOperandFilter) + + return complexFilter + + + def simpleFilter(self, operand): + simpleFilter = self.auth_stub.soap_client.factory.create('SimpleFilterPart') + for prop in simpleFilter: + propertyKey = prop[0] + if propertyKey in operand: + simpleFilter[propertyKey] = operand[propertyKey] + + return simpleFilter + + + def filterFor(self, operand): + result = None + if operand.has_key('LogicalOperator'): + result = self.complexFilter(self.auth_stub, + operand["LeftOperand"], + operand["RightOperand"], + operand["LogicalOperator"], + operand.get('AdditionalOperands', [])) + else: + result = self.simpleFilter(self.auth_stub, operand) + + return result + + ######## ## ## Get call to a web service @@ -162,35 +218,8 @@ def __init__(self, auth_stub, obj_type, props = None, search_filter = None, opti ws_retrieveRequest.Properties = props if search_filter is not None: - if search_filter.has_key('LogicalOperator'): - ws_simpleFilterPartLeft = auth_stub.soap_client.factory.create('SimpleFilterPart') - for prop in ws_simpleFilterPartLeft: - #print prop[0] - if prop[0] in search_filter['LeftOperand']: - ws_simpleFilterPartLeft[prop[0]] = search_filter['LeftOperand'][prop[0]] - - ws_simpleFilterPartRight = auth_stub.soap_client.factory.create('SimpleFilterPart') - for prop in ws_simpleFilterPartRight: - if prop[0] in search_filter['RightOperand']: - ws_simpleFilterPartRight[prop[0]] = search_filter['RightOperand'][prop[0]] - - ws_complexFilterPart = auth_stub.soap_client.factory.create('ComplexFilterPart') - ws_complexFilterPart.LeftOperand = ws_simpleFilterPartLeft - ws_complexFilterPart.RightOperand = ws_simpleFilterPartRight - ws_complexFilterPart.LogicalOperator = search_filter['LogicalOperator'] - for additional_operand in search_filter.get('AdditionalOperands', []): - ws_simpleFilterPart = auth_stub.soap_client.factory.create('SimpleFilterPart') - for k, v in additional_operand.items(): - ws_simpleFilterPart[k] = v - ws_complexFilterPart.AdditionalOperands.Operand.append(ws_simpleFilterPart) - - ws_retrieveRequest.Filter = ws_complexFilterPart - else: - ws_simpleFilterPart = auth_stub.soap_client.factory.create('SimpleFilterPart') - for prop in ws_simpleFilterPart: - if prop[0] in search_filter: - ws_simpleFilterPart[prop[0]] = search_filter[prop[0]] - ws_retrieveRequest.Filter = ws_simpleFilterPart + newFilter = ET_Filter(auth_stub).filterFor(search_filter) + ws_retrieveRequest.Filter = newFilter if options is not None: for key, value in options.iteritems(): From 76b01a15bf88dd8cd08b5201ee1ae4b4b9c46ded Mon Sep 17 00:00:00 2001 From: Francis Carr Date: Mon, 11 Jan 2016 15:16:04 +0000 Subject: [PATCH 2/2] Fix call to complex filter and usage of additionalOprands --- FuelSDK/rest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/FuelSDK/rest.py b/FuelSDK/rest.py index ec50a78..1576ce3 100644 --- a/FuelSDK/rest.py +++ b/FuelSDK/rest.py @@ -150,7 +150,7 @@ def __init__(self, auth_stub): def additionalOperandToSimpleFilter(self, operand): simpleFilter = self.auth_stub.soap_client.factory.create('SimpleFilterPart') - for k, v in additional_operand.items(): + for k, v in operand.items(): simpleFilter[k] = v return simpleFilter @@ -158,8 +158,8 @@ def additionalOperandToSimpleFilter(self, operand): def complexFilter(self, leftOperand, rightOperand, logicalOperator, additionalOperands): complexFilter = self.auth_stub.soap_client.factory.create('ComplexFilterPart') - complexFilter["LeftOperand"] = self.filterFor(self.auth_stub, leftOperand) - complexFilter["RightOperand"] = self.filterFor(self.auth_stub, rightOperand) + complexFilter["LeftOperand"] = self.filterFor(leftOperand) + complexFilter["RightOperand"] = self.filterFor(rightOperand) complexFilter["LogicalOperator"] = logicalOperator for op in additionalOperands: @@ -182,13 +182,13 @@ def simpleFilter(self, operand): def filterFor(self, operand): result = None if operand.has_key('LogicalOperator'): - result = self.complexFilter(self.auth_stub, + result = self.complexFilter( operand["LeftOperand"], operand["RightOperand"], operand["LogicalOperator"], operand.get('AdditionalOperands', [])) else: - result = self.simpleFilter(self.auth_stub, operand) + result = self.simpleFilter(operand) return result