From: jlaine Date: Wed, 2 Sep 2009 07:47:01 +0000 (+0000) Subject: change declaration of base_dn and object_classes X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=81037a75f672596982475a7081946bfd9b4fd286;p=matthijs%2Fupstream%2Fdjango-ldapdb.git change declaration of base_dn and object_classes git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@548 e071eeec-0327-468d-9b6a-08194a12b294 --- diff --git a/ldapdb/models/base.py b/ldapdb/models/base.py index 3c15151..cdc44e2 100644 --- a/ldapdb/models/base.py +++ b/ldapdb/models/base.py @@ -59,6 +59,10 @@ class Model(django.db.models.base.Model): dn = django.db.models.fields.CharField(max_length=200) + # meta-data + base_dn = None + object_classes = ['top'] + def __init__(self, *args, **kwargs): super(Model, self).__init__(*args, **kwargs) self.saved_pk = self.pk @@ -79,7 +83,7 @@ class Model(django.db.models.base.Model): """ Build the Distinguished Name for this entry. """ - return "%s,%s" % (self.build_rdn(), self._meta.dn) + return "%s,%s" % (self.build_rdn(), self.base_dn) raise Exception("Could not build Distinguished Name") def delete(self): @@ -94,7 +98,7 @@ class Model(django.db.models.base.Model): if not self.dn: # create a new entry record_exists = False - entry = [('objectClass', self._meta.object_classes)] + entry = [('objectClass', self.object_classes)] new_dn = self.build_dn() for field in self._meta.local_fields: @@ -143,3 +147,14 @@ class Model(django.db.models.base.Model): self.saved_pk = self.pk signals.post_save.send(sender=self.__class__, instance=self, created=(not record_exists)) + @classmethod + def scoped(base_class, base_dn): + """ + Returns a copy of the current class with a different base_dn. + """ + import new + import re + name = "%s_%s" % (base_class.__name__, re.sub('[=,]', '_', base_dn)) + new_class = new.classobj(name, (base_class,), {'base_dn': base_dn, '__module__': base_class.__module__}) + return new_class + diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index b53d959..7d952d4 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -59,14 +59,14 @@ class WhereNode(BaseWhereNode): class Query(BaseQuery): def results_iter(self): # FIXME: use all object classes - filterstr = '(objectClass=%s)' % self.model._meta.object_classes[0] + filterstr = '(objectClass=%s)' % self.model.object_classes[0] filterstr += self.where.as_sql() filterstr = '(&%s)' % filterstr attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ] try: vals = ldapdb.connection.search_s( - self.model._meta.dn, + self.model.base_dn, ldap.SCOPE_SUBTREE, filterstr=filterstr, attrlist=attrlist,