mirror of
https://github.com/django-components/django-components.git
synced 2025-08-03 22:08:17 +00:00
Autodiscover components in components.py inside an app (#24)
Co-authored-by: @danjac
This commit is contained in:
parent
ce7906b6f4
commit
4df314b7f6
3 changed files with 44 additions and 0 deletions
|
@ -0,0 +1,15 @@
|
|||
from importlib import import_module
|
||||
|
||||
from django.utils.module_loading import autodiscover_modules
|
||||
|
||||
default_app_config = "django_components.apps.ComponentsConfig"
|
||||
|
||||
|
||||
def autodiscover():
|
||||
# look for "components" module/pkg in each app
|
||||
from . import app_settings
|
||||
|
||||
if app_settings.AUTODISCOVER:
|
||||
autodiscover_modules("components")
|
||||
for path in app_settings.LIBRARIES:
|
||||
import_module(path)
|
21
django_components/app_settings.py
Normal file
21
django_components/app_settings.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class AppSettings:
|
||||
def __init__(self):
|
||||
self.settings = getattr(settings, "COMPONENTS", {})
|
||||
|
||||
@property
|
||||
def AUTODISCOVER(self):
|
||||
return self.settings.setdefault("autodiscover", True)
|
||||
|
||||
@property
|
||||
def LIBRARIES(self):
|
||||
return self.settings.setdefault("libraries", [])
|
||||
|
||||
|
||||
app_settings = AppSettings()
|
||||
app_settings.__name__ = __name__
|
||||
sys.modules[__name__] = app_settings
|
8
django_components/apps.py
Normal file
8
django_components/apps.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ComponentsConfig(AppConfig):
|
||||
name = "django_components"
|
||||
|
||||
def ready(self):
|
||||
self.module.autodiscover()
|
Loading…
Add table
Add a link
Reference in a new issue