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 d1dcc47d05012a4260431dcabc6b2b3ffe306068..767e6a9fc3030a7dfe6363e7f337e39f2054a8f5 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 981fea6d994565051d838730414dc34ecfe1d71b..9291c6b122656dc4b08e92db20dc30928d22bc28 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 e074a6a7b51dfbacf274af488e3b8d334a422662..201a63e74dfd77d92b5289116ca9ff94a29c78e7 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':