Commit 3c820045a886c71faa74831f976f4fbdd5a02319

Authored by Lucas Kanashiro
Committed by Sergio Oliveira
1 parent 4423a4f6

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
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
3 5  
4 6  
5 7 class ProxyGitlabAppConfig(ColabProxiedAppConfig):
6 8 name = 'colab.plugins.gitlab'
7 9 verbose_name = 'Gitlab Plugin'
  10 + short_name = 'gitlab'
  11 +
  12 + 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/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!")
... ...