Skip to content
Snippets Groups Projects
Unverified Commit 70e67771 authored by Milton Lenis's avatar Milton Lenis Committed by GitHub
Browse files

Merge pull request #10 from yamijuan/juan-development

Added support for AppConfig notation in INSTALLED_APPS settings
parents c329e079 6630e78b
No related branches found
No related tags found
No related merge requests found
from django.conf import settings from django.conf import settings
from ..utils import get_callable from ..utils import get_callable, clean_app_config
MENU_DICT = ".menus.MENUS" MENU_DICT = ".menus.MENUS"
...@@ -14,8 +14,9 @@ def get_menu_from_apps(menu_name): ...@@ -14,8 +14,9 @@ def get_menu_from_apps(menu_name):
installed_apps = getattr(settings, "INSTALLED_APPS", []) installed_apps = getattr(settings, "INSTALLED_APPS", [])
menu_list = [] menu_list = []
for app in installed_apps: for app in installed_apps:
cleaned_app = clean_app_config(app)
try: try:
all_menus_dict = get_callable(app + MENU_DICT) all_menus_dict = get_callable(cleaned_app + MENU_DICT)
except ImportError: except ImportError:
all_menus_dict = None all_menus_dict = None
except AttributeError: except AttributeError:
......
default_app_config = 'app1.apps.MyAppConfig'
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'menu_generator.tests.test_apps.app1'
verbose_name = 'app 1'
...@@ -7,7 +7,7 @@ DATABASES = { ...@@ -7,7 +7,7 @@ DATABASES = {
SECRET_KEY = "r4dy" SECRET_KEY = "r4dy"
INSTALLED_APPS = [ INSTALLED_APPS = [
'menu_generator', '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.app2',
'menu_generator.tests.test_apps.app3' 'menu_generator.tests.test_apps.app3'
] ]
......
from importlib import import_module from importlib import import_module
from django.apps import apps
from django.core.exceptions import ImproperlyConfigured
def get_callable(func_or_path): def get_callable(func_or_path):
""" """
...@@ -13,3 +16,22 @@ def get_callable(func_or_path): ...@@ -13,3 +16,22 @@ def get_callable(func_or_path):
_module = import_module(module_name) _module = import_module(module_name)
func = getattr(_module, function_name) func = getattr(_module, function_name)
return func 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)
)
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