diff --git a/colab/__init__.py b/colab/__init__.py index d13e951..8a4f90d 100644 --- a/colab/__init__.py +++ b/colab/__init__.py @@ -1,5 +1,8 @@ from __future__ import absolute_import +from . import plugins + # This will make sure the app is always imported when # Django starts so that shared_task will use this app. from .celery import app as celery_app # noqa + diff --git a/colab/plugins/__init__.py b/colab/plugins/__init__.py index e69de29..53489b8 100644 --- a/colab/plugins/__init__.py +++ b/colab/plugins/__init__.py @@ -0,0 +1,2 @@ + +default_app_config = 'colab.plugins.apps.PluginAppConfig' diff --git a/colab/plugins/apps.py b/colab/plugins/apps.py new file mode 100644 index 0000000..9d50038 --- /dev/null +++ b/colab/plugins/apps.py @@ -0,0 +1,12 @@ + +from django.apps import AppConfig + +from .utils.signals import connect_signal, register_signal + + +class PluginAppConfig(AppConfig): + name = 'colab.plugins' + + def ready(self): + register_signal() + connect_signal() diff --git a/colab/plugins/utils/apps.py b/colab/plugins/utils/apps.py index fadcfdf..25590ba 100644 --- a/colab/plugins/utils/apps.py +++ b/colab/plugins/utils/apps.py @@ -1,30 +1,12 @@ -import importlib -import inspect from django.apps import AppConfig -from colab.plugins.utils.signals import AbstractSignal class ColabProxiedAppConfig(AppConfig): colab_proxied_app = True - def __init__(self, app_name, app_module): - super(ColabProxiedAppConfig, self).__init__(app_name, app_module) - self._import_signals(app_name) - self.signals.register_signal() + def register_signals(self): + pass - def _import_signals(self, app_name): - self.module_path = app_name + '.signals' - self.module = importlib.import_module(self.module_path) - - for module_item_name in dir(self.module): - module_item = getattr(self.module, module_item_name) - if not inspect.isclass(module_item): - continue - if issubclass(module_item, AbstractSignal): - if module_item != AbstractSignal: - self.signals = module_item() - break - - def ready(self): - self.signals.connect_signal() + def connect_signals(self): + pass diff --git a/colab/plugins/utils/signals.py b/colab/plugins/utils/signals.py index a0a79eb..1c2d7c4 100644 --- a/colab/plugins/utils/signals.py +++ b/colab/plugins/utils/signals.py @@ -1,13 +1,22 @@ -from abc import abstractmethod +from django.apps import apps -class AbstractSignal(object): +def _init_signals(method_name): + for app in apps.get_app_configs(): + # Try to get the method with `method_name`. + # If it exists call it using `app` as the first parameter. + # This is required because methods take `self` as first + # parameter and as we are calling it as a function python + # won't send it explicitly. + # If the method doesn't exist we return a dummy function that + # won't do anything. + getattr(app, method_name, lambda x: None)(app) - @abstractmethod - def register_signal(self): - raise NotImplementedError - @abstractmethod - def connect_signal(self): - raise NotImplementedError +def register_signal(): + _init_signals('register_signal') + + +def connect_signal(): + _init_signals('connect_signal') -- libgit2 0.21.2