File tree 5 files changed +33
-3
lines changed
5 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 1
1
from django .conf import settings
2
- from ..utils import get_callable
2
+ from ..utils import get_callable , clean_app_config
3
3
4
4
MENU_DICT = ".menus.MENUS"
5
5
@@ -14,8 +14,9 @@ def get_menu_from_apps(menu_name):
14
14
installed_apps = getattr (settings , "INSTALLED_APPS" , [])
15
15
menu_list = []
16
16
for app in installed_apps :
17
+ cleaned_app = clean_app_config (app )
17
18
try :
18
- all_menus_dict = get_callable (app + MENU_DICT )
19
+ all_menus_dict = get_callable (cleaned_app + MENU_DICT )
19
20
except ImportError :
20
21
all_menus_dict = None
21
22
except AttributeError :
Original file line number Diff line number Diff line change
1
+ default_app_config = 'app1.apps.MyAppConfig'
Original file line number Diff line number Diff line change
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'
Original file line number Diff line number Diff line change 7
7
SECRET_KEY = "r4dy"
8
8
INSTALLED_APPS = [
9
9
'menu_generator' ,
10
- 'menu_generator.tests.test_apps.app1' ,
10
+ 'menu_generator.tests.test_apps.app1.apps.MyAppConfig ' ,
11
11
'menu_generator.tests.test_apps.app2' ,
12
12
'menu_generator.tests.test_apps.app3'
13
13
]
Original file line number Diff line number Diff line change 1
1
from importlib import import_module
2
2
3
+ from django .apps import apps
4
+ from django .core .exceptions import ImproperlyConfigured
5
+
3
6
4
7
def get_callable (func_or_path ):
5
8
"""
@@ -13,3 +16,22 @@ def get_callable(func_or_path):
13
16
_module = import_module (module_name )
14
17
func = getattr (_module , function_name )
15
18
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
+ )
You can’t perform that action at this time.
0 commit comments