From 1fb23bec834636d0427e7ac6db92639c133ef1d2 Mon Sep 17 00:00:00 2001 From: jlaine Date: Mon, 31 May 2010 16:17:50 +0000 Subject: [PATCH] start moving charset conversion to fields git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@893 e071eeec-0327-468d-9b6a-08194a12b294 --- ldapdb/__init__.py | 14 ++------------ ldapdb/models/base.py | 4 ++-- ldapdb/models/fields.py | 12 ++++++++++++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index d1dcc47..767e6a9 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -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() diff --git a/ldapdb/models/base.py b/ldapdb/models/base.py index 981fea6..9291c6b 100644 --- a/ldapdb/models/base.py +++ b/ldapdb/models/base.py @@ -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)) diff --git a/ldapdb/models/fields.py b/ldapdb/models/fields.py index e074a6a..201a63e 100644 --- a/ldapdb/models/fields.py +++ b/ldapdb/models/fields.py @@ -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': -- 2.30.2