@@ -84,16 +84,19 @@ def __getattr__(self, name):
84
84
85
85
def matches_filters (self , property_filters = None , key_filters = None ):
86
86
"""
87
- Processes a filter list to determine if given record matches all the filters
87
+ Processes a filter list to determine if given record matches all
88
+ the filters
88
89
89
- Provides an additional filtering mechanism so filters can be applied to a model
90
- matching model_match_rule and thus generate a subset of records matched by the
91
- match rule.
90
+ Provides an additional filtering mechanism so filters can be applied
91
+ to a model matching model_match_rule and thus generate a subset of
92
+ records matched by the match rule.
92
93
93
94
If key_filters are defined, these are processed before property filters.
94
95
95
96
Args:
96
- - property_filters (iterable) List of tuples formated in the following way:
97
+ - property_filters (iterable) List of tuples formated in the following
98
+ way:
99
+
97
100
(attr_name, operation, value)
98
101
- attr_name: (str) indicates the attribute that will be fetched
99
102
via `getattr(record, attr_name)` from the record
@@ -102,14 +105,17 @@ def matches_filters(self, property_filters=None, key_filters=None):
102
105
- value: an arbitrary value that will be matched against the value
103
106
provided by `getattr(record, attr_name)`
104
107
105
- - key_filters (iterable) List of lists of tuples formated in the following way:
106
- (Model name, Value). Items in the list are considered paths conformed by the tuples
107
- they contain. Paths are processed left to right, ((ModelA, 1), (ModelB, 1)) will generate
108
- all records matching the key at the leftmost position ej:
109
- ((ModelA, 1), (ModelB,1), (ModelC, 3)), in general ((ModelA, 1), (ModelB,1), *).
108
+ - key_filters (iterable) List of lists of tuples formated in the
109
+ following way:
110
+ (Model name, Value). Items in the list are considered paths conformed
111
+ by the tuples they contain. Paths are processed left to right,
112
+ ((ModelA, 1), (ModelB, 1)) will generate all records matching the key
113
+ at the leftmost position ej:
114
+ ((ModelA, 1), (ModelB,1), (ModelC, 3)), in general
115
+ ((ModelA, 1), (ModelB,1), *).
110
116
111
- Every item of the list is considered a path, and evaluated as OR rule, if any path matches
112
- the record is generated.
117
+ Every item of the list is considered a path, and evaluated as OR rule,
118
+ if any path matches the record is generated.
113
119
114
120
"""
115
121
if property_filters is None and key_filters is None :
@@ -136,7 +142,7 @@ def matches_key_filters(self, key_filters):
136
142
137
143
for rule_pair in key_filters :
138
144
chain = list ()
139
- for pos , r in enumerate (rule_pair ):
145
+ for pos , r in enumerate (rule_pair ):
140
146
chain .append (self .key_pairs [pos ] == tuple (r ))
141
147
142
148
if all (chain ):
@@ -154,6 +160,11 @@ def matches_property_filters(self, property_filters):
154
160
attr , oper , cmp_value = rule
155
161
oper = str (oper )
156
162
real_value = getattr (self , attr )
163
+
164
+ # ndb.Keys can only be compared as strings
165
+ if isinstance (real_value , ndb .Key ):
166
+ real_value = real_value .urlsafe ()
167
+
157
168
if oper == '=' and real_value != cmp_value :
158
169
return False
159
170
elif oper == 'IN' and real_value not in cmp_value :
@@ -230,6 +241,11 @@ def match_rule(self, property_map):
230
241
for prop in match_rule ["properties" ]:
231
242
rule_attr_name , rule_attr_value = prop
232
243
attr_value = getattr (self , rule_attr_name )
244
+
245
+ # ndb.Keys must be compared as strings
246
+ if isinstance (attr_value , ndb .Key ):
247
+ attr_value = attr_value .urlsafe ()
248
+
233
249
chain .append (attr_value == rule_attr_value )
234
250
prop_match = all (chain )
235
251
except KeyError : # Means no properties were given to match
0 commit comments