2014-09-26 19:21:09 +00:00
|
|
|
from django.db import models
|
2015-03-23 15:36:51 +00:00
|
|
|
from django.db.models.signals import pre_save, pre_delete
|
|
|
|
from django.dispatch import receiver
|
2014-09-26 21:24:23 +00:00
|
|
|
from django.utils.functional import cached_property
|
2014-09-26 19:21:09 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from jsonfield import JSONField
|
|
|
|
|
2014-11-20 15:34:59 +00:00
|
|
|
from orchestra.core import services, validators
|
|
|
|
from orchestra.models.fields import NullableCharField
|
2014-09-26 19:21:09 +00:00
|
|
|
|
2015-03-27 19:50:54 +00:00
|
|
|
from .fields import VirtualDatabaseRelation
|
2014-09-26 19:21:09 +00:00
|
|
|
from .services import SoftwareService
|
|
|
|
|
|
|
|
|
|
|
|
class SaaS(models.Model):
|
|
|
|
service = models.CharField(_("service"), max_length=32,
|
|
|
|
choices=SoftwareService.get_plugin_choices())
|
2015-03-25 15:45:04 +00:00
|
|
|
name = models.CharField(_("Name"), max_length=64,
|
2014-11-20 15:34:59 +00:00
|
|
|
help_text=_("Required. 64 characters or fewer. Letters, digits and ./-/_ only."),
|
|
|
|
validators=[validators.validate_username])
|
2014-11-09 10:16:07 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
|
|
|
|
related_name='saas')
|
2015-03-25 17:04:44 +00:00
|
|
|
is_active = models.BooleanField(_("active"), default=True,
|
|
|
|
help_text=_("Designates whether this service should be treated as active. "))
|
2015-03-23 15:36:51 +00:00
|
|
|
data = JSONField(_("data"), default={},
|
|
|
|
help_text=_("Extra information dependent of each service."))
|
2015-03-27 19:50:54 +00:00
|
|
|
database = models.ForeignKey('databases.Database', null=True, blank=True)
|
|
|
|
|
|
|
|
# Some SaaS sites may need a database, with this virtual field we tell the ORM to delete them
|
|
|
|
databases = VirtualDatabaseRelation('databases.Database')
|
2014-09-26 19:21:09 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "SaaS"
|
|
|
|
verbose_name_plural = "SaaS"
|
2014-11-20 15:34:59 +00:00
|
|
|
unique_together = (
|
2015-03-25 15:45:04 +00:00
|
|
|
('name', 'service'),
|
2014-11-20 15:34:59 +00:00
|
|
|
)
|
2014-09-26 21:24:23 +00:00
|
|
|
|
2014-09-28 12:28:57 +00:00
|
|
|
def __unicode__(self):
|
2015-03-25 15:45:04 +00:00
|
|
|
return "%s@%s" % (self.name, self.service)
|
2014-09-28 12:28:57 +00:00
|
|
|
|
2014-09-26 21:24:23 +00:00
|
|
|
@cached_property
|
|
|
|
def service_class(self):
|
|
|
|
return SoftwareService.get_plugin(self.service)
|
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
@cached_property
|
|
|
|
def service_instance(self):
|
|
|
|
""" Per request lived service_instance """
|
2015-03-11 16:32:33 +00:00
|
|
|
return self.service_class(self)
|
2015-03-04 21:06:16 +00:00
|
|
|
|
2015-03-25 17:04:44 +00:00
|
|
|
@cached_property
|
|
|
|
def active(self):
|
|
|
|
return self.is_active and self.account.is_active
|
|
|
|
|
2014-11-09 10:16:07 +00:00
|
|
|
def clean(self):
|
2015-03-11 16:32:33 +00:00
|
|
|
self.data = self.service_instance.clean_data()
|
2014-11-20 15:34:59 +00:00
|
|
|
|
2015-03-25 15:45:04 +00:00
|
|
|
def get_site_domain(self):
|
|
|
|
return self.service_instance.get_site_domain()
|
|
|
|
|
2014-11-20 15:34:59 +00:00
|
|
|
def set_password(self, password):
|
|
|
|
self.password = password
|
2014-09-26 19:21:09 +00:00
|
|
|
|
2015-03-23 15:36:51 +00:00
|
|
|
|
2014-09-26 19:21:09 +00:00
|
|
|
services.register(SaaS)
|
2015-03-23 15:36:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Admin bulk deletion doesn't call model.delete()
|
|
|
|
# So, signals are used instead of model method overriding
|
|
|
|
|
|
|
|
@receiver(pre_save, sender=SaaS, dispatch_uid='saas.service.save')
|
|
|
|
def type_save(sender, *args, **kwargs):
|
|
|
|
instance = kwargs['instance']
|
|
|
|
instance.service_instance.save()
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=SaaS, dispatch_uid='saas.service.delete')
|
|
|
|
def type_delete(sender, *args, **kwargs):
|
|
|
|
instance = kwargs['instance']
|
|
|
|
try:
|
|
|
|
instance.service_instance.delete()
|
|
|
|
except KeyError:
|
|
|
|
pass
|