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
|
|
|
|
|
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
|
|
|
|
|
|
|
from .services import SoftwareService
|
|
|
|
|
|
|
|
|
|
|
|
class SaaS(models.Model):
|
|
|
|
service = models.CharField(_("service"), max_length=32,
|
|
|
|
choices=SoftwareService.get_plugin_choices())
|
2014-11-20 15:34:59 +00:00
|
|
|
username = models.CharField(_("username"), max_length=64,
|
|
|
|
help_text=_("Required. 64 characters or fewer. Letters, digits and ./-/_ only."),
|
|
|
|
validators=[validators.validate_username])
|
|
|
|
site_name = NullableCharField(_("site name"), max_length=32, null=True)
|
2014-11-09 10:16:07 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
|
|
|
|
related_name='saas')
|
2014-11-20 15:34:59 +00:00
|
|
|
data = JSONField(_("data"), help_text=_("Extra information dependent of each service."))
|
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 = (
|
|
|
|
('username', 'service'),
|
|
|
|
('site_name', 'service'),
|
|
|
|
)
|
2014-09-26 21:24:23 +00:00
|
|
|
|
2014-09-28 12:28:57 +00:00
|
|
|
def __unicode__(self):
|
2014-11-20 15:34:59 +00:00
|
|
|
return "%s@%s" % (self.username, 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
|
|
|
|
2014-11-20 15:34:59 +00:00
|
|
|
def get_site_name(self):
|
2015-03-11 16:32:33 +00:00
|
|
|
return self.service_instance.get_site_name()
|
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
|
|
|
|
|
|
|
def set_password(self, password):
|
|
|
|
self.password = password
|
2014-09-26 19:21:09 +00:00
|
|
|
|
|
|
|
services.register(SaaS)
|