move field conversion
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 16:32:10 +0000 (16:32 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 16:32:10 +0000 (16:32 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@894 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/__init__.py
ldapdb/models/fields.py
ldapdb/models/query.py

index 767e6a9..3ca9c63 100644 (file)
@@ -89,8 +89,6 @@ class LdapConnection(object):
         results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
         output = []
         for dn, attrs in results:
-            for field in attrs:
-                attrs[field] = convert(field, attrs[field], lambda x: x.decode(self.charset))
             output.append((dn.decode(self.charset), attrs))
         return output
 
index 201a63e..ed1d3df 100644 (file)
@@ -27,6 +27,12 @@ class CharField(fields.CharField):
         kwargs['max_length'] = 200
         super(CharField, self).__init__(*args, **kwargs)
 
+    def from_ldap(self, value, connection):
+        if len(value) == 0:
+            return ''
+        else:
+            return value[0].decode(connection.charset)
+
     def get_db_prep_lookup(self, lookup_type, value):
         "Returns field's value prepared for database lookup."
         if lookup_type == 'endswith':
@@ -61,6 +67,12 @@ class CharField(fields.CharField):
         raise TypeError("CharField has invalid lookup: %s" % lookup_type)
 
 class ImageField(fields.Field):
+    def from_ldap(self, value, connection):
+        if len(value) == 0:
+            return ''
+        else:
+            return value[0]
+
     def get_db_prep_lookup(self, lookup_type, value):
         "Returns field's value prepared for database lookup."
         return [self.get_prep_lookup(lookup_type, value)]
@@ -73,6 +85,12 @@ class ImageField(fields.Field):
         raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
 
 class IntegerField(fields.IntegerField):
+    def from_ldap(self, value, connection):
+        if len(value) == 0:
+            return 0
+        else:
+            return int(value[0])
+        
     def get_db_prep_lookup(self, lookup_type, value):
         "Returns field's value prepared for database lookup."
         return [self.get_prep_lookup(lookup_type, value)]
@@ -89,6 +107,9 @@ class IntegerField(fields.IntegerField):
 class ListField(fields.Field):
     __metaclass__ = SubfieldBase
 
+    def from_ldap(self, value, connection):
+        return value
+
     def get_db_prep_lookup(self, lookup_type, value):
         "Returns field's value prepared for database lookup."
         return [self.get_prep_lookup(lookup_type, value)]
index e43be2d..d49b44a 100644 (file)
@@ -28,7 +28,7 @@ from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as
 
 import ldapdb
 
-from ldapdb.models.fields import CharField, IntegerField, ListField
+from ldapdb.models.fields import CharField
 
 def get_lookup_operator(lookup_type):
     if lookup_type == 'gte':
@@ -185,12 +185,10 @@ class Query(BaseQuery):
             for field in iter(self.model._meta.fields):
                 if field.attname == 'dn':
                     row.append(dn)
-                elif isinstance(field, IntegerField):
-                    row.append(int(attrs.get(field.db_column, [0])[0]))
-                elif isinstance(field, ListField):
-                    row.append(attrs.get(field.db_column, []))
+                elif hasattr(field, 'from_ldap'):
+                    row.append(field.from_ldap(attrs.get(field.db_column, []), connection=ldapdb.connection))
                 else:
-                    row.append(attrs.get(field.db_column, [''])[0])
+                    row.append(None)
             yield row
 
 class QuerySet(BaseQuerySet):