From 78d343cb76a3b0619ce12c332ce2f5e46250231a Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Thu, 2 Jul 2015 16:01:57 -0300 Subject: [PATCH] Remove duplicate code from generic admin setup --- base/admin.py | 11 ++--------- comissoes/admin.py | 11 ++--------- lexml/admin.py | 11 ++--------- materia/admin.py | 11 ++--------- norma/admin.py | 11 ++--------- parlamentares/admin.py | 11 ++--------- protocoloadm/admin.py | 11 ++--------- sapl/utils.py | 13 +++++++++++++ sessao/admin.py | 11 ++--------- 9 files changed, 29 insertions(+), 72 deletions(-) create mode 100644 sapl/utils.py diff --git a/base/admin.py b/base/admin.py index 17d47f4c6..483f7357b 100644 --- a/base/admin.py +++ b/base/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/comissoes/admin.py b/comissoes/admin.py index 17d47f4c6..483f7357b 100644 --- a/comissoes/admin.py +++ b/comissoes/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/lexml/admin.py b/lexml/admin.py index 17d47f4c6..483f7357b 100644 --- a/lexml/admin.py +++ b/lexml/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/materia/admin.py b/materia/admin.py index 17d47f4c6..483f7357b 100644 --- a/materia/admin.py +++ b/materia/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/norma/admin.py b/norma/admin.py index 17d47f4c6..483f7357b 100644 --- a/norma/admin.py +++ b/norma/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/parlamentares/admin.py b/parlamentares/admin.py index 17d47f4c6..483f7357b 100644 --- a/parlamentares/admin.py +++ b/parlamentares/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/protocoloadm/admin.py b/protocoloadm/admin.py index 17d47f4c6..483f7357b 100644 --- a/protocoloadm/admin.py +++ b/protocoloadm/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__) diff --git a/sapl/utils.py b/sapl/utils.py new file mode 100644 index 000000000..da019dfb9 --- /dev/null +++ b/sapl/utils.py @@ -0,0 +1,13 @@ +from django.apps import apps +from django.contrib import admin + + +def register_all_models_in_admin(module_name): + appname = module_name.split('.')[0] + app = apps.get_app_config(appname) + for model in app.get_models(): + class CustomModelAdmin(admin.ModelAdmin): + list_display = [f.name for f in model._meta.fields if f.name != 'id'] + + if not admin.site.is_registered(model): + admin.site.register(model, CustomModelAdmin) diff --git a/sessao/admin.py b/sessao/admin.py index 17d47f4c6..483f7357b 100644 --- a/sessao/admin.py +++ b/sessao/admin.py @@ -1,11 +1,4 @@ -from django.apps import apps -from django.contrib import admin +from sapl.utils import register_all_models_in_admin -appname = __name__.split('.')[0] -app = apps.get_app_config(appname) -for model in app.get_models(): - class CustomModelAdmin(admin.ModelAdmin): - list_display = [f.name for f in model._meta.fields if f.name != 'id'] - if not admin.site.is_registered(model): - admin.site.register(model, CustomModelAdmin) +register_all_models_in_admin(__name__)