diff --git a/menu_generator/templatetags/utils.py b/menu_generator/templatetags/utils.py index bcdee11d681a8a0bec53256f7b535a7ec437c8f7..a59caf3fb608771eab0cffe4082e23b82583eaab 100644 --- a/menu_generator/templatetags/utils.py +++ b/menu_generator/templatetags/utils.py @@ -1,5 +1,5 @@ from django.conf import settings -from ..utils import get_callable +from ..utils import get_callable, clean_app_config MENU_DICT = ".menus.MENUS" @@ -14,8 +14,9 @@ def get_menu_from_apps(menu_name): installed_apps = getattr(settings, "INSTALLED_APPS", []) menu_list = [] for app in installed_apps: + cleaned_app = clean_app_config(app) try: - all_menus_dict = get_callable(app + MENU_DICT) + all_menus_dict = get_callable(cleaned_app + MENU_DICT) except ImportError: all_menus_dict = None except AttributeError: diff --git a/menu_generator/tests/test_apps/app1/__init__.py b/menu_generator/tests/test_apps/app1/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..799abb991742c5655bf037b25b9a273d4c464095 100755 --- a/menu_generator/tests/test_apps/app1/__init__.py +++ b/menu_generator/tests/test_apps/app1/__init__.py @@ -0,0 +1 @@ +default_app_config = 'app1.apps.MyAppConfig' diff --git a/menu_generator/tests/test_apps/app1/apps.py b/menu_generator/tests/test_apps/app1/apps.py new file mode 100644 index 0000000000000000000000000000000000000000..b2f212fc990a5d517846a971e673c38825ef99e9 --- /dev/null +++ b/menu_generator/tests/test_apps/app1/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MyAppConfig(AppConfig): + name = 'menu_generator.tests.test_apps.app1' + verbose_name = 'app 1' diff --git a/menu_generator/tests/testsettings.py b/menu_generator/tests/testsettings.py index 07e94044a12d70b58eeac4146a35fe76693e2932..46d87dd1ed2b9e1c570e79cf125c85621cfbb50f 100755 --- a/menu_generator/tests/testsettings.py +++ b/menu_generator/tests/testsettings.py @@ -7,7 +7,7 @@ DATABASES = { SECRET_KEY = "r4dy" INSTALLED_APPS = [ 'menu_generator', - 'menu_generator.tests.test_apps.app1', + 'menu_generator.tests.test_apps.app1.apps.MyAppConfig', 'menu_generator.tests.test_apps.app2', 'menu_generator.tests.test_apps.app3' ] diff --git a/menu_generator/utils.py b/menu_generator/utils.py index 74dbef66fd4debb94c65a99a334e14288d96b1d5..d761bdc5f5f64ef76e7efc969a191a7f92811f8c 100755 --- a/menu_generator/utils.py +++ b/menu_generator/utils.py @@ -1,5 +1,8 @@ from importlib import import_module +from django.apps import apps +from django.core.exceptions import ImproperlyConfigured + def get_callable(func_or_path): """ @@ -13,3 +16,22 @@ def get_callable(func_or_path): _module = import_module(module_name) func = getattr(_module, function_name) return func + + +def clean_app_config(app_path): + """ + Removes the AppConfig path for this app and returns the new string + """ + apps_names = [app.name for app in apps.get_app_configs()] + if app_path in apps_names: + return app_path + else: + app_split = app_path.split('.') + new_app = '.'.join(app_split[:-2]) + if new_app in apps_names: + return new_app + else: # pragma: no cover + raise ImproperlyConfigured( + "The application {0} is not in the configured apps or does" + + "not have the pattern app.apps.AppConfig".format(app_path) + )