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

112 lines
3.5 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
import re
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
from orchestra.core import validators, services
2014-10-24 10:16:46 +00:00
from orchestra.utils import tuple_setting_to_choices, dict_setting_to_choices
2014-05-08 16:59:35 +00:00
from orchestra.utils.functional import cached
from . import settings
class WebApp(models.Model):
""" Represents a web application """
2014-10-27 13:29:02 +00:00
name = models.CharField(_("name"), max_length=128, validators=[validators.validate_name],
blank=settings.WEBAPPS_ALLOW_BLANK_NAME)
2014-05-08 16:59:35 +00:00
type = models.CharField(_("type"), max_length=32,
2014-10-24 10:16:46 +00:00
choices=dict_setting_to_choices(settings.WEBAPPS_TYPES),
2014-05-08 16:59:35 +00:00
default=settings.WEBAPPS_DEFAULT_TYPE)
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
related_name='webapps')
class Meta:
unique_together = ('name', 'account')
verbose_name = _("Web App")
verbose_name_plural = _("Web Apps")
def __unicode__(self):
2014-11-09 10:16:07 +00:00
return self.get_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()
def clean(self):
# Validate unique webapp names
if self.app_type.get('unique_name', False):
try:
webapp = WebApp.objects.exclude(id=self.pk).get(name=self.name, type=self.type)
except WebApp.DoesNotExist:
pass
else:
raise ValidationError({
'name': _("A webapp with this name already exists."),
})
2014-05-08 16:59:35 +00:00
@cached
def get_options(self):
return { opt.name: opt.value for opt in self.options.all() }
2014-11-27 19:17:26 +00:00
@property
def app_type(self):
return settings.WEBAPPS_TYPES[self.type]
2014-11-09 10:16:07 +00:00
def get_name(self):
2014-11-09 10:17:59 +00:00
return self.name or settings.WEBAPPS_BLANK_NAME
2014-11-09 10:16:07 +00:00
2014-05-08 16:59:35 +00:00
def get_fpm_port(self):
return settings.WEBAPPS_FPM_START_PORT + self.account_id
2014-05-08 16:59:35 +00:00
2014-10-24 10:16:46 +00:00
def get_directive(self):
2014-11-27 19:17:26 +00:00
directive = self.app_type['directive']
2014-10-24 10:16:46 +00:00
args = directive[1:] if len(directive) > 1 else ()
return directive[0], args
2014-05-08 16:59:35 +00:00
def get_path(self):
context = {
'home': self.get_user().get_home(),
2014-11-09 10:25:02 +00:00
'app_name': self.get_name(),
2014-05-08 16:59:35 +00:00
}
return settings.WEBAPPS_BASE_ROOT % context
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,
2014-10-24 10:16:46 +00:00
choices=tuple_setting_to_choices(settings.WEBAPPS_OPTIONS))
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
def clean(self):
""" validates name and value according to WEBAPPS_OPTIONS """
regex = settings.WEBAPPS_OPTIONS[self.name][-1]
2014-05-08 16:59:35 +00:00
if not re.match(regex, self.value):
2014-11-05 20:22:01 +00:00
raise ValidationError({
'value': ValidationError(_("'%(value)s' does not match %(regex)s."),
params={
'value': self.value,
'regex': regex
}),
})
2014-05-08 16:59:35 +00:00
services.register(WebApp)