Commit c811c353337d84cb0ecfcd517d2f69439f8dd8b5
Committed by
Sergio Oliveira
1 parent
d1286481
Exists in
master
and in
30 other branches
Create abstract signal class for plugins
In order to create an abstract class for signal handling, it was necessary to remove the signals methods from the apps of the plugins, and put the implementation from the signal on ColabProxiedAppConfig. It was also created a abstract class on plugins utils that provide abstract methods to register and connect signals. Signed-off-by: Lucas Kanashiro <kanashiro.duarte@gmail.com>
Showing
7 changed files
with
88 additions
and
47 deletions
Show diff stats
colab/plugins/gitlab/apps.py
1 | 1 | |
2 | 2 | from ..utils.apps import ColabProxiedAppConfig |
3 | -from colab.signals.tasks import register_signal, connect_signal | |
4 | -from colab.plugins.gitlab.tasks import handling_method | |
5 | 3 | |
6 | 4 | |
7 | 5 | class ProxyGitlabAppConfig(ColabProxiedAppConfig): |
... | ... | @@ -10,8 +8,3 @@ class ProxyGitlabAppConfig(ColabProxiedAppConfig): |
10 | 8 | short_name = 'gitlab' |
11 | 9 | |
12 | 10 | signals_list = ['gitlab_create_project'] |
13 | - | |
14 | - def __init__(self, app_name, app_module): | |
15 | - super(ProxyGitlabAppConfig, self).__init__(app_name, app_module) | |
16 | - register_signal(self.short_name, self.signals_list) | |
17 | - connect_signal(self.signals_list[0], self.short_name, handling_method) | ... | ... |
colab/plugins/gitlab/models.py
... | ... | @@ -2,7 +2,6 @@ from django.db import models |
2 | 2 | from django.utils.translation import ugettext_lazy as _ |
3 | 3 | from colab.plugins.utils.models import Collaboration |
4 | 4 | from hitcounter.models import HitCounterModelMixin |
5 | -from colab.signals.tasks import send | |
6 | 5 | |
7 | 6 | |
8 | 7 | class GitlabProject(models.Model, HitCounterModelMixin): |
... | ... | @@ -18,7 +17,6 @@ class GitlabProject(models.Model, HitCounterModelMixin): |
18 | 17 | |
19 | 18 | @property |
20 | 19 | def url(self): |
21 | - send('gitlab_create_project', 'colab.plugins.gitlab') | |
22 | 20 | return u'/gitlab/{}'.format(self.path_with_namespace) |
23 | 21 | |
24 | 22 | class Meta: | ... | ... |
... | ... | @@ -0,0 +1,16 @@ |
1 | +from colab.plugins.utils.signals import AbstractSignal | |
2 | +from colab.plugins.gitlab.tasks import handling_method | |
3 | +from colab.signals.signals import register_signal, connect_signal | |
4 | + | |
5 | +class GitlabSignals(AbstractSignal): | |
6 | + | |
7 | + short_name = 'gitlab' | |
8 | + signals_list = ['gitlab_create_project'] | |
9 | + | |
10 | + | |
11 | + def register_signal(self): | |
12 | + register_signal(self.short_name, self.signals_list) | |
13 | + | |
14 | + | |
15 | + def connect_signal(self): | |
16 | + connect_signal(self.signals_list[0], self.short_name, handling_method) | ... | ... |
colab/plugins/utils/apps.py
1 | +import importlib | |
2 | +import inspect | |
1 | 3 | |
2 | 4 | from django.apps import AppConfig |
5 | +from colab.plugins.utils.signals import AbstractSignal | |
3 | 6 | |
4 | 7 | |
5 | 8 | class ColabProxiedAppConfig(AppConfig): |
6 | 9 | colab_proxied_app = True |
10 | + | |
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() | |
15 | + | |
16 | + | |
17 | + def _import_signals(self, app_name): | |
18 | + self.module_path = app_name + '.signals' | |
19 | + self.module = importlib.import_module(self.module_path) | |
20 | + | |
21 | + for module_item_name in dir(self.module): | |
22 | + module_item = getattr(self.module, module_item_name) | |
23 | + if not inspect.isclass(module_item): | |
24 | + continue | |
25 | + if issubclass(module_item, AbstractSignal): | |
26 | + if module_item != AbstractSignal: | |
27 | + self.signals = module_item() | |
28 | + break | |
29 | + | |
30 | + | |
31 | + def ready(self): | |
32 | + self.signals.connect_signal() | ... | ... |
... | ... | @@ -0,0 +1,38 @@ |
1 | +from django.dispatch import Signal | |
2 | +from colab.signals.celery import app | |
3 | + | |
4 | + | |
5 | +registered_signals = {} | |
6 | +signal_instances = {} | |
7 | + | |
8 | + | |
9 | +# Fix celery serialization for signal | |
10 | +def reducer(self): | |
11 | + return (Signal, (self.providing_args,)) | |
12 | +Signal.__reduce__ = reducer | |
13 | + | |
14 | + | |
15 | +def register_signal(plugin_name, list_signals): | |
16 | + for signal in list_signals: | |
17 | + if signal in registered_signals: | |
18 | + if not plugin_name in registered_signals[signal]: | |
19 | + registered_signals[signal].append(plugin_name) | |
20 | + else: | |
21 | + registered_signals[signal] = [] | |
22 | + registered_signals[signal].append(plugin_name) | |
23 | + signal_instances[signal] = Signal() | |
24 | + | |
25 | + | |
26 | +def connect_signal(signal_name, sender, handling_method): | |
27 | + if signal_name in signal_instances: | |
28 | + signal_instances[signal_name].connect(handling_method.delay, | |
29 | + sender=sender) | |
30 | + else: | |
31 | + raise Exception("Signal does not exist!") | |
32 | + | |
33 | + | |
34 | +def send(signal_name, sender, **kwargs): | |
35 | + if signal_name in signal_instances: | |
36 | + signal_instances[signal_name].send(sender=sender, **kwargs) | |
37 | + else: | |
38 | + raise Exception("Signal does not exist!") | ... | ... |
colab/signals/tasks.py
... | ... | @@ -1,38 +0,0 @@ |
1 | -from django.dispatch import Signal | |
2 | -from colab.signals.celery import app | |
3 | - | |
4 | - | |
5 | -registered_signals = {} | |
6 | -signal_instances = {} | |
7 | - | |
8 | - | |
9 | -# Fix celery serialization for signal | |
10 | -def reducer(self): | |
11 | - return (Signal, (self.providing_args,)) | |
12 | -Signal.__reduce__ = reducer | |
13 | - | |
14 | - | |
15 | -def register_signal(plugin_name, list_signals): | |
16 | - for signal in list_signals: | |
17 | - if signal in registered_signals: | |
18 | - if not plugin_name in registered_signals[signal]: | |
19 | - registered_signals[signal].append(plugin_name) | |
20 | - else: | |
21 | - registered_signals[signal] = [] | |
22 | - registered_signals[signal].append(plugin_name) | |
23 | - signal_instances[signal] = Signal() | |
24 | - | |
25 | - | |
26 | -def connect_signal(signal_name, sender, handling_method): | |
27 | - if signal_name in signal_instances: | |
28 | - signal_instances[signal_name].connect(handling_method.delay, | |
29 | - sender=sender) | |
30 | - else: | |
31 | - raise Exception("Signal does not exist!") | |
32 | - | |
33 | - | |
34 | -def send(signal_name, sender, **kwargs): | |
35 | - if signal_name in signal_instances: | |
36 | - signal_instances[signal_name].send(sender=sender, **kwargs) | |
37 | - else: | |
38 | - raise Exception("Signal does not exist!") |