From 47bdad4e6b3bf70aec0d19d65eeb6a7319ad045a Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Mon, 12 Jan 2015 19:55:57 -0500 Subject: [PATCH] Replaced inner functions by class methods. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #24031 Thanks to Tim Graham and MichaƂ Modzelewski for the review. --- django/db/models/query_utils.py | 26 ++++++++++++++------------ django/db/models/sql/where.py | 28 +++++++++++++++------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 9e4ddfec58..7751f988db 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -90,22 +90,24 @@ class Q(tree.Node): clause, _ = query._add_q(self, reuse, allow_joins=allow_joins) return clause - def refs_aggregate(self, existing_aggregates): - def _refs_aggregate(obj, existing_aggregates): - if not isinstance(obj, tree.Node): - aggregate, aggregate_lookups = refs_aggregate(obj[0].split(LOOKUP_SEP), existing_aggregates) - if not aggregate and hasattr(obj[1], 'refs_aggregate'): - return obj[1].refs_aggregate(existing_aggregates) + @classmethod + def _refs_aggregate(cls, obj, existing_aggregates): + if not isinstance(obj, tree.Node): + aggregate, aggregate_lookups = refs_aggregate(obj[0].split(LOOKUP_SEP), existing_aggregates) + if not aggregate and hasattr(obj[1], 'refs_aggregate'): + return obj[1].refs_aggregate(existing_aggregates) + return aggregate, aggregate_lookups + for c in obj.children: + aggregate, aggregate_lookups = cls._refs_aggregate(c, existing_aggregates) + if aggregate: return aggregate, aggregate_lookups - for c in obj.children: - aggregate, aggregate_lookups = _refs_aggregate(c, existing_aggregates) - if aggregate: - return aggregate, aggregate_lookups - return False, () + return False, () + def refs_aggregate(self, existing_aggregates): if not existing_aggregates: return False - return _refs_aggregate(self, existing_aggregates) + + return self._refs_aggregate(self, existing_aggregates) class DeferredAttribute(object): diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index cbb709dfc8..e3766c51d6 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -315,24 +315,26 @@ class WhereNode(tree.Node): clone.relabel_aliases(change_map) return clone + @classmethod + def _contains_aggregate(cls, obj): + if not isinstance(obj, tree.Node): + return getattr(obj.lhs, 'contains_aggregate', False) or getattr(obj.rhs, 'contains_aggregate', False) + return any(cls._contains_aggregate(c) for c in obj.children) + @cached_property def contains_aggregate(self): - def _contains_aggregate(obj): - if not isinstance(obj, tree.Node): - return getattr(obj.lhs, 'contains_aggregate', False) or getattr(obj.rhs, 'contains_aggregate', False) - return any(_contains_aggregate(c) for c in obj.children) + return self._contains_aggregate(self) - return _contains_aggregate(self) + @classmethod + def _refs_field(cls, obj, aggregate_types, field_types): + if not isinstance(obj, tree.Node): + if hasattr(obj.rhs, 'refs_field'): + return obj.rhs.refs_field(aggregate_types, field_types) + return False + return any(cls._refs_field(c, aggregate_types, field_types) for c in obj.children) def refs_field(self, aggregate_types, field_types): - def _refs_field(obj, aggregate_types, field_types): - if not isinstance(obj, tree.Node): - if hasattr(obj.rhs, 'refs_field'): - return obj.rhs.refs_field(aggregate_types, field_types) - return False - return any(_refs_field(c, aggregate_types, field_types) for c in obj.children) - - return _refs_field(self, aggregate_types, field_types) + return self._refs_field(self, aggregate_types, field_types) class EmptyWhere(WhereNode):