From c04580563f978929bc4f06dfa85ea7a26248d688 Mon Sep 17 00:00:00 2001 From: jlaine Date: Mon, 31 May 2010 16:32:10 +0000 Subject: [PATCH] move field conversion git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@894 e071eeec-0327-468d-9b6a-08194a12b294 --- ldapdb/__init__.py | 2 -- ldapdb/models/fields.py | 21 +++++++++++++++++++++ ldapdb/models/query.py | 10 ++++------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index 767e6a9..3ca9c63 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -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 diff --git a/ldapdb/models/fields.py b/ldapdb/models/fields.py index 201a63e..ed1d3df 100644 --- a/ldapdb/models/fields.py +++ b/ldapdb/models/fields.py @@ -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)] diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index e43be2d..d49b44a 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -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): -- 2.30.2