Skip to content

Commit 70e6777

Browse files
authored
Merge pull request #10 from yamijuan/juan-development
Added support for AppConfig notation in INSTALLED_APPS settings
2 parents c329e07 + 6630e78 commit 70e6777

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

menu_generator/templatetags/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.conf import settings
2-
from ..utils import get_callable
2+
from ..utils import get_callable, clean_app_config
33

44
MENU_DICT = ".menus.MENUS"
55

@@ -14,8 +14,9 @@ def get_menu_from_apps(menu_name):
1414
installed_apps = getattr(settings, "INSTALLED_APPS", [])
1515
menu_list = []
1616
for app in installed_apps:
17+
cleaned_app = clean_app_config(app)
1718
try:
18-
all_menus_dict = get_callable(app + MENU_DICT)
19+
all_menus_dict = get_callable(cleaned_app + MENU_DICT)
1920
except ImportError:
2021
all_menus_dict = None
2122
except AttributeError:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_app_config = 'app1.apps.MyAppConfig'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class MyAppConfig(AppConfig):
5+
name = 'menu_generator.tests.test_apps.app1'
6+
verbose_name = 'app 1'

menu_generator/tests/testsettings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
SECRET_KEY = "r4dy"
88
INSTALLED_APPS = [
99
'menu_generator',
10-
'menu_generator.tests.test_apps.app1',
10+
'menu_generator.tests.test_apps.app1.apps.MyAppConfig',
1111
'menu_generator.tests.test_apps.app2',
1212
'menu_generator.tests.test_apps.app3'
1313
]

menu_generator/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from importlib import import_module
22

3+
from django.apps import apps
4+
from django.core.exceptions import ImproperlyConfigured
5+
36

47
def get_callable(func_or_path):
58
"""
@@ -13,3 +16,22 @@ def get_callable(func_or_path):
1316
_module = import_module(module_name)
1417
func = getattr(_module, function_name)
1518
return func
19+
20+
21+
def clean_app_config(app_path):
22+
"""
23+
Removes the AppConfig path for this app and returns the new string
24+
"""
25+
apps_names = [app.name for app in apps.get_app_configs()]
26+
if app_path in apps_names:
27+
return app_path
28+
else:
29+
app_split = app_path.split('.')
30+
new_app = '.'.join(app_split[:-2])
31+
if new_app in apps_names:
32+
return new_app
33+
else: # pragma: no cover
34+
raise ImproperlyConfigured(
35+
"The application {0} is not in the configured apps or does" +
36+
"not have the pattern app.apps.AppConfig".format(app_path)
37+
)

0 commit comments

Comments
 (0)