Changeset 7169
- Timestamp:
- 02/27/08 20:49:31 (9 months ago)
- Files:
-
- django/branches/queryset-refactor/django/db/models/query.py (modified) (3 diffs)
- django/branches/queryset-refactor/django/db/models/query_utils.py (modified) (3 diffs)
- django/branches/queryset-refactor/django/db/models/sql/where.py (modified) (1 diff)
- django/branches/queryset-refactor/django/utils/tree.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/queryset-refactor/django/db/models/query.py
r7165 r7169 3 3 from django.db import connection, transaction 4 4 from django.db.models.fields import DateField, FieldDoesNotExist 5 from django.db.models.query_utils import Q, QNot, EmptyResultSet5 from django.db.models.query_utils import Q, EmptyResultSet, not_q 6 6 from django.db.models import signals, sql 7 7 from django.dispatch import dispatcher … … 317 317 set. 318 318 """ 319 return self._filter_or_exclude( QNot, *args, **kwargs)319 return self._filter_or_exclude(not_q, *args, **kwargs) 320 320 321 321 def _filter_or_exclude(self, mapper, *args, **kwargs): … … 592 592 yield iter([]).next() 593 593 594 # QOperator, Q And and QOr are temporarily retained for backwards compatibility.595 # All the old functionality is now part of the 'Q' class.594 # QOperator, QNot, QAnd and QOr are temporarily retained for backwards 595 # compatibility. All the old functionality is now part of the 'Q' class. 596 596 class QOperator(Q): 597 597 def __init__(self, *args, **kwargs): 598 598 warnings.warn('Use Q instead of QOr, QAnd or QOperation.', 599 599 DeprecationWarning, stacklevel=2) 600 super(QOperator, self).__init__(*args, **kwargs) 600 601 601 602 QOr = QAnd = QOperator 603 604 def QNot(q): 605 warnings.warn('Use ~q instead of QNot(q)', DeprecationWarning, stacklevel=2) 606 return ~q 602 607 603 608 def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0, django/branches/queryset-refactor/django/db/models/query_utils.py
r6518 r7169 5 5 modules without getting into circular import difficulties. 6 6 """ 7 8 from copy import deepcopy 9 7 10 from django.utils import tree 8 11 … … 32 35 if not isinstance(other, Q): 33 36 raise TypeError(other) 34 self.add(other, conn) 35 return self 37 obj = deepcopy(self) 38 obj.add(other, conn) 39 return obj 36 40 37 41 def __or__(self, other): … … 42 46 43 47 def __invert__(self): 44 return QNot(self) 48 obj = deepcopy(self) 49 obj.negate() 50 return obj 45 51 46 class QNot(Q): 47 """ 48 Encapsulates the negation of a Q object. 49 """ 50 def __init__(self, q): 51 """Creates the negation of the Q object passed in.""" 52 super(QNot, self).__init__() 53 self.add(q, self.AND) 54 self.negate() 52 def not_q(q): 53 return ~q 55 54 56 def __invert__(self):57 return self.children[0]58 django/branches/queryset-refactor/django/db/models/sql/where.py
r7088 r7169 50 50 elif isinstance(child, tree.Node): 51 51 sql, params = self.as_sql(child, qn) 52 if child.negated:53 format = ' NOT (%s)'52 if len(child.children) == 1: 53 format = '%s' 54 54 else: 55 55 format = '(%s)' 56 if child.negated: 57 format = 'NOT %s' % format 56 58 else: 57 59 sql, params = self.make_atom(child, qn) django/branches/queryset-refactor/django/utils/tree.py
r6957 r7169 4 4 """ 5 5 6 importcopy6 from copy import deepcopy 7 7 8 8 class Node(object): … … 16 16 default = 'DEFAULT' 17 17 18 def __init__(self, children=None, connector=None ):18 def __init__(self, children=None, connector=None, negated=False): 19 19 self.children = children and children[:] or [] 20 20 self.connector = connector or self.default 21 21 self.subtree_parents = [] 22 self.negated = False22 self.negated = negated 23 23 24 24 def __str__(self): 25 if self.negated: 26 return '(NOT (%s: %s))' % (self.connector, ', '.join([str(c) for c 27 in self.children])) 25 28 return '(%s: %s)' % (self.connector, ', '.join([str(c) for c in 26 self.children]))29 self.children])) 27 30 28 31 def __deepcopy__(self, memodict): … … 30 33 Utility method used by copy.deepcopy(). 31 34 """ 32 obj = self.__class__(connector=self.connector)33 obj. children = copy.deepcopy(self.children, memodict)34 obj. subtree_parents = copy.deepcopy(self.subtree_parents, memodict)35 obj. negated = self.negated35 obj = Node(connector=self.connector, negated=self.negated) 36 obj.__class__ = self.__class__ 37 obj.children = deepcopy(self.children, memodict) 38 obj.subtree_parents = deepcopy(self.subtree_parents, memodict) 36 39 return obj 37 40 … … 64 67 self.connector = conn_type 65 68 if self.connector == conn_type: 66 if isinstance(node, Node) and (node.connector == conn_type 67 orlen(node) == 1):69 if isinstance(node, Node) and (node.connector == conn_type or 70 len(node) == 1): 68 71 self.children.extend(node.children) 69 72 else: 70 73 self.children.append(node) 71 74 else: 72 obj = Node(self.children, self.connector )75 obj = Node(self.children, self.connector, self.negated) 73 76 self.connector = conn_type 74 77 self.children = [obj, node] … … 81 84 method is useful for implementing "not" arrangements. 82 85 """ 83 self.children = [NegatedNode(self.children, self.connector, 84 old_state=self.negated)] 86 self.children = [Node(self.children, self.connector, not self.negated)] 85 87 self.connector = self.default 86 88 … … 94 96 self.connector = conn_type 95 97 elif self.connector != conn_type: 96 self.children = [Node(self.children, self.connector )]98 self.children = [Node(self.children, self.connector, self.negated)] 97 99 self.connector = conn_type 100 self.negated = False 98 101 99 self.subtree_parents.append(Node(self.children, self.connector)) 102 self.subtree_parents.append(Node(self.children, self.connector, 103 self.negated)) 100 104 self.connector = self.default 105 self.negated = False 101 106 self.children = [] 102 107 … … 111 116 node = Node(self.children, self.connector) 112 117 self.connector = obj.connector 118 self.negated = obj.negated 113 119 self.children = obj.children 114 120 self.children.append(node) 115 121 116 class NegatedNode(Node):117 """118 A class that indicates the connector type should be negated (whatever that119 means -- it's up to the client) when used by the client code.120 """121 def __init__(self, children=None, connector=None, old_state=True):122 super(NegatedNode, self).__init__(children, connector)123 self.negated = not old_state124
