Commit 96c9b14308e3e2d14be856c8bca4b4ba33eb8c10
1 parent
c30d7a00
Exists in
master
and in
30 other branches
Fixed signals implementation
tests were breaking
Showing
5 changed files
with
38 additions
and
30 deletions
Show diff stats
colab/__init__.py
1 | from __future__ import absolute_import | 1 | from __future__ import absolute_import |
2 | 2 | ||
3 | +from . import plugins | ||
4 | + | ||
3 | # This will make sure the app is always imported when | 5 | # This will make sure the app is always imported when |
4 | # Django starts so that shared_task will use this app. | 6 | # Django starts so that shared_task will use this app. |
5 | from .celery import app as celery_app # noqa | 7 | from .celery import app as celery_app # noqa |
8 | + |
colab/plugins/__init__.py
colab/plugins/utils/apps.py
1 | -import importlib | ||
2 | -import inspect | ||
3 | 1 | ||
4 | from django.apps import AppConfig | 2 | from django.apps import AppConfig |
5 | -from colab.plugins.utils.signals import AbstractSignal | ||
6 | 3 | ||
7 | 4 | ||
8 | class ColabProxiedAppConfig(AppConfig): | 5 | class ColabProxiedAppConfig(AppConfig): |
9 | colab_proxied_app = True | 6 | colab_proxied_app = True |
10 | 7 | ||
11 | - def __init__(self, app_name, app_module): | ||
12 | - super(ColabProxiedAppConfig, self).__init__(app_name, app_module) | ||
13 | - self._import_signals(app_name) | ||
14 | - self.signals.register_signal() | 8 | + def register_signals(self): |
9 | + pass | ||
15 | 10 | ||
16 | - def _import_signals(self, app_name): | ||
17 | - self.module_path = app_name + '.signals' | ||
18 | - self.module = importlib.import_module(self.module_path) | ||
19 | - | ||
20 | - for module_item_name in dir(self.module): | ||
21 | - module_item = getattr(self.module, module_item_name) | ||
22 | - if not inspect.isclass(module_item): | ||
23 | - continue | ||
24 | - if issubclass(module_item, AbstractSignal): | ||
25 | - if module_item != AbstractSignal: | ||
26 | - self.signals = module_item() | ||
27 | - break | ||
28 | - | ||
29 | - def ready(self): | ||
30 | - self.signals.connect_signal() | 11 | + def connect_signals(self): |
12 | + pass |
colab/plugins/utils/signals.py
1 | 1 | ||
2 | -from abc import abstractmethod | 2 | +from django.apps import apps |
3 | 3 | ||
4 | 4 | ||
5 | -class AbstractSignal(object): | 5 | +def _init_signals(method_name): |
6 | + for app in apps.get_app_configs(): | ||
7 | + # Try to get the method with `method_name`. | ||
8 | + # If it exists call it using `app` as the first parameter. | ||
9 | + # This is required because methods take `self` as first | ||
10 | + # parameter and as we are calling it as a function python | ||
11 | + # won't send it explicitly. | ||
12 | + # If the method doesn't exist we return a dummy function that | ||
13 | + # won't do anything. | ||
14 | + getattr(app, method_name, lambda x: None)(app) | ||
6 | 15 | ||
7 | - @abstractmethod | ||
8 | - def register_signal(self): | ||
9 | - raise NotImplementedError | ||
10 | 16 | ||
11 | - @abstractmethod | ||
12 | - def connect_signal(self): | ||
13 | - raise NotImplementedError | 17 | +def register_signal(): |
18 | + _init_signals('register_signal') | ||
19 | + | ||
20 | + | ||
21 | +def connect_signal(): | ||
22 | + _init_signals('connect_signal') |