Skip to content
Snippets Groups Projects
Commit 17ab618f authored by yamijuan's avatar yamijuan
Browse files

Added support to AppConfig notation in INSTALLED_APPS settings

parent dd3559f6
No related branches found
No related tags found
No related merge requests found
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:
......
......@@ -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'
]
......
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[:len(app_split) - 2])
if new_app in apps_names:
return new_app
else:
raise ImproperlyConfigured(
"The application {0} is not in the configured apps or does" +
"not have the pattern app.apps.AppConfig".format(app_path)
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment