change declaration of base_dn and object_classes
[matthijs/upstream/django-ldapdb.git] / ldapdb / models / query.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 from copy import deepcopy
24 import ldap
25
26 from django.db.models.query import QuerySet as BaseQuerySet
27 from django.db.models.query_utils import Q
28 from django.db.models.sql import Query as BaseQuery
29 from django.db.models.sql.where import WhereNode as BaseWhereNode, AND, OR
30
31 import ldapdb
32
33 class WhereNode(BaseWhereNode):
34     def as_sql(self):
35         bits = []
36         for item in self.children:
37             if isinstance(item, WhereNode):
38                 bits.append(item.as_sql())
39                 continue
40             if len(item) == 4:
41                 # django 1.1
42                 (table, column, type), x, y, values = item
43             else:
44                 # django 1.0
45                 table, column, type, x, y, values = item
46             if self.negated:
47                 bits.append('(!(%s=%s))' % (column,values[0]))
48             else:
49                 bits.append('(%s=%s)' % (column,values[0]))
50         if len(bits) == 1:
51             return bits[0]
52         elif self.connector == AND:
53             return '(&%s)' % ''.join(bits)
54         elif self.connector == OR:
55             return '(|%s)' % ''.join(bits)
56         else:
57             raise Exception("Unhandled WHERE connector: %s" % self.connector)
58
59 class Query(BaseQuery):
60     def results_iter(self):
61         # FIXME: use all object classes
62         filterstr = '(objectClass=%s)' % self.model.object_classes[0]
63         filterstr += self.where.as_sql()
64         filterstr = '(&%s)' % filterstr
65         attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ]
66
67         try:
68             vals = ldapdb.connection.search_s(
69                 self.model.base_dn,
70                 ldap.SCOPE_SUBTREE,
71                 filterstr=filterstr,
72                 attrlist=attrlist,
73             )
74         except:
75             raise self.model.DoesNotExist
76
77         # perform sorting
78         if self.extra_order_by:
79             ordering = self.extra_order_by
80         elif not self.default_ordering:
81             ordering = self.order_by
82         else:
83             ordering = self.order_by or self.model._meta.ordering
84         def getkey(x):
85             keys = []
86             for k in ordering:
87                 attr = self.model._meta.get_field(k).db_column
88                 keys.append(x[1][attr])
89             return keys
90         vals = sorted(vals, key=lambda x: getkey(x))
91
92         # process results
93         for dn, attrs in vals:
94             row = []
95             for field in iter(self.model._meta.fields):
96                 if field.attname == 'dn':
97                     row.append(dn)
98                 else:
99                     row.append(attrs.get(field.db_column, None))
100             yield row
101
102 class QuerySet(BaseQuerySet):
103     def __init__(self, model=None, query=None):
104         if not query:
105             query = Query(model, None, WhereNode)
106         super(QuerySet, self).__init__(model, query)
107