Skip to content

Commit 8fbf44f

Browse files
committed
Added urlsafe() serialization to compare ndb.Key values
1 parent 09c9aea commit 8fbf44f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

datastoreutils/__init__.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,19 @@ def __getattr__(self, name):
8484

8585
def matches_filters(self, property_filters=None, key_filters=None):
8686
"""
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
8889
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.
9293
9394
If key_filters are defined, these are processed before property filters.
9495
9596
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+
97100
(attr_name, operation, value)
98101
- attr_name: (str) indicates the attribute that will be fetched
99102
via `getattr(record, attr_name)` from the record
@@ -102,14 +105,17 @@ def matches_filters(self, property_filters=None, key_filters=None):
102105
- value: an arbitrary value that will be matched against the value
103106
provided by `getattr(record, attr_name)`
104107
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), *).
110116
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.
113119
114120
"""
115121
if property_filters is None and key_filters is None:
@@ -136,7 +142,7 @@ def matches_key_filters(self, key_filters):
136142

137143
for rule_pair in key_filters:
138144
chain = list()
139-
for pos, r in enumerate(rule_pair):
145+
for pos, r in enumerate(rule_pair):
140146
chain.append(self.key_pairs[pos] == tuple(r))
141147

142148
if all(chain):
@@ -154,6 +160,11 @@ def matches_property_filters(self, property_filters):
154160
attr, oper, cmp_value = rule
155161
oper = str(oper)
156162
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+
157168
if oper == '=' and real_value != cmp_value:
158169
return False
159170
elif oper == 'IN' and real_value not in cmp_value:
@@ -230,6 +241,11 @@ def match_rule(self, property_map):
230241
for prop in match_rule["properties"]:
231242
rule_attr_name, rule_attr_value = prop
232243
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+
233249
chain.append(attr_value == rule_attr_value)
234250
prop_match = all(chain)
235251
except KeyError: # Means no properties were given to match

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="datastoreutils",
6-
version="2.1",
6+
version="2.2",
77
author="Maciek Ruckgaber",
88
author_email="[email protected]",
99
description="Python mapreduce utilities for exporting / transforming Datastore entries",

0 commit comments

Comments
 (0)