django-orchestra/orchestra/apps/orders/handlers.py

41 lines
1.1 KiB
Python
Raw Normal View History

2014-07-21 12:20:04 +00:00
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from orchestra.utils import plugins
class ServiceHandler(plugins.Plugin):
model = None
__metaclass__ = plugins.PluginMount
def __init__(self, service):
self.service = service
2014-07-21 15:43:36 +00:00
def __getattr__(self, attr):
return getattr(self.service, attr)
2014-07-21 12:20:04 +00:00
@classmethod
def get_plugin_choices(cls):
choices = super(ServiceHandler, cls).get_plugin_choices()
return [('', _("Default"))] + choices
2014-07-21 15:43:36 +00:00
def get_content_type(self):
if not self.model:
return self.content_type
app_label, model = self.model.split('.')
return ContentType.objects.get_by_natural_key(app_label, model.lower())
2014-07-21 12:20:04 +00:00
def matches(self, instance):
safe_locals = {
instance._meta.model_name: instance
}
return eval(self.match, safe_locals)
def get_metric(self, instance):
2014-07-21 15:43:36 +00:00
if self.metric:
safe_locals = {
instance._meta.model_name: instance
}
return eval(self.metric, safe_locals)