Do not create tables for LDAP models (thanks Gervase)
[matthijs/upstream/django-ldapdb.git] / ldapdb / router.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (c) 2009-2011, BollorĂ© telecom
5 # All rights reserved.
6
7 # See AUTHORS file for a full list of contributors.
8
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
11
12 #     1. Redistributions of source code must retain the above copyright notice, 
13 #        this list of conditions and the following disclaimer.
14 #     
15 #     2. Redistributions in binary form must reproduce the above copyright 
16 #        notice, this list of conditions and the following disclaimer in the
17 #        documentation and/or other materials provided with the distribution.
18
19 #     3. Neither the name of BollorĂ© telecom nor the names of its contributors
20 #        may be used to endorse or promote products derived from this software
21 #        without specific prior written permission.
22
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #
34
35 def is_ldap_model(model):
36     # FIXME: there is probably a better check than testing 'base_dn'
37     return hasattr(model, 'base_dn')
38
39 class Router(object):
40     """
41     A router to point database operations on LDAP models to the LDAP
42     database.
43
44     NOTE: if you have more than one LDAP database, you will need to
45     write your own router.
46     """
47
48     def __init__(self):
49         "Find the name of the LDAP database"
50         from django.conf import settings
51         self.ldap_alias = None
52         for alias, settings_dict in settings.DATABASES.items():
53             if settings_dict['ENGINE'] == 'ldapdb.backends.ldap':
54                 self.ldap_alias = alias
55                 break
56
57     def allow_syncdb(self, db, model):
58         "Do not create tables for LDAP models"
59         if is_ldap_model(model):
60             return db == self.ldap_alias
61         return None
62
63     def db_for_read(self, model, **hints):
64         "Point all operations on LDAP models to the LDAP database"
65         if is_ldap_model(model):
66             return self.ldap_alias
67         return None
68
69     def db_for_write(self, model, **hints):
70         "Point all operations on LDAP models to the LDAP database"
71         if is_ldap_model(model):
72             return self.ldap_alias
73         return None
74