Commit c792331e8ce28151b0788f8ef74c35841af39e7b

Authored by Lucas Kanashiro
Committed by Sergio Oliveira
1 parent a1c9a273

Create example of plugin using signals structure

Using the gitlab plugin structure to test the signal structure created
by using a GitlabProject method to send a dummy signal.

Signed-off-by: Lucas Moura <lucas.moura128@gmail.com>
Signed-off-by: Gustavo Jaruga <darksshades@gmail>
colab/plugins/gitlab/apps.py
... ... @@ -2,11 +2,16 @@
2 2 from django.utils.translation import ugettext_lazy as _
3 3  
4 4 from ..utils.apps import ColabProxiedAppConfig
  5 +from colab.signals.tasks import register_signal, connect_signal
  6 +from colab.plugins.gitlab.tasks import handling_method
5 7  
6 8  
7 9 class ProxyGitlabAppConfig(ColabProxiedAppConfig):
8 10 name = 'colab.plugins.gitlab'
9 11 verbose_name = 'Gitlab Proxy'
  12 + short_name = 'gitlab'
  13 +
  14 + signals_list = ['gitlab_create_project']
10 15  
11 16 menu = {
12 17 'title': _('Code'),
... ... @@ -23,3 +28,9 @@ class ProxyGitlabAppConfig(ColabProxiedAppConfig):
23 28  
24 29 ),
25 30 }
  31 +
  32 +
  33 + def __init__(self, app_name, app_module):
  34 + super(ProxyGitlabAppConfig, self).__init__(app_name, app_module)
  35 + register_signal(self.short_name, self.signals_list)
  36 + connect_signal(self.signals_list[0], self.short_name, handling_method)
... ...
colab/plugins/gitlab/celery.py 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +from __future__ import absolute_import
  2 +
  3 +import os
  4 +
  5 +from celery import Celery
  6 +
  7 +# set the default Django settings module for the 'celery' program.
  8 +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'colab.settings')
  9 +
  10 +from django.conf import settings
  11 +
  12 +app = Celery('colab')
  13 +
  14 +app.config_from_object('django.conf:settings')
  15 +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
  16 +
  17 +app.conf.update(
  18 + CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
  19 +)
  20 +app.conf.update(
  21 + CELERY_RESULT_BACKEND='djcelery.backends.cache:CacheBackend',
  22 +)
... ...
colab/plugins/gitlab/models.py
... ... @@ -2,6 +2,7 @@ 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
5 6  
6 7  
7 8 class GitlabProject(models.Model, HitCounterModelMixin):
... ... @@ -17,6 +18,7 @@ class GitlabProject(models.Model, HitCounterModelMixin):
17 18  
18 19 @property
19 20 def url(self):
  21 + send('gitlab_create_project', 'colab.plugins.gitlab')
20 22 return u'/gitlab/{}'.format(self.path_with_namespace)
21 23  
22 24 class Meta:
... ...
colab/plugins/gitlab/tasks.py 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +from colab.plugins.gitlab.celery import app
  2 +
  3 +
  4 +@app.task(bind=True)
  5 +def handling_method(self, **kwargs):
  6 + f = open('/vagrant/test_plugin', 'wb')
  7 + f.write(str(kwargs))
  8 + f.close()
  9 + return 5
... ...
colab/signals/tasks.py
... ... @@ -33,6 +33,6 @@ def connect_signal(signal_name, sender, handling_method):
33 33  
34 34 def send(signal_name, sender, **kwargs):
35 35 if signal_name in signal_instances:
36   - signal_instances[signal_name].send(sender=sender)
  36 + signal_instances[signal_name].send(sender=sender, **kwargs)
37 37 else:
38 38 raise Exception("Signal does not exist!")
... ...