start moving charset conversion to fields
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 16:17:50 +0000 (16:17 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 16:17:50 +0000 (16:17 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@893 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/__init__.py
ldapdb/models/base.py
ldapdb/models/fields.py

index d1dcc47..767e6a9 100644 (file)
@@ -69,26 +69,16 @@ class LdapConnection(object):
         return DatabaseCursor(self.connection)
 
     def add_s(self, dn, modlist):
-        mods = []
-        for field, value in modlist:
-            converted = convert(field, value, lambda x: x.encode(self.charset))
-            if isinstance(converted, list):
-                mods.append((field, converted))
-            else:
-                mods.append((field, [converted]))
         cursor = self._cursor()
-        return cursor.connection.add_s(dn.encode(self.charset), mods)
+        return cursor.connection.add_s(dn.encode(self.charset), modlist)
 
     def delete_s(self, dn):
         cursor = self._cursor()
         return cursor.connection.delete_s(dn.encode(self.charset))
 
     def modify_s(self, dn, modlist):
-        mods = []
-        for op, field, value in modlist:
-            mods.append((op, field, convert(field, value, lambda x: x.encode(self.charset))))
         cursor = self._cursor()
-        return cursor.connection.modify_s(dn.encode(self.charset), mods)
+        return cursor.connection.modify_s(dn.encode(self.charset), modlist)
 
     def rename_s(self, dn, newrdn):
         cursor = self._cursor()
index 981fea6..9291c6b 100644 (file)
@@ -99,7 +99,7 @@ class Model(django.db.models.base.Model):
                     continue
                 value = getattr(self, field.name)
                 if value:
-                    entry.append((field.db_column, value))
+                    entry.append((field.db_column, field.get_db_prep_save(value, connection=ldapdb.connection)))
 
             logging.debug("Creating new LDAP entry %s" % new_dn)
             ldapdb.connection.add_s(new_dn, entry)
@@ -119,7 +119,7 @@ class Model(django.db.models.base.Model):
                 new_value = getattr(self, field.name, None)
                 if old_value != new_value:
                     if new_value:
-                        modlist.append((ldap.MOD_REPLACE, field.db_column, new_value))
+                        modlist.append((ldap.MOD_REPLACE, field.db_column, field.get_db_prep_save(new_value, connection=ldapdb.connection)))
                     elif old_value:
                         modlist.append((ldap.MOD_DELETE, field.db_column, None))
 
index e074a6a..201a63e 100644 (file)
@@ -42,6 +42,9 @@ class CharField(fields.CharField):
 
         raise TypeError("CharField has invalid lookup: %s" % lookup_type)
 
+    def get_db_prep_save(self, value, connection):
+        return [value.encode(connection.charset)]
+
     def get_prep_lookup(self, lookup_type, value):
         "Perform preliminary non-db specific lookup checks and conversions"
         if lookup_type == 'endswith':
@@ -62,6 +65,9 @@ class ImageField(fields.Field):
         "Returns field's value prepared for database lookup."
         return [self.get_prep_lookup(lookup_type, value)]
 
+    def get_db_prep_save(self, value, connection):
+        return [value]
+
     def get_prep_lookup(self, lookup_type, value):
         "Perform preliminary non-db specific lookup checks and conversions"
         raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
@@ -71,6 +77,9 @@ class IntegerField(fields.IntegerField):
         "Returns field's value prepared for database lookup."
         return [self.get_prep_lookup(lookup_type, value)]
 
+    def get_db_prep_save(self, value, connection):
+        return [str(value)]
+
     def get_prep_lookup(self, lookup_type, value):
         "Perform preliminary non-db specific lookup checks and conversions"
         if lookup_type in ('exact', 'gte', 'lte'):
@@ -84,6 +93,9 @@ class ListField(fields.Field):
         "Returns field's value prepared for database lookup."
         return [self.get_prep_lookup(lookup_type, value)]
 
+    def get_db_prep_save(self, value, connection):
+        return [x.encode(connection.charset) for x in value]
+
     def get_prep_lookup(self, lookup_type, value):
         "Perform preliminary non-db specific lookup checks and conversions"
         if lookup_type == 'contains':