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 -*-
23 from copy import deepcopy
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
33 class WhereNode(BaseWhereNode):
36 for item in self.children:
37 if isinstance(item, WhereNode):
38 bits.append(item.as_sql())
40 table, column, type, x, y, values = item
42 bits.append('(!(%s=%s))' % (column,values[0]))
44 bits.append('(%s=%s)' % (column,values[0]))
47 elif self.connector == AND:
48 return '(&%s)' % ''.join(bits)
49 elif self.connector == OR:
50 return '(|%s)' % ''.join(bits)
52 raise Exception("Unhandled WHERE connector: %s" % self.connector)
54 class Query(BaseQuery):
55 def results_iter(self):
56 # FIXME: use all object classes
57 filterstr = '(objectClass=%s)' % self.model._meta.object_classes[0]
58 filterstr += self.where.as_sql()
59 filterstr = '(&%s)' % filterstr
60 attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ]
63 vals = ldapdb.connection.search_s(
70 raise self.model.DoesNotExist
73 if self.extra_order_by:
74 ordering = self.extra_order_by
75 elif not self.default_ordering:
76 ordering = self.order_by
78 ordering = self.order_by or self.model._meta.ordering
82 attr = self.model._meta.get_field(k).db_column
83 keys.append(x[1][attr])
85 vals = sorted(vals, key=lambda x: getkey(x))
88 for dn, attrs in vals:
90 for field in iter(self.model._meta.fields):
91 row.append(attrs.get(field.db_column, None))
94 class QuerySet(BaseQuerySet):
95 def __init__(self, model=None, query=None):
97 query = Query(model, None, WhereNode)
98 super(QuerySet, self).__init__(model, query)