From 3478a2cf6d8e9f05f9abccea01652170ce67f210 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 1 Jul 2020 18:53:13 +0200 Subject: [PATCH] admin: add filter to hide classes with `__debug_only__` when Debug is disabled --- passbook/lib/utils/reflection.py | 14 +++++++------- passbook/policies/dummy/models.py | 2 ++ passbook/stages/dummy/models.py | 2 ++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/passbook/lib/utils/reflection.py b/passbook/lib/utils/reflection.py index 5fc72f948..0a218f6fd 100644 --- a/passbook/lib/utils/reflection.py +++ b/passbook/lib/utils/reflection.py @@ -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 diff --git a/passbook/policies/dummy/models.py b/passbook/policies/dummy/models.py index 5e7ca8c8b..a8172157a 100644 --- a/passbook/policies/dummy/models.py +++ b/passbook/policies/dummy/models.py @@ -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) diff --git a/passbook/stages/dummy/models.py b/passbook/stages/dummy/models.py index b934a9d10..3dfb1d891 100644 --- a/passbook/stages/dummy/models.py +++ b/passbook/stages/dummy/models.py @@ -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"