From 80d90b91e8ee18a011be9297ad62ded6ab41b95b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 7 May 2020 00:05:10 +0200 Subject: [PATCH] core: add general admin.py loader, remove individual files --- passbook/audit/admin.py | 5 ----- passbook/core/admin.py | 25 ++++++++++++++++++++++--- passbook/crypto/admin.py | 5 ----- passbook/factors/captcha/admin.py | 5 ----- passbook/factors/dummy/admin.py | 5 ----- passbook/factors/email/admin.py | 5 ----- passbook/factors/password/admin.py | 5 ----- passbook/lib/admin.py | 22 ---------------------- passbook/policies/expiry/admin.py | 5 ----- passbook/policies/expression/admin.py | 5 ----- passbook/policies/hibp/admin.py | 5 ----- passbook/policies/password/admin.py | 4 ---- passbook/policies/reputation/admin.py | 5 ----- passbook/policies/webhook/admin.py | 4 ---- passbook/providers/app_gw/admin.py | 5 ----- passbook/providers/oauth/admin.py | 4 ---- passbook/providers/oidc/admin.py | 5 ----- passbook/providers/saml/admin.py | 5 ----- passbook/root/settings.py | 3 ++- passbook/sources/ldap/admin.py | 5 ----- passbook/sources/oauth/admin.py | 5 ----- passbook/sources/saml/admin.py | 5 ----- 22 files changed, 24 insertions(+), 118 deletions(-) delete mode 100644 passbook/audit/admin.py delete mode 100644 passbook/crypto/admin.py delete mode 100644 passbook/factors/captcha/admin.py delete mode 100644 passbook/factors/dummy/admin.py delete mode 100644 passbook/factors/email/admin.py delete mode 100644 passbook/factors/password/admin.py delete mode 100644 passbook/lib/admin.py delete mode 100644 passbook/policies/expiry/admin.py delete mode 100644 passbook/policies/expression/admin.py delete mode 100644 passbook/policies/hibp/admin.py delete mode 100644 passbook/policies/password/admin.py delete mode 100644 passbook/policies/reputation/admin.py delete mode 100644 passbook/policies/webhook/admin.py delete mode 100644 passbook/providers/app_gw/admin.py delete mode 100644 passbook/providers/oauth/admin.py delete mode 100644 passbook/providers/oidc/admin.py delete mode 100644 passbook/providers/saml/admin.py delete mode 100644 passbook/sources/ldap/admin.py delete mode 100644 passbook/sources/oauth/admin.py delete mode 100644 passbook/sources/saml/admin.py diff --git a/passbook/audit/admin.py b/passbook/audit/admin.py deleted file mode 100644 index 82a532499..000000000 --- a/passbook/audit/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""passbook audit model admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_audit") diff --git a/passbook/core/admin.py b/passbook/core/admin.py index 6e91b2080..d32203090 100644 --- a/passbook/core/admin.py +++ b/passbook/core/admin.py @@ -1,5 +1,24 @@ -"""passbook core model admin""" +"""passbook core admin""" -from passbook.lib.admin import admin_autoregister +from django.apps import AppConfig, apps +from django.contrib import admin +from django.contrib.admin.sites import AlreadyRegistered +from guardian.admin import GuardedModelAdmin +from structlog import get_logger -admin_autoregister("passbook_core") +LOGGER = get_logger() + + +def admin_autoregister(app: AppConfig): + """Automatically register all models from app""" + for model in app.get_models(): + try: + admin.site.register(model, GuardedModelAdmin) + except AlreadyRegistered: + pass + + +for app in apps.get_app_configs(): + if app.label.startswith("passbook_"): + LOGGER.debug("Registering application for dj-admin", app=app.label) + admin_autoregister(app) diff --git a/passbook/crypto/admin.py b/passbook/crypto/admin.py deleted file mode 100644 index 5179b0d07..000000000 --- a/passbook/crypto/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""passbook crypto model admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_crypto") diff --git a/passbook/factors/captcha/admin.py b/passbook/factors/captcha/admin.py deleted file mode 100644 index 88f03befa..000000000 --- a/passbook/factors/captcha/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""captcha factor admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_factors_captcha") diff --git a/passbook/factors/dummy/admin.py b/passbook/factors/dummy/admin.py deleted file mode 100644 index 2499671fe..000000000 --- a/passbook/factors/dummy/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""dummy factor admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_factors_dummy") diff --git a/passbook/factors/email/admin.py b/passbook/factors/email/admin.py deleted file mode 100644 index e5343ef52..000000000 --- a/passbook/factors/email/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""email factor admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_factors_email") diff --git a/passbook/factors/password/admin.py b/passbook/factors/password/admin.py deleted file mode 100644 index 2da557451..000000000 --- a/passbook/factors/password/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""password factor admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_factors_password") diff --git a/passbook/lib/admin.py b/passbook/lib/admin.py deleted file mode 100644 index c0ddefa72..000000000 --- a/passbook/lib/admin.py +++ /dev/null @@ -1,22 +0,0 @@ -"""passbook core admin""" - -from django.apps import apps -from django.contrib import admin -from django.contrib.admin.sites import AlreadyRegistered -from django.contrib.auth.admin import UserAdmin -from guardian.admin import GuardedModelAdmin - -from passbook.core.models import User - - -def admin_autoregister(app): - """Automatically register all models from app""" - app_models = apps.get_app_config(app).get_models() - for model in app_models: - try: - admin.site.register(model, GuardedModelAdmin) - except AlreadyRegistered: - pass - - -admin.site.register(User, UserAdmin) diff --git a/passbook/policies/expiry/admin.py b/passbook/policies/expiry/admin.py deleted file mode 100644 index c9aa9e577..000000000 --- a/passbook/policies/expiry/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Passbook passbook expiry policy Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_policies_expiry") diff --git a/passbook/policies/expression/admin.py b/passbook/policies/expression/admin.py deleted file mode 100644 index eb5a93a7f..000000000 --- a/passbook/policies/expression/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Passbook passbook expression policy Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_policies_expression") diff --git a/passbook/policies/hibp/admin.py b/passbook/policies/hibp/admin.py deleted file mode 100644 index c6a064c44..000000000 --- a/passbook/policies/hibp/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Passbook HIBP Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_policies_hibp") diff --git a/passbook/policies/password/admin.py b/passbook/policies/password/admin.py deleted file mode 100644 index 11d83aeca..000000000 --- a/passbook/policies/password/admin.py +++ /dev/null @@ -1,4 +0,0 @@ -"""autodiscover admin""" -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_policies_password") diff --git a/passbook/policies/reputation/admin.py b/passbook/policies/reputation/admin.py deleted file mode 100644 index bfe52af9d..000000000 --- a/passbook/policies/reputation/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Passbook reputation Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_policies_reputation") diff --git a/passbook/policies/webhook/admin.py b/passbook/policies/webhook/admin.py deleted file mode 100644 index 3fad9a10d..000000000 --- a/passbook/policies/webhook/admin.py +++ /dev/null @@ -1,4 +0,0 @@ -"""autodiscover admin""" -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_policies_webhook") diff --git a/passbook/providers/app_gw/admin.py b/passbook/providers/app_gw/admin.py deleted file mode 100644 index a0fe583ea..000000000 --- a/passbook/providers/app_gw/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""passbook Application Security Gateway model admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_providers_app_gw") diff --git a/passbook/providers/oauth/admin.py b/passbook/providers/oauth/admin.py deleted file mode 100644 index 6f0f106db..000000000 --- a/passbook/providers/oauth/admin.py +++ /dev/null @@ -1,4 +0,0 @@ -"""oauth provider admin""" -# from passbook.lib.admin import admin_autoregister - -# admin_autoregister('passbook_providers_oauth') diff --git a/passbook/providers/oidc/admin.py b/passbook/providers/oidc/admin.py deleted file mode 100644 index 338db4948..000000000 --- a/passbook/providers/oidc/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""oidc provider admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_providers_oidc") diff --git a/passbook/providers/saml/admin.py b/passbook/providers/saml/admin.py deleted file mode 100644 index 713248957..000000000 --- a/passbook/providers/saml/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""SAML IDP Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_providers_saml") diff --git a/passbook/root/settings.py b/passbook/root/settings.py index b60197ab7..fcd8b0d98 100644 --- a/passbook/root/settings.py +++ b/passbook/root/settings.py @@ -79,7 +79,6 @@ INSTALLED_APPS = [ "drf_yasg", "guardian", "django_prometheus", - "passbook.core.apps.PassbookCoreConfig", "passbook.static.apps.PassbookStaticConfig", "passbook.admin.apps.PassbookAdminConfig", "passbook.api.apps.PassbookAPIConfig", @@ -384,3 +383,5 @@ for _app in INSTALLED_APPS: if DEBUG: INSTALLED_APPS.append("debug_toolbar") MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware") + +INSTALLED_APPS.append("passbook.core.apps.PassbookCoreConfig") diff --git a/passbook/sources/ldap/admin.py b/passbook/sources/ldap/admin.py deleted file mode 100644 index 0656c92e1..000000000 --- a/passbook/sources/ldap/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Passbook LDAP Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_sources_ldap") diff --git a/passbook/sources/oauth/admin.py b/passbook/sources/oauth/admin.py deleted file mode 100644 index bd9ad0587..000000000 --- a/passbook/sources/oauth/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""passbook oauth_client admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_sources_oauth") diff --git a/passbook/sources/saml/admin.py b/passbook/sources/saml/admin.py deleted file mode 100644 index 010b830a6..000000000 --- a/passbook/sources/saml/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""SAML SP Admin""" - -from passbook.lib.admin import admin_autoregister - -admin_autoregister("passbook_sources_saml")