Skip to content

Added support for AppConfig notation in INSTALLED_APPS settings #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)