Skip to content

Commit

Permalink
Merge pull request #590 from foarsitter/unused_variable_levels
Browse files Browse the repository at this point in the history
Levels variable is never set
  • Loading branch information
foarsitter authored Apr 3, 2024
2 parents 53941ec + ae562b4 commit 714632e
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions model_utils/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def __init__(self, *args, **kwargs):
self._iterable_class = InheritanceIterable

def select_subclasses(self, *subclasses):
levels = None
calculated_subclasses = self._get_subclasses_recurse(
self.model, levels=levels)
calculated_subclasses = self._get_subclasses_recurse(self.model)
# if none were passed in, we can just short circuit and select all
if not subclasses:
subclasses = calculated_subclasses
Expand All @@ -60,8 +58,7 @@ def select_subclasses(self, *subclasses):
continue

if not isinstance(subclass, (str,)):
subclass = self._get_ancestors_path(
subclass, levels=levels)
subclass = self._get_ancestors_path(subclass)

if subclass in calculated_subclasses:
verified_subclasses.append(subclass)
Expand Down Expand Up @@ -101,7 +98,7 @@ def annotate(self, *args, **kwargs):
qset._annotated = [a.default_alias for a in args] + list(kwargs.keys())
return qset

def _get_subclasses_recurse(self, model, levels=None):
def _get_subclasses_recurse(self, model):
"""
Given a Model class, find all related objects, exploring children
recursively, returning a `list` of strings representing the
Expand All @@ -120,18 +117,14 @@ def _get_subclasses_recurse(self, model, levels=None):
]

subclasses = []
if levels:
levels -= 1

for rel in rels:
if levels or levels is None:
for subclass in self._get_subclasses_recurse(
rel.field.model, levels=levels):
subclasses.append(
rel.get_accessor_name() + LOOKUP_SEP + subclass)
for subclass in self._get_subclasses_recurse(rel.field.model):
subclasses.append(rel.get_accessor_name() + LOOKUP_SEP + subclass)
subclasses.append(rel.get_accessor_name())
return subclasses

def _get_ancestors_path(self, model, levels=None):
def _get_ancestors_path(self, model):
"""
Serves as an opposite to _get_subclasses_recurse, instead walking from
the Model class up the Model's ancestry and constructing the desired
Expand All @@ -144,17 +137,14 @@ def _get_ancestors_path(self, model, levels=None):
ancestry = []
# should be a OneToOneField or None
parent_link = model._meta.get_ancestor_link(self.model)
if levels:
levels -= 1

while parent_link is not None:
related = parent_link.remote_field
ancestry.insert(0, related.get_accessor_name())
if levels or levels is None:
parent_model = related.model
parent_link = parent_model._meta.get_ancestor_link(
self.model)
else:
parent_link = None

parent_model = related.model
parent_link = parent_model._meta.get_ancestor_link(self.model)

return LOOKUP_SEP.join(ancestry)

def _get_sub_obj_recurse(self, obj, s):
Expand Down

0 comments on commit 714632e

Please sign in to comment.