Skip to content

Commit

Permalink
Merge pull request #10 from yamijuan/juan-development
Browse files Browse the repository at this point in the history
Added support for AppConfig notation in INSTALLED_APPS settings
  • Loading branch information
MiltonLn committed Feb 19, 2018
2 parents c329e07 + 6630e78 commit 70e6777
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
5 changes: 3 additions & 2 deletions menu_generator/templatetags/utils.py
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions menu_generator/tests/test_apps/app1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'app1.apps.MyAppConfig'
6 changes: 6 additions & 0 deletions menu_generator/tests/test_apps/app1/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class MyAppConfig(AppConfig):
name = 'menu_generator.tests.test_apps.app1'
verbose_name = 'app 1'
2 changes: 1 addition & 1 deletion menu_generator/tests/testsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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'
]
Expand Down
22 changes: 22 additions & 0 deletions menu_generator/utils.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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)
)

0 comments on commit 70e6777

Please sign in to comment.