1 # -*- coding: utf-8 -*-
4 # Copyright (C) 2009 Bolloré telecom
5 # See AUTHORS file for a full list of contributors.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # -*- coding: utf-8 -*-
26 import django.db.models
27 from django.db.models import signals
30 from ldapdb.models.query import QuerySet
32 class ModelBase(django.db.models.base.ModelBase):
34 Metaclass for all LDAP models.
36 def __new__(cls, name, bases, attrs):
37 attr_meta = attrs.pop('Ldap', None)
39 super_new = super(ModelBase, cls).__new__
40 new_class = super_new(cls, name, bases, attrs)
42 # patch manager to use our own QuerySet class
43 if not new_class._meta.abstract:
45 return QuerySet(new_class)
46 new_class.objects.get_query_set = get_query_set
47 new_class._default_manager.get_query_set = get_query_set
50 new_class._meta.dn = attr_meta.dn
51 new_class._meta.object_classes = attr_meta.object_classes
55 class Model(django.db.models.base.Model):
57 Base class for all LDAP models.
59 __metaclass__ = ModelBase
61 dn = django.db.models.fields.CharField(max_length=200)
65 object_classes = ['top']
67 def __init__(self, *args, **kwargs):
68 super(Model, self).__init__(*args, **kwargs)
69 self.saved_pk = self.pk
73 Build the Relative Distinguished Name for this entry.
76 for field in self._meta.local_fields:
78 bits.append("%s=%s" % (field.db_column, getattr(self, field.name)))
80 raise Exception("Could not build Distinguished Name")
85 Build the Distinguished Name for this entry.
87 return "%s,%s" % (self.build_rdn(), self.base_dn)
88 raise Exception("Could not build Distinguished Name")
94 logging.debug("Deleting LDAP entry %s" % self.dn)
95 ldapdb.connection.delete_s(self.dn)
96 signals.post_delete.send(sender=self.__class__, instance=self)
101 record_exists = False
102 entry = [('objectClass', self.object_classes)]
103 new_dn = self.build_dn()
105 for field in self._meta.local_fields:
106 if not field.db_column:
108 value = getattr(self, field.name)
110 entry.append((field.db_column, value))
112 logging.debug("Creating new LDAP entry %s" % new_dn)
113 ldapdb.connection.add_s(new_dn, entry)
119 # update an existing entry
122 orig = self.__class__.objects.get(pk=self.saved_pk)
123 for field in self._meta.local_fields:
124 if not field.db_column:
126 old_value = getattr(orig, field.name, None)
127 new_value = getattr(self, field.name, None)
128 if old_value != new_value:
130 modlist.append((ldap.MOD_REPLACE, field.db_column, new_value))
132 modlist.append((ldap.MOD_DELETE, field.db_column, None))
136 new_dn = self.build_dn()
137 if new_dn != self.dn:
138 logging.debug("Renaming LDAP entry %s to %s" % (self.dn, new_dn))
139 ldapdb.connection.rename_s(self.dn, self.build_rdn())
142 logging.debug("Modifying existing LDAP entry %s" % self.dn)
143 ldapdb.connection.modify_s(self.dn, modlist)
145 logging.debug("No changes to be saved to LDAP entry %s" % self.dn)
148 self.saved_pk = self.pk
149 signals.post_save.send(sender=self.__class__, instance=self, created=(not record_exists))
152 def scoped(base_class, base_dn):
154 Returns a copy of the current class with a different base_dn.
158 suffix = re.sub('[=,]', '_', base_dn)
159 name = "%s_%s" % (base_class.__name__, str(suffix))
160 new_class = new.classobj(name, (base_class,), {'base_dn': base_dn, '__module__': base_class.__module__})