admin: add filter to hide classes with `__debug_only__` when Debug is disabled

This commit is contained in:
Jens Langhammer 2020-07-01 18:53:13 +02:00
parent 3b70d12a5f
commit 3478a2cf6d
3 changed files with 11 additions and 7 deletions

View File

@ -1,12 +1,19 @@
"""passbook lib reflection utilities"""
from importlib import import_module
from django.conf import settings
def all_subclasses(cls, sort=True):
"""Recursively return all subclassess of cls"""
classes = set(cls.__subclasses__()).union(
[s for c in cls.__subclasses__() for s in all_subclasses(c, sort=sort)]
)
# Check if we're in debug mode, if not exclude classes which have `__debug_only__`
if not settings.DEBUG:
# Filter class out when __debug_only__ is not False
classes = [x for x in classes if not getattr(x, "__debug_only__", False)]
# classes = filter(lambda x: not getattr(x, "__debug_only__", False), classes)
if sort:
return sorted(classes, key=lambda x: x.__name__)
return classes
@ -34,10 +41,3 @@ def get_apps():
for _app in apps.get_app_configs():
if _app.name.startswith("passbook"):
yield _app
def app(name):
"""Return true if app with `name` is enabled"""
from django.conf import settings
return name in settings.INSTALLED_APPS

View File

@ -16,6 +16,8 @@ class DummyPolicy(Policy):
"""Policy used for debugging the PolicyEngine. Returns a fixed result,
but takes a random time to process."""
__debug_only__ = True
result = models.BooleanField(default=False)
wait_min = models.IntegerField(default=5)
wait_max = models.IntegerField(default=30)

View File

@ -7,6 +7,8 @@ from passbook.flows.models import Stage
class DummyStage(Stage):
"""Used for debugging."""
__debug_only__ = True
type = "passbook.stages.dummy.stage.DummyStage"
form = "passbook.stages.dummy.forms.DummyStageForm"