From 3c820045a886c71faa74831f976f4fbdd5a02319 Mon Sep 17 00:00:00 2001 From: Lucas Kanashiro Date: Mon, 27 Jul 2015 15:40:20 -0300 Subject: [PATCH] Create example of plugin using signals structure --- colab/plugins/gitlab/apps.py | 10 ++++++++++ colab/plugins/gitlab/celery.py | 22 ++++++++++++++++++++++ colab/plugins/gitlab/models.py | 2 ++ colab/plugins/gitlab/tasks.py | 9 +++++++++ colab/signals/tasks.py | 2 +- 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 colab/plugins/gitlab/celery.py create mode 100644 colab/plugins/gitlab/tasks.py diff --git a/colab/plugins/gitlab/apps.py b/colab/plugins/gitlab/apps.py index bd08b0a..cdb583c 100644 --- a/colab/plugins/gitlab/apps.py +++ b/colab/plugins/gitlab/apps.py @@ -1,7 +1,17 @@ from ..utils.apps import ColabProxiedAppConfig +from colab.signals.tasks import register_signal, connect_signal +from colab.plugins.gitlab.tasks import handling_method class ProxyGitlabAppConfig(ColabProxiedAppConfig): name = 'colab.plugins.gitlab' verbose_name = 'Gitlab Plugin' + short_name = 'gitlab' + + signals_list = ['gitlab_create_project'] + + def __init__(self, app_name, app_module): + super(ProxyGitlabAppConfig, self).__init__(app_name, app_module) + register_signal(self.short_name, self.signals_list) + connect_signal(self.signals_list[0], self.short_name, handling_method) diff --git a/colab/plugins/gitlab/celery.py b/colab/plugins/gitlab/celery.py new file mode 100644 index 0000000..6d19e21 --- /dev/null +++ b/colab/plugins/gitlab/celery.py @@ -0,0 +1,22 @@ +from __future__ import absolute_import + +import os + +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'colab.settings') + +from django.conf import settings + +app = Celery('colab') + +app.config_from_object('django.conf:settings') +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) + +app.conf.update( + CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend', +) +app.conf.update( + CELERY_RESULT_BACKEND='djcelery.backends.cache:CacheBackend', +) diff --git a/colab/plugins/gitlab/models.py b/colab/plugins/gitlab/models.py index cbc9b2e..5d8cc53 100644 --- a/colab/plugins/gitlab/models.py +++ b/colab/plugins/gitlab/models.py @@ -2,6 +2,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from colab.plugins.utils.models import Collaboration from hitcounter.models import HitCounterModelMixin +from colab.signals.tasks import send class GitlabProject(models.Model, HitCounterModelMixin): @@ -17,6 +18,7 @@ class GitlabProject(models.Model, HitCounterModelMixin): @property def url(self): + send('gitlab_create_project', 'colab.plugins.gitlab') return u'/gitlab/{}'.format(self.path_with_namespace) class Meta: diff --git a/colab/plugins/gitlab/tasks.py b/colab/plugins/gitlab/tasks.py new file mode 100644 index 0000000..bba1d63 --- /dev/null +++ b/colab/plugins/gitlab/tasks.py @@ -0,0 +1,9 @@ +from colab.plugins.gitlab.celery import app + + +@app.task(bind=True) +def handling_method(self, **kwargs): + f = open('/vagrant/test_plugin', 'wb') + f.write(str(kwargs)) + f.close() + return 5 diff --git a/colab/signals/tasks.py b/colab/signals/tasks.py index 33e3a86..4ad4abb 100644 --- a/colab/signals/tasks.py +++ b/colab/signals/tasks.py @@ -33,6 +33,6 @@ def connect_signal(signal_name, sender, handling_method): def send(signal_name, sender, **kwargs): if signal_name in signal_instances: - signal_instances[signal_name].send(sender=sender) + signal_instances[signal_name].send(sender=sender, **kwargs) else: raise Exception("Signal does not exist!") -- libgit2 0.21.2