Plugin registry

This commit is contained in:
Raphael Michel
2014-10-07 12:21:13 +02:00
parent 1ec224049d
commit 3bae6a6819
15 changed files with 240 additions and 33 deletions

View File

@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('tixlbase', '0015_auto_20141006_2205'),
]
operations = [
migrations.AddField(
model_name='event',
name='plugins',
field=models.TextField(blank=True, verbose_name='Plugins', null=True),
preserve_default=True,
),
]

View File

@@ -276,6 +276,10 @@ class Event(models.Model):
verbose_name=_("Last date of payments"),
help_text=_("The last date any payments are accepted. This has precedence over the number of days configured above.")
)
plugins = models.TextField(
null=True, blank=True,
verbose_name=_("Plugins"),
)
class Meta:
verbose_name = _("Event")
@@ -291,6 +295,11 @@ class Event(models.Model):
self.get_cache().clear()
return obj
def get_plugins(self):
if self.plugins is None:
return []
return self.plugins.split(",")
def get_date_from_display(self):
return _date(
self.date_from,

17
src/tixlbase/plugins.py Normal file
View File

@@ -0,0 +1,17 @@
from enum import Enum
from django.apps import apps
class PluginType(Enum):
RESTRICTION = 1
def get_all_plugins():
plugins = []
for app in apps.get_app_configs():
if hasattr(app, 'TixlPluginMeta'):
meta = app.TixlPluginMeta
meta.module = app.name
plugins.append(meta)
return plugins

View File

@@ -1,5 +1,39 @@
import django.dispatch
from django.apps import apps
from django.dispatch.dispatcher import NO_RECEIVERS
determine_availability = django.dispatch.Signal(
class EventPluginSignal(django.dispatch.Signal):
def send(self, sender, **named):
"""
Send signal from sender to all connected receivers that belong to
plugins enabled for the given Event.
sender is required to be an instance of ``tixlbase.models.Event``.
"""
responses = []
if not self.receivers or self.sender_receivers_cache.get(sender) is NO_RECEIVERS:
return responses
for receiver in self._live_receivers(sender):
# Find the Django application this belongs to
searchpath = receiver.__module__
app = None
while "." in searchpath:
try:
if apps.is_installed(searchpath):
app = apps.get_app_config(searchpath.split(".")[-1])
except LookupError:
pass
searchpath, mod = searchpath.rsplit(".", 1)
# Only fire receivers from active plugins
if app.name in sender.get_plugins():
response = receiver(signal=self, sender=sender, **named)
responses.append((receiver, response))
return responses
determine_availability = EventPluginSignal(
providing_args=["item", "variations", "context", "cache"]
)