rename modules
[matthijs/upstream/django-ldapdb.git] / ldapdb / models / base.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (C) 2009 BollorĂ© telecom
5 # See AUTHORS file for a full list of contributors.
6
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.
11
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.
16
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/>.
19 #
20
21 # -*- coding: utf-8 -*-
22
23 import ldap
24 import logging
25
26 import django.db.models
27
28 import ldapdb
29 from ldapdb.models.query import QuerySet
30
31 class ModelBase(django.db.models.base.ModelBase):
32     """
33     Metaclass for all LDAP models.
34     """
35     def __new__(cls, name, bases, attrs):
36         attr_meta = attrs.get('Meta', None)
37         if attr_meta:
38             dn = attr_meta._dn
39             object_classes = attr_meta._object_classes
40
41         super_new = super(ModelBase, cls).__new__
42         new_class = super_new(cls, name, bases, attrs)
43
44         # patch manager to use our own QuerySet class
45         def get_query_set():
46             return QuerySet(new_class)
47         new_class.objects.get_query_set = get_query_set
48         new_class._default_manager.get_query_set = get_query_set
49
50         if attr_meta:
51             new_class._meta.dn = dn
52             new_class._meta.object_classes = attr_meta._object_classes
53
54         return new_class
55
56 class Model(django.db.models.base.Model):
57     """
58     Base class for all LDAP models.
59     """
60     __metaclass__ = ModelBase
61
62     def __init__(self, dn=None, *args, **kwargs):
63         self.dn = dn
64         super(Model, self).__init__(*args, **kwargs)
65
66     def build_dn(self):
67         """
68         Build the Distinguished Name for this entry.
69         """
70         for field in self._meta.local_fields:
71             if field.primary_key:
72                 return "%s=%s,%s" % (field.db_column, getattr(self, field.name), self._meta.dn)
73         raise Exception("Could not build Distinguished Name")
74
75     def delete(self):
76         """
77         Delete this entry.
78         """
79         logging.debug("Deleting LDAP entry %s" % self.dn)
80         ldapdb.connection.delete_s(self.dn)
81         
82     def save(self):
83         # create a new entry
84         if not self.dn:
85             entry = [('objectClass', self._meta.object_classes)]
86             new_dn = self.build_dn()
87
88             for field in self._meta.local_fields:
89                 if not field.db_column:
90                     continue
91                 value = getattr(self, field.name)
92                 if value:
93                     entry.append((field.db_column, value))
94
95             logging.debug("Creating new LDAP entry %s" % new_dn)
96             ldapdb.connection.add_s(new_dn, entry)
97             
98             # update object
99             self.dn = new_dn
100             return
101
102         # update an existing entry
103         modlist = []
104         orig = self.__class__.objects.get(pk=self.pk)
105         for field in self._meta.local_fields:
106             if not field.db_column:
107                 continue
108             old_value = getattr(orig, field.name, None)
109             new_value = getattr(self, field.name, None)
110             if old_value != new_value:
111                 if new_value:
112                     modlist.append((ldap.MOD_REPLACE, field.db_column, new_value))
113                 elif old_value:
114                     modlist.append((ldap.MOD_DELETE, field.db_column, None))
115
116         if len(modlist):
117             logging.debug("Modifying existing LDAP entry %s" % self.dn)
118             ldapdb.connection.modify_s(self.dn, modlist)
119         else:
120             logging.debug("No changes to be saved to LDAP entry %s" % self.dn)
121