projects
/
matthijs
/
upstream
/
django-ldapdb.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
improve tests : Constraint should operate on a field
[matthijs/upstream/django-ldapdb.git]
/
ldapdb
/
models
/
query.py
diff --git
a/ldapdb/models/query.py
b/ldapdb/models/query.py
index f430286b65be5d14531e46a86b540eaa3a851ab6..ae4b73c7b559a3eac87e4953935b271c4a46bba1 100644
(file)
--- a/
ldapdb/models/query.py
+++ b/
ldapdb/models/query.py
@@
-21,7
+21,6
@@
from copy import deepcopy
import ldap
from copy import deepcopy
import ldap
-from django.db.models.fields import Field
from django.db.models.query import QuerySet as BaseQuerySet
from django.db.models.query_utils import Q
from django.db.models.sql import Query as BaseQuery
from django.db.models.query import QuerySet as BaseQuerySet
from django.db.models.query_utils import Q
from django.db.models.sql import Query as BaseQuery
@@
-29,6
+28,14
@@
from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as
import ldapdb
import ldapdb
+def escape_ldap_filter(value):
+ value = str(value)
+ return value.replace('\\', '\\5c') \
+ .replace('*', '\\2a') \
+ .replace('(', '\\28') \
+ .replace(')', '\\29') \
+ .replace('\0', '\\00')
+
class Constraint(BaseConstraint):
"""
An object that can be passed to WhereNode.add() and knows how to
class Constraint(BaseConstraint):
"""
An object that can be passed to WhereNode.add() and knows how to
@@
-43,13
+50,15
@@
class Constraint(BaseConstraint):
from django.db.models.base import ObjectDoesNotExist
if lookup_type == 'endswith':
from django.db.models.base import ObjectDoesNotExist
if lookup_type == 'endswith':
- params = ["*%s" %
value
]
+ params = ["*%s" %
escape_ldap_filter(value)
]
elif lookup_type == 'startswith':
elif lookup_type == 'startswith':
- params = ["%s*" % value]
+ params = ["%s*" % escape_ldap_filter(value)]
+ elif lookup_type == 'contains':
+ params = ["*%s*" % escape_ldap_filter(value)]
elif lookup_type == 'exact':
elif lookup_type == 'exact':
- params = [
value
]
+ params = [
escape_ldap_filter(value)
]
elif lookup_type == 'in':
elif lookup_type == 'in':
- params = [
v
for v in value]
+ params = [
escape_ldap_filter(v)
for v in value]
else:
raise TypeError("Field has invalid lookup: %s" % lookup_type)
else:
raise TypeError("Field has invalid lookup: %s" % lookup_type)
@@
-81,12
+90,7
@@
class WhereNode(BaseWhereNode):
if isinstance(item, WhereNode):
bits.append(item.as_sql())
continue
if isinstance(item, WhereNode):
bits.append(item.as_sql())
continue
- if len(item) == 4:
- # django 1.1
- (table, column, type), x, y, values = item
- else:
- # django 1.0
- table, column, type, x, y, values = item
+ (table, column, type), x, y, values = item
equal_bits = [ "(%s=%s)" % (column, value) for value in values ]
if len(equal_bits) == 1:
clause = equal_bits[0]
equal_bits = [ "(%s=%s)" % (column, value) for value in values ]
if len(equal_bits) == 1:
clause = equal_bits[0]