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
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':
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)]
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)]
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)]
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':
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):