add support for "greater or equal" and "less or equal" lookups on IntegerFields
[matthijs/upstream/django-ldapdb.git] / ldapdb / __init__.py
index c8c56eda8704e34532b4db35b0433849b8ee5922..4f9392e90c2a8c6802e721afaf9564bd2533f609 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 # 
 # django-ldapdb
-# Copyright (C) 2009 BollorĂ© telecom
+# Copyright (C) 2009-2010 BollorĂ© telecom
 # See AUTHORS file for a full list of contributors.
 # 
 # This program is free software: you can redistribute it and/or modify
@@ -31,7 +31,15 @@ def convert(field, value, func):
         return [ func(x) for x in value ]
     else:
         return func(value)
-        
+
+def escape_ldap_filter(value):
+    value = str(value)
+    return value.replace('\\', '\\5c') \
+                .replace('*', '\\2a') \
+                .replace('(', '\\28') \
+                .replace(')', '\\29') \
+                .replace('\0', '\\00')
+
 class LdapConnection(object):
     def __init__(self, server, bind_dn, bind_password):
         self.connection = ldap.initialize(server)
@@ -49,7 +57,7 @@ class LdapConnection(object):
         return self.connection.add_s(dn.encode(self.charset), mods)
 
     def delete_s(self, dn):
-        return self.connection.delete_s(dn)
+        return self.connection.delete_s(dn.encode(self.charset))
 
     def modify_s(self, dn, modlist):
         mods = []
@@ -62,13 +70,15 @@ class LdapConnection(object):
 
     def search_s(self, base, scope, filterstr, attrlist):
         results = self.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
+        output = []
         for dn, attrs in results:
             for field in attrs:
                 if field == "member" or field == "memberUid":
                     attrs[field] = convert(field, attrs[field], lambda x: x.decode(self.charset))
                 else:
                     attrs[field] = convert(field, attrs[field][0], lambda x: x.decode(self.charset))
-        return results
+            output.append((dn.decode(self.charset), attrs))
+        return output
 
 # FIXME: is this the right place to initialize the LDAP connection?
 connection = LdapConnection(settings.LDAPDB_SERVER_URI,