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 767e6a9fc3030a7dfe6363e7f337e39f2054a8f5..3ca9c632a5afd5dadd8b17523db2d6f47e450f36 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 201a63e74dfd77d92b5289116ca9ff94a29c78e7..ed1d3df798fd3ee0f6b4fce4fc93aacc40c0d5c1 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 e43be2d19dcf92bc0099500d3b3d1bbf496fe639..d49b44a41d1977c8b37fdd6266b3df9fca662bfb 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):