Skip to content

Commit 3db4abd

Browse files
committed
remove class hierarchy walks
1 parent ed8db2d commit 3db4abd

File tree

3 files changed

+10
-80
lines changed

3 files changed

+10
-80
lines changed

src/polymorphic/admin/helpers.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def __iter__(self):
6363
for form in self.formset.extra_forms + self.formset.empty_forms:
6464
model = form._meta.model
6565
child_inline = self.opts.get_child_inline_instance(model)
66-
6766
yield PolymorphicInlineAdminForm(
6867
formset=self.formset,
6968
form=form,
@@ -142,26 +141,3 @@ def get_inline_formsets(self, request, formsets, inline_instances, obj=None, *ar
142141
admin_formset.request = request
143142
admin_formset.obj = obj
144143
return inline_admin_formsets
145-
146-
147-
def get_leaf_subclasses(cls, exclude=None):
148-
"Get leaf subclasses of `cls` class"
149-
150-
if exclude is None:
151-
exclude = ()
152-
153-
elif not isinstance(exclude, (list, tuple)):
154-
# Accept single instance in `exclude`
155-
exclude = (exclude,)
156-
157-
result = []
158-
159-
subclasses = cls.__subclasses__()
160-
161-
if subclasses:
162-
for subclass in subclasses:
163-
result.extend(get_leaf_subclasses(subclass, exclude))
164-
elif not (cls in exclude or (hasattr(cls, "_meta") and cls._meta.abstract)):
165-
result.append(cls)
166-
167-
return result

src/polymorphic/admin/inlines.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from polymorphic.formsets.utils import add_media
2222

23-
from .helpers import PolymorphicInlineSupportMixin, get_leaf_subclasses
23+
from .helpers import PolymorphicInlineSupportMixin
2424

2525

2626
class PolymorphicInlineModelAdmin(InlineModelAdmin):
@@ -86,35 +86,14 @@ def __init__(self, parent_model, admin_site):
8686

8787
def get_child_inlines(self):
8888
"""
89-
Return the derived inline classes which this admin should handle
89+
Return the derived inline classes which this admin should handle.
9090
9191
This should return a list of tuples, exactly like
9292
:attr:`child_inlines` is.
93-
94-
The inline classes can be retrieved as
95-
``base_inline.__subclasses__()``, a setting in a config file, or
96-
a query of a plugin registration system at your option
9793
"""
9894
if self.child_inlines is not None:
9995
return self.child_inlines
100-
101-
child_inlines = get_leaf_subclasses(
102-
PolymorphicInlineModelAdmin.Child, self.exclude_children
103-
)
104-
child_inlines = tuple(
105-
inline
106-
for inline in child_inlines
107-
if (inline.model is not None and issubclass(inline.model, self.model))
108-
)
109-
110-
if child_inlines:
111-
return child_inlines
112-
113-
raise ImproperlyConfigured(
114-
f"No child inlines found for '{self.model.__name__}', please "
115-
"define the 'child_inlines' attribute or overwrite the "
116-
"'get_child_inlines()' method."
117-
)
96+
return ()
11897

11998
def get_child_inline_instances(self):
12099
"""

src/polymorphic/admin/parentadmin.py

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from polymorphic.utils import get_base_polymorphic_model
2020

2121
from .forms import PolymorphicModelChoiceForm
22-
from .helpers import get_leaf_subclasses
2322

2423

2524
class RegistrationClosed(RuntimeError):
@@ -52,13 +51,6 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
5251
#: The child models that should be displayed
5352
child_models = None
5453

55-
#: The models that should be excluded from the auto-discovered child
56-
#: leaf models that should be displayed. This can be a list of
57-
#: models or a single model. It's useful to exclude non-abstract
58-
#: base models (abstract models are always excluded) when they don't
59-
#: have defined any child models.
60-
exclude_children = None
61-
6254
#: Whether the list should be polymorphic too, leave to ``False`` to optimize
6355
polymorphic_list = False
6456

@@ -117,41 +109,24 @@ def register_child(self, model, model_admin):
117109
def get_child_models(self):
118110
"""
119111
Return the derived model classes which this admin should handle.
112+
This should return a list of tuples, exactly like :attr:`child_models` is.
120113
121-
This should return a list of tuples, exactly like
122-
:attr:`child_models` is.
123-
124-
The model classes can be retrieved as
125-
``base_model.__subclasses__()``, a setting in a config file, or
126-
a query of a plugin registration system at your option
114+
The model classes can be retrieved as ``base_model.__subclasses__()``,
115+
a setting in a config file, or a query of a plugin registration system at your option
127116
"""
128-
if self.child_models is not None:
129-
return self.child_models
117+
if self.child_models is None:
118+
raise NotImplementedError("Implement get_child_models() or child_models")
130119

131-
child_models = get_leaf_subclasses(self.base_model, self.exclude_children)
132-
133-
if child_models:
134-
return child_models
135-
136-
raise ImproperlyConfigured(
137-
f"No child models found for '{self.base_model.__name__}', please "
138-
"define the 'child_models' attribute or overwrite the "
139-
"'get_child_models' method."
140-
)
120+
return self.child_models
141121

142122
def get_child_type_choices(self, request, action):
143123
"""
144124
Return a list of polymorphic types for which the user has the permission to perform the given action.
145125
"""
146126
self._lazy_setup()
147-
148-
child_models = self._child_models
149-
if not child_models:
150-
raise ImproperlyConfigured("No child models are available.")
151-
152127
choices = []
153128
content_types = ContentType.objects.get_for_models(
154-
*child_models, for_concrete_models=False
129+
*self.get_child_models(), for_concrete_models=False
155130
)
156131

157132
for model, ct in content_types.items():

0 commit comments

Comments
 (0)