do not hardcode database name in router
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Wed, 13 Apr 2011 09:05:52 +0000 (09:05 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Wed, 13 Apr 2011 09:05:52 +0000 (09:05 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@1051 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/router.py

index 4da0c253a2bbe93e73b6d8d125425620ca0cd8d0..5c3a6744c0581ecd0d4466d74ebc440fea33f4d9 100644 (file)
@@ -37,18 +37,32 @@ def is_ldap_model(model):
     return hasattr(model, 'base_dn')
 
 class Router(object):
-    """A router to control all database operations on models in
-    the myapp application"""
+    """
+    A router to point database operations on LDAP models to the LDAP
+    database.
+
+    NOTE: if you have more than one LDAP database, you will need to
+    write your own router.
+    """
+
+    def __init__(self):
+        "Find the name of the LDAP database"
+        from django.conf import settings
+        self.ldap_alias = None
+        for alias, settings_dict in settings.DATABASES.items():
+            if settings_dict['ENGINE'] == 'ldapdb.backends.ldap':
+                self.ldap_alias = alias
+                break
 
     def db_for_read(self, model, **hints):
-        "Point all operations on LDAP models to 'ldap'"
+        "Point all operations on LDAP models to the LDAP database"
         if is_ldap_model(model):
-            return 'ldap'
+            return self.ldap_alias
         return None
 
     def db_for_write(self, model, **hints):
-        "Point all operations on LDAP models to 'ldap'"
+        "Point all operations on LDAP models to the LDAP database"
         if is_ldap_model(model):
-            return 'ldap'
+            return self.ldap_alias
         return None