X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fdjango-ldapdb.git;a=blobdiff_plain;f=ldapdb%2Frouter.py;h=ba00631cb8b77af21cbc9c9fd61b556b008f4238;hp=7e3ff4ace93112d8b7d2c38b27f7d31d99282a5d;hb=62d069c59b8ccea19532c7cda599af992b4c7ebe;hpb=50d3fcb1ad4326a55bb156fd641ce40bf52a9a51 diff --git a/ldapdb/router.py b/ldapdb/router.py index 7e3ff4a..ba00631 100644 --- a/ldapdb/router.py +++ b/ldapdb/router.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # django-ldapdb -# Copyright (c) 2009-2010, Bolloré telecom +# Copyright (c) 2009-2011, Bolloré telecom # All rights reserved. # # See AUTHORS file for a full list of contributors. @@ -37,18 +37,38 @@ 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 allow_syncdb(self, db, model): + "Do not create tables for LDAP models" + if is_ldap_model(model): + return db == self.ldap_alias + return None 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