change declaration of base_dn and object_classes
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Wed, 2 Sep 2009 07:47:01 +0000 (07:47 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Wed, 2 Sep 2009 07:47:01 +0000 (07:47 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@548 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/base.py
ldapdb/models/query.py

index 3c15151ba53fe154234e535b52a3ff594e142982..cdc44e21fc4e32869dc0c49043951a88f130f102 100644 (file)
@@ -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
+
index b53d9591d73c54a2a187d92cc74b0fc5949c2c97..7d952d45b23aca76514f787f7c22e87d9498345d 100644 (file)
@@ -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,