django-orchestra-test/orchestra/apps/webapps/models.py

130 lines
3.8 KiB
Python
Raw Normal View History

2015-03-10 21:51:10 +00:00
import os
2014-05-08 16:59:35 +00:00
import re
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import pre_save, pre_delete
from django.dispatch import receiver
2015-03-04 21:06:16 +00:00
from django.utils.functional import cached_property
2014-05-08 16:59:35 +00:00
from django.utils.translation import ugettext_lazy as _
2015-03-04 21:06:16 +00:00
from jsonfield import JSONField
2014-05-08 16:59:35 +00:00
from orchestra.core import validators, services
from orchestra.utils.functional import cached
from . import settings
from .options import AppOption
2015-03-04 21:06:16 +00:00
from .types import AppType
2014-05-08 16:59:35 +00:00
class WebApp(models.Model):
""" Represents a web application """
2015-03-02 10:37:25 +00:00
name = models.CharField(_("name"), max_length=128, validators=[validators.validate_name])
2014-05-08 16:59:35 +00:00
type = models.CharField(_("type"), max_length=32,
2015-03-04 21:06:16 +00:00
choices=AppType.get_plugin_choices())
2014-05-08 16:59:35 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
related_name='webapps')
2015-03-18 21:51:12 +00:00
data = JSONField(_("data"), blank=True,
help_text=_("Extra information dependent of each service."))
2014-05-08 16:59:35 +00:00
class Meta:
unique_together = ('name', 'account')
verbose_name = _("Web App")
verbose_name_plural = _("Web Apps")
def __unicode__(self):
2015-03-02 10:37:25 +00:00
return self.name
2014-05-08 16:59:35 +00:00
2014-11-27 19:17:26 +00:00
def get_description(self):
return self.get_type_display()
2015-03-04 21:06:16 +00:00
@cached_property
def type_class(self):
return AppType.get_plugin(self.type)
@cached_property
def type_instance(self):
""" Per request lived type_instance """
return self.type_class(self)
2015-03-04 21:06:16 +00:00
2014-11-27 19:17:26 +00:00
def clean(self):
2015-03-04 21:06:16 +00:00
apptype = self.type_instance
apptype.validate()
self.data = apptype.clean_data()
2014-11-27 19:17:26 +00:00
2014-05-08 16:59:35 +00:00
@cached
def get_options(self):
2015-03-04 21:06:16 +00:00
return {
opt.name: opt.value for opt in self.options.all()
}
2014-05-08 16:59:35 +00:00
2014-10-24 10:16:46 +00:00
def get_directive(self):
return self.type_instance.get_directive()
2014-05-08 16:59:35 +00:00
def get_path(self):
context = {
'home': self.get_user().get_home(),
2015-03-02 10:37:25 +00:00
'app_name': self.name,
2014-05-08 16:59:35 +00:00
}
2015-03-02 10:37:25 +00:00
path = settings.WEBAPPS_BASE_ROOT % context
public_root = self.options.filter(name='public-root').first()
if public_root:
path = os.path.join(path, public_root.value)
2015-03-10 21:51:10 +00:00
return os.path.normpath(path.replace('//', '/'))
2014-10-23 15:38:46 +00:00
2014-10-24 10:16:46 +00:00
def get_user(self):
return self.account.main_systemuser
2014-10-23 15:38:46 +00:00
def get_username(self):
2014-10-24 10:16:46 +00:00
return self.get_user().username
2014-10-23 15:38:46 +00:00
def get_groupname(self):
return self.get_username()
2014-05-08 16:59:35 +00:00
class WebAppOption(models.Model):
webapp = models.ForeignKey(WebApp, verbose_name=_("Web application"),
related_name='options')
name = models.CharField(_("name"), max_length=128, choices=AppType.get_options_choices())
2014-05-08 16:59:35 +00:00
value = models.CharField(_("value"), max_length=256)
class Meta:
unique_together = ('webapp', 'name')
verbose_name = _("option")
verbose_name_plural = _("options")
def __unicode__(self):
return self.name
@cached_property
def option_class(self):
return AppOption.get_plugin(self.name)
@cached_property
def option_instance(self):
""" Per request lived option instance """
return self.option_class(self)
2014-05-08 16:59:35 +00:00
def clean(self):
self.option_instance.validate()
2014-05-08 16:59:35 +00:00
services.register(WebApp)
2015-03-04 21:06:16 +00:00
# Admin bulk deletion doesn't call model.delete()
# So, signals are used instead of model method overriding
2015-03-04 21:06:16 +00:00
@receiver(pre_save, sender=WebApp, dispatch_uid='webapps.type.save')
def type_save(sender, *args, **kwargs):
instance = kwargs['instance']
instance.type_instance.save()
2015-03-04 21:06:16 +00:00
@receiver(pre_delete, sender=WebApp, dispatch_uid='webapps.type.delete')
def type_delete(sender, *args, **kwargs):
instance = kwargs['instance']
2015-03-18 21:51:12 +00:00
try:
instance.type_instance.delete()
except KeyError:
pass