Skip to content

Commit 246804c

Browse files
committed
Merge pull request #6 from virogenesis/master
Signal for state switch
2 parents 337cead + 5c6d391 commit 246804c

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

djangoplugins/models.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import absolute_import
22

3+
from dirtyfields import DirtyFieldsMixin
34
from django.db import models
45
from django.utils.translation import ugettext_lazy as _
56
from django.utils.encoding import python_2_unicode_compatible
6-
7+
from djangoplugins.signals import django_plugin_enabled, django_plugin_disabled
78
from .utils import get_plugin_name, get_plugin_from_string
89

910
ENABLED = 0
@@ -16,6 +17,9 @@
1617
(REMOVED, _('Removed')),
1718
)
1819

20+
STATUS_CHOICES_ENABLED = (ENABLED,)
21+
STATUS_CHOICES_DISABLED = (DISABLED, REMOVED,)
22+
1923

2024
class PluginPointManager(models.Manager):
2125
def get_point(self, point):
@@ -47,7 +51,7 @@ def get_by_natural_key(self, name):
4751

4852

4953
@python_2_unicode_compatible
50-
class Plugin(models.Model):
54+
class Plugin(DirtyFieldsMixin, models.Model):
5155
"""
5256
Database representation of a plugin.
5357
@@ -101,3 +105,14 @@ def is_active(self):
101105
def get_plugin(self):
102106
plugin_class = get_plugin_from_string(self.pythonpath)
103107
return plugin_class()
108+
109+
def save(self, *args, **kwargs):
110+
if "status" in self.get_dirty_fields().keys() and self.pk:
111+
if self.status in STATUS_CHOICES_ENABLED:
112+
django_plugin_enabled.send(sender=self.__class__,
113+
plugin=self.get_plugin())
114+
else:
115+
django_plugin_disabled.send(sender=self.__class__,
116+
plugin=self.get_plugin())
117+
118+
return super(Plugin, self).save(*args, **kwargs)

djangoplugins/signals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from django.dispatch import Signal
2+
3+
django_plugin_disabled = Signal(providing_args=["plugin"])
4+
django_plugin_enabled = Signal(providing_args=["plugin"])

docs/index.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,30 @@ ORM::
221221
'plugins': MyPluginPoint.get_plugins_qs().order_by('name')
222222
}
223223

224+
225+
226+
Signals
227+
-------
228+
229+
There are two registered signals with Django that you might use for hooking
230+
event handlers in case a Django plugin gets disabled or enabled at a certain
231+
point. See the following code snippet for an example usage:
232+
233+
.. code-block:: python
234+
235+
from django.dispatch.dispatcher import receiver
236+
from djangoplugins.signals import django_plugin_disabled, django_plugin_enabled
237+
238+
@receiver(django_plugin_enabled)
239+
def _django_plugin_enabled(sender, plugin, **kwargs):
240+
enable_plugin(plugin)
241+
242+
@receiver(django_plugin_disabled)
243+
def _django_plugin_disabled(sender, plugin, **kwargs):
244+
disable_plugin(plugin)
245+
246+
247+
224248
Model fields
225249
------------
226250

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def read_docs(filename):
3030
packages=find_packages(exclude=['sample-project']),
3131
install_requires=[
3232
'django>=1.6',
33+
'django-dirtyfields',
3334
],
3435
url='https://github.com/krischer/django-plugins',
3536
download_url='http://pypi.python.org/pypi/django-plugins',

0 commit comments

Comments
 (0)