1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of Bolloré telecom nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 from django.db.models.query import QuerySet as BaseQuerySet
36 from django.db.models.sql import Query
37 from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as BaseConstraint, AND, OR
39 from ldapdb.models.fields import CharField
41 class Constraint(BaseConstraint):
43 An object that can be passed to WhereNode.add() and knows how to
44 pre-process itself prior to including in the WhereNode.
47 - we redefine this class, because when self.field is None calls
48 Field().get_db_prep_lookup(), which short-circuits our LDAP-specific code.
50 def process(self, lookup_type, value, connection):
52 Returns a tuple of data suitable for inclusion in a WhereNode
55 # Because of circular imports, we need to import this here.
56 from django.db.models.base import ObjectDoesNotExist
60 params = self.field.get_db_prep_lookup(lookup_type, value,
61 connection=connection, prepared=True)
62 db_type = self.field.db_type()
64 params = CharField().get_db_prep_lookup(lookup_type, value,
65 connection=connection, prepared=True)
67 except ObjectDoesNotExist:
68 raise EmptyShortCircuit
70 return (self.alias, self.col, db_type), params
72 class WhereNode(BaseWhereNode):
73 def add(self, data, connector):
74 if not isinstance(data, (list, tuple)):
75 super(WhereNode, self).add(data, connector)
78 # we replace the native Constraint by our own
79 obj, lookup_type, value = data
80 if hasattr(obj, "process"):
81 obj = Constraint(obj.alias, obj.col, obj.field)
82 super(WhereNode, self).add((obj, lookup_type, value), connector)
84 class QuerySet(BaseQuerySet):
85 def __init__(self, model=None, query=None, using=None):
87 query = Query(model, WhereNode)
88 super(QuerySet, self).__init__(model=model, query=query, using=using)