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""" """passbook lib reflection utilities"""
from importlib import import_module from importlib import import_module
from django.conf import settings
def all_subclasses(cls, sort=True): def all_subclasses(cls, sort=True):
"""Recursively return all subclassess of cls""" """Recursively return all subclassess of cls"""
classes = set(cls.__subclasses__()).union( classes = set(cls.__subclasses__()).union(
[s for c in cls.__subclasses__() for s in all_subclasses(c, sort=sort)] [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: if sort:
return sorted(classes, key=lambda x: x.__name__) return sorted(classes, key=lambda x: x.__name__)
return classes return classes
@ -34,10 +41,3 @@ def get_apps():
for _app in apps.get_app_configs(): for _app in apps.get_app_configs():
if _app.name.startswith("passbook"): if _app.name.startswith("passbook"):
yield _app 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, """Policy used for debugging the PolicyEngine. Returns a fixed result,
but takes a random time to process.""" but takes a random time to process."""
__debug_only__ = True
result = models.BooleanField(default=False) result = models.BooleanField(default=False)
wait_min = models.IntegerField(default=5) wait_min = models.IntegerField(default=5)
wait_max = models.IntegerField(default=30) wait_max = models.IntegerField(default=30)

View file

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