django-orchestra/orchestra/contrib/saas/models.py

87 lines
3 KiB
Python
Raw Normal View History

2014-09-26 19:21:09 +00:00
from django.db import models
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
2015-05-04 19:52:53 +00:00
from orchestra.core import validators
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
2015-04-23 14:34:04 +00:00
class SaaSQuerySet(models.QuerySet):
def create(self, **kwargs):
""" Sets password if provided, all within a single DB operation """
password = kwargs.pop('password')
saas = SaaS(**kwargs)
if password:
saas.set_password(password)
saas.save()
return saas
2014-09-26 19:21:09 +00:00
class SaaS(models.Model):
service = models.CharField(_("service"), max_length=32,
2015-04-05 10:46:24 +00:00
choices=SoftwareService.get_choices())
name = models.CharField(_("Name"), max_length=64,
help_text=_("Required. 64 characters or fewer. Letters, digits and ./- only."),
validators=[validators.validate_hostname])
2014-11-09 10:16:07 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
2015-04-05 10:46:24 +00:00
related_name='saas')
2015-03-25 17:04:44 +00:00
is_active = models.BooleanField(_("active"), default=True,
2015-04-05 10:46:24 +00:00
help_text=_("Designates whether this service should be treated as active. "))
2015-03-23 15:36:51 +00:00
data = JSONField(_("data"), default={},
2015-04-05 10:46:24 +00:00
help_text=_("Extra information dependent of each service."))
custom_url = models.URLField(_("custom URL"), blank=True,
help_text=_("Optional and alternative URL for accessing this service instance. "
"i.e. <tt>https://wiki.mydomain/doku/</tt><br>"
"A related website will be automatically configured if needed."))
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')
2015-04-23 14:34:04 +00:00
objects = SaaSQuerySet.as_manager()
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 = (
('name', 'service'),
2014-11-20 15:34:59 +00:00
)
2014-09-26 21:24:23 +00:00
2015-04-02 16:14:55 +00:00
def __str__(self):
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(self.service)
2014-09-26 21:24:23 +00:00
2015-03-04 21:06:16 +00:00
@cached_property
def service_instance(self):
""" Per request lived service_instance """
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
2015-05-09 17:08:45 +00:00
def disable(self):
self.is_active = False
self.save(update_fields=('is_active',))
2016-03-31 16:18:38 +00:00
def enable(self):
self.is_active = True
2016-03-31 16:18:38 +00:00
self.save(update_fields=('is_active',))
2014-11-09 10:16:07 +00:00
def clean(self):
2015-04-07 15:14:49 +00:00
if not self.pk:
self.name = self.name.lower()
self.service_instance.clean()
self.data = self.service_instance.clean_data()
2014-11-20 15:34:59 +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