2015-03-10 21:51:10 +00:00
|
|
|
import os
|
2015-04-13 14:46:10 +00:00
|
|
|
from collections import OrderedDict
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from django.db import models
|
2015-03-10 11:46:48 +00:00
|
|
|
from django.utils.functional import cached_property
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-05-07 14:09:37 +00:00
|
|
|
from orchestra.core import validators
|
2015-10-29 18:19:00 +00:00
|
|
|
from orchestra.utils.functional import cached
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
from . import settings
|
|
|
|
from .directives import SiteDirective
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Website(models.Model):
|
2014-11-27 19:17:26 +00:00
|
|
|
""" Models a web site, also known as virtual host """
|
2015-03-10 21:51:10 +00:00
|
|
|
HTTP = 'http'
|
|
|
|
HTTPS = 'https'
|
|
|
|
HTTP_AND_HTTPS = 'http/https'
|
|
|
|
HTTPS_ONLY = 'https-only'
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-02-27 16:57:39 +00:00
|
|
|
name = models.CharField(_("name"), max_length=128,
|
2015-04-05 10:46:24 +00:00
|
|
|
validators=[validators.validate_name])
|
2021-04-22 08:28:00 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("Account"), related_name='websites')
|
2015-03-10 21:51:10 +00:00
|
|
|
protocol = models.CharField(_("protocol"), max_length=16,
|
2015-04-05 10:46:24 +00:00
|
|
|
choices=settings.WEBSITES_PROTOCOL_CHOICES,
|
|
|
|
default=settings.WEBSITES_DEFAULT_PROTOCOL,
|
|
|
|
help_text=_("Select the protocol(s) for this website<br>"
|
|
|
|
"<tt>HTTPS only</tt> performs a redirection from <tt>http</tt> to <tt>https</tt>."))
|
2015-03-10 21:51:10 +00:00
|
|
|
# port = models.PositiveIntegerField(_("port"),
|
|
|
|
# choices=settings.WEBSITES_PORT_CHOICES,
|
|
|
|
# default=settings.WEBSITES_DEFAULT_PORT)
|
2015-05-20 18:21:21 +00:00
|
|
|
domains = models.ManyToManyField(settings.WEBSITES_DOMAIN_MODEL, blank=True,
|
2015-04-05 10:46:24 +00:00
|
|
|
related_name='websites', verbose_name=_("domains"))
|
2014-05-08 16:59:35 +00:00
|
|
|
contents = models.ManyToManyField('webapps.WebApp', through='websites.Content')
|
2021-04-22 08:28:00 +00:00
|
|
|
target_server = models.ForeignKey('orchestration.Server', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("Target Server"), related_name='websites')
|
2014-09-30 10:20:11 +00:00
|
|
|
is_active = models.BooleanField(_("active"), default=True)
|
2021-01-13 15:41:36 +00:00
|
|
|
comments = models.TextField(default="", blank=True)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-02-27 16:57:39 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('name', 'account')
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-05-08 16:59:35 +00:00
|
|
|
return self.name
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
@property
|
|
|
|
def unique_name(self):
|
2015-03-18 21:51:12 +00:00
|
|
|
context = self.get_settings_context()
|
|
|
|
return settings.WEBSITES_UNIQUE_NAME_FORMAT % context
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-07 15:14:49 +00:00
|
|
|
@cached_property
|
|
|
|
def active(self):
|
|
|
|
return self.is_active and self.account.is_active
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-05-09 17:08:45 +00:00
|
|
|
def disable(self):
|
|
|
|
self.is_active = False
|
|
|
|
self.save(update_fields=('is_active',))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-03-31 16:18:38 +00:00
|
|
|
def enable(self):
|
|
|
|
self.is_active = False
|
|
|
|
self.save(update_fields=('is_active',))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-18 21:51:12 +00:00
|
|
|
def get_settings_context(self):
|
|
|
|
""" format settings strings """
|
|
|
|
return {
|
2015-02-27 16:57:39 +00:00
|
|
|
'id': self.id,
|
|
|
|
'pk': self.pk,
|
2015-03-18 21:51:12 +00:00
|
|
|
'home': self.get_user().get_home(),
|
|
|
|
'user': self.get_username(),
|
|
|
|
'group': self.get_groupname(),
|
|
|
|
'site_name': self.name,
|
2015-03-10 21:51:10 +00:00
|
|
|
'protocol': self.protocol,
|
2015-02-27 16:57:39 +00:00
|
|
|
}
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-10 21:51:10 +00:00
|
|
|
def get_protocol(self):
|
|
|
|
if self.protocol in (self.HTTP, self.HTTP_AND_HTTPS):
|
|
|
|
return self.HTTP
|
|
|
|
return self.HTTPS
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-10-29 18:19:00 +00:00
|
|
|
@cached
|
2015-03-10 11:46:48 +00:00
|
|
|
def get_directives(self):
|
2015-04-13 14:46:10 +00:00
|
|
|
directives = OrderedDict()
|
|
|
|
for opt in self.directives.all().order_by('name', 'value'):
|
2015-03-10 21:51:10 +00:00
|
|
|
try:
|
|
|
|
directives[opt.name].append(opt.value)
|
|
|
|
except KeyError:
|
|
|
|
directives[opt.name] = [opt.value]
|
|
|
|
return directives
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def get_absolute_url(self):
|
2016-02-17 12:32:30 +00:00
|
|
|
try:
|
|
|
|
domain = self.domains.all()[0]
|
|
|
|
except IndexError:
|
|
|
|
return
|
|
|
|
else:
|
2015-03-10 21:51:10 +00:00
|
|
|
return '%s://%s' % (self.get_protocol(), domain)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-18 21:51:12 +00:00
|
|
|
def get_user(self):
|
|
|
|
return self.account.main_systemuser
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-18 21:51:12 +00:00
|
|
|
def get_username(self):
|
|
|
|
return self.get_user().username
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-18 21:51:12 +00:00
|
|
|
def get_groupname(self):
|
|
|
|
return self.get_username()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-02 10:37:25 +00:00
|
|
|
def get_www_access_log_path(self):
|
2015-03-18 21:51:12 +00:00
|
|
|
context = self.get_settings_context()
|
2015-04-27 14:54:17 +00:00
|
|
|
context['unique_name'] = self.unique_name
|
2015-03-02 10:37:25 +00:00
|
|
|
path = settings.WEBSITES_WEBSITE_WWW_ACCESS_LOG_PATH % context
|
2015-03-27 19:50:54 +00:00
|
|
|
return os.path.normpath(path)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-02 10:37:25 +00:00
|
|
|
def get_www_error_log_path(self):
|
2015-03-18 21:51:12 +00:00
|
|
|
context = self.get_settings_context()
|
2015-04-27 14:54:17 +00:00
|
|
|
context['unique_name'] = self.unique_name
|
2015-03-02 10:37:25 +00:00
|
|
|
path = settings.WEBSITES_WEBSITE_WWW_ERROR_LOG_PATH % context
|
2015-03-27 19:50:54 +00:00
|
|
|
return os.path.normpath(path)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
|
2015-03-18 21:51:12 +00:00
|
|
|
class WebsiteDirective(models.Model):
|
2021-04-22 08:28:00 +00:00
|
|
|
website = models.ForeignKey(Website, on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("web site"), related_name='directives')
|
2016-02-19 10:11:28 +00:00
|
|
|
name = models.CharField(_("name"), max_length=128, db_index=True,
|
2016-02-17 12:32:30 +00:00
|
|
|
choices=SiteDirective.get_choices())
|
2016-03-11 12:19:34 +00:00
|
|
|
value = models.CharField(_("value"), max_length=256, blank=True)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-05-08 16:59:35 +00:00
|
|
|
return self.name
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
@cached_property
|
|
|
|
def directive_class(self):
|
2015-03-31 12:39:08 +00:00
|
|
|
return SiteDirective.get(self.name)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
@cached_property
|
|
|
|
def directive_instance(self):
|
|
|
|
""" Per request lived directive instance """
|
|
|
|
return self.directive_class()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean(self):
|
2015-03-10 11:46:48 +00:00
|
|
|
self.directive_instance.validate(self)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Content(models.Model):
|
2015-03-10 16:57:23 +00:00
|
|
|
# related_name is content_set to differentiate between website.content -> webapp
|
2021-04-22 08:28:00 +00:00
|
|
|
webapp = models.ForeignKey('webapps.WebApp', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("web application"))
|
|
|
|
website = models.ForeignKey('websites.Website', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("web site"))
|
2014-10-21 16:13:18 +00:00
|
|
|
path = models.CharField(_("path"), max_length=256, blank=True,
|
2015-04-05 10:46:24 +00:00
|
|
|
validators=[validators.validate_url_path])
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('website', 'path')
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-10-14 13:50:19 +00:00
|
|
|
try:
|
|
|
|
return self.website.name + self.path
|
|
|
|
except Website.DoesNotExist:
|
|
|
|
return self.path
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-09-28 10:51:03 +00:00
|
|
|
def clean_fields(self, *args, **kwargs):
|
|
|
|
self.path = self.path.strip()
|
|
|
|
return super(Content, self).clean_fields(*args, **kwargs)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean(self):
|
2015-03-29 16:10:07 +00:00
|
|
|
if not self.path:
|
|
|
|
self.path = '/'
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def get_absolute_url(self):
|
2016-02-17 12:32:30 +00:00
|
|
|
try:
|
|
|
|
domain = self.website.domains.all()[0]
|
|
|
|
except IndexError:
|
|
|
|
return
|
|
|
|
else:
|
2015-03-10 21:51:10 +00:00
|
|
|
return '%s://%s%s' % (self.website.get_protocol(), domain, self.path)
|