django-orchestra/orchestra/apps/saas/services/options.py

55 lines
1.7 KiB
Python
Raw Normal View History

2014-09-26 19:21:09 +00:00
from django import forms
2014-11-09 10:16:07 +00:00
from django.core.exceptions import ValidationError
2014-09-26 19:21:09 +00:00
from django.utils.translation import ugettext_lazy as _
2014-11-12 16:33:40 +00:00
from orchestra.apps import plugins
2014-09-26 19:21:09 +00:00
from orchestra.utils.functional import cached
from orchestra.utils.python import import_class
from .. import settings
2014-11-09 10:16:07 +00:00
# TODO if unique_description: make description_field create only
2014-09-26 19:21:09 +00:00
class SoftwareService(plugins.Plugin):
2014-09-26 21:24:23 +00:00
description_field = ''
2014-11-09 10:16:07 +00:00
unique_description = True
2014-09-28 12:28:57 +00:00
form = None
2014-09-26 19:21:09 +00:00
serializer = None
2014-10-11 12:43:08 +00:00
icon = 'orchestra/icons/apps.png'
class_verbose_name = _("Software as a Service")
2014-09-26 19:21:09 +00:00
@classmethod
@cached
def get_plugins(cls):
plugins = []
for cls in settings.SAAS_ENABLED_SERVICES:
plugins.append(import_class(cls))
return plugins
2014-11-09 10:16:07 +00:00
@classmethod
def clean_data(cls, saas):
""" model clean, uses cls.serizlier by default """
if cls.unique_description and not saas.pk:
from ..models import SaaS
field = cls.description_field
if SaaS.objects.filter(data__contains='"%s":"%s"' % (field, saas.data[field])).exists():
raise ValidationError({
field: _("SaaS service with this %(field)s already exists.")
}, params={'field': field})
serializer = cls.serializer(data=saas.data)
if not serializer.is_valid():
raise ValidationError(serializer.errors)
return serializer.data
2014-09-26 19:21:09 +00:00
def get_form(self):
self.form.plugin = self
2014-09-26 21:24:23 +00:00
self.form.plugin_field = 'service'
2014-09-26 19:21:09 +00:00
return self.form
def get_serializer(self):
self.serializer.plugin = self
return self.serializer
2014-09-26 21:24:23 +00:00
def get_description(self, data):
return data[self.description_field]