support ordering
[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 BaseQuery
29 from django.db.models.sql.where import WhereNode
30
31 import ldapdb
32
33 def compile(q):
34     filterstr = ""
35     for item in q.children:
36         if isinstance(item, WhereNode):
37             filterstr += compile(item)
38             continue
39         table, column, type, x, y, values = item
40         if q.negated:
41             filterstr += "(!(%s=%s))" % (column,values[0])
42         else:
43             filterstr += "(%s=%s)" % (column,values[0])
44     return filterstr
45
46 class Query(BaseQuery):
47     def results_iter(self):
48         # FIXME: use all object classes
49         filterstr = '(objectClass=%s)' % self.model._meta.object_classes[0]
50         filterstr += compile(self.where)
51         filterstr = '(&%s)' % filterstr
52         attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ]
53
54         try:
55             vals = ldapdb.connection.search_s(
56                 self.model._meta.dn,
57                 ldap.SCOPE_SUBTREE,
58                 filterstr=filterstr,
59                 attrlist=attrlist,
60             )
61         except:
62             raise self.model.DoesNotExist
63
64         # perform sorting
65         if self.extra_order_by:
66             ordering = self.extra_order_by
67         elif not self.default_ordering:
68             ordering = self.order_by
69         else:
70             ordering = self.order_by or self.model._meta.ordering
71         def getkey(x):
72             keys = []
73             for k in ordering:
74                 attr = self.model._meta.get_field(k).db_column
75                 keys.append(x[1][attr])
76             return keys
77         vals = sorted(vals, key=lambda x: getkey(x))
78
79         # process results
80         for dn, attrs in vals:
81             row = [dn]
82             for field in iter(self.model._meta.fields):
83                 row.append(attrs.get(field.db_column, None))
84             yield row
85
86 class QuerySet(BaseQuerySet):
87     def __init__(self, model=None, query=None):
88         if not query:
89             query = Query(model, None)
90         super(QuerySet, self).__init__(model, query)
91