2015-01-26 22:47:49 +01:00
|
|
|
"""
|
|
|
|
This module defines the database models for website handling.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from __future__ import absolute_import
|
2015-01-26 22:47:49 +01:00
|
|
|
|
2015-01-27 18:40:22 +01:00
|
|
|
from django.db import models, transaction
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2015-01-26 22:47:49 +01:00
|
|
|
|
|
|
|
from domains.models import HostingDomain
|
2015-01-27 18:40:22 +01:00
|
|
|
from fileservertasks.tasks import (
|
|
|
|
create_file_website_hierarchy,
|
2015-01-27 18:42:07 +01:00
|
|
|
delete_file_website_hierarchy,
|
2015-01-27 18:40:22 +01:00
|
|
|
)
|
2023-02-18 22:46:48 +01:00
|
|
|
from osusers.models import User as OsUser
|
2015-01-27 18:40:22 +01:00
|
|
|
from webtasks.tasks import (
|
|
|
|
create_web_php_fpm_pool_config,
|
|
|
|
create_web_vhost_config,
|
2015-01-27 18:42:07 +01:00
|
|
|
delete_web_php_fpm_pool_config,
|
|
|
|
delete_web_vhost_config,
|
|
|
|
disable_web_vhost,
|
2015-01-27 18:40:22 +01:00
|
|
|
enable_web_vhost,
|
|
|
|
)
|
|
|
|
|
2015-01-26 22:47:49 +01:00
|
|
|
|
|
|
|
class Website(models.Model):
|
|
|
|
"""
|
|
|
|
This is the model for a website.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
subdomain = models.CharField(_("sub domain"), max_length=64)
|
2015-01-26 22:47:49 +01:00
|
|
|
osuser = models.ForeignKey(
|
2023-02-18 22:46:48 +01:00
|
|
|
OsUser, verbose_name=_("operating system user"), on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
domain = models.ForeignKey(HostingDomain, models.CASCADE, verbose_name=_("domain"))
|
|
|
|
wildcard = models.BooleanField(_("wildcard"), default=False)
|
2015-01-26 22:47:49 +01:00
|
|
|
|
|
|
|
class Meta:
|
2023-02-18 22:46:48 +01:00
|
|
|
unique_together = [("domain", "subdomain")]
|
|
|
|
verbose_name = _("website")
|
|
|
|
verbose_name_plural = _("websites")
|
2015-01-26 21:00:52 +01:00
|
|
|
|
2015-01-26 22:47:49 +01:00
|
|
|
def __str__(self):
|
2015-01-27 18:40:22 +01:00
|
|
|
return self.get_fqdn()
|
|
|
|
|
|
|
|
def get_fqdn(self):
|
2015-01-26 22:47:49 +01:00
|
|
|
return "{subdomain}.{domain}".format(
|
|
|
|
subdomain=self.subdomain, domain=self.domain.domain
|
|
|
|
)
|
2015-01-27 18:40:22 +01:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.pk:
|
|
|
|
fqdn = self.get_fqdn()
|
|
|
|
if not Website.objects.filter(osuser=self.osuser).exists():
|
2023-02-18 22:46:48 +01:00
|
|
|
create_web_php_fpm_pool_config.delay(self.osuser.username).get()
|
|
|
|
create_file_website_hierarchy.delay(self.osuser.username, fqdn).get()
|
2015-01-27 18:40:22 +01:00
|
|
|
create_web_vhost_config.delay(
|
2023-02-18 22:46:48 +01:00
|
|
|
self.osuser.username, fqdn, self.wildcard
|
|
|
|
).get()
|
2015-01-27 18:40:22 +01:00
|
|
|
enable_web_vhost.delay(fqdn).get()
|
|
|
|
return super(Website, self).save(*args, **kwargs)
|
2015-01-27 18:42:07 +01:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
fqdn = self.get_fqdn()
|
|
|
|
osuser = self.osuser
|
|
|
|
disable_web_vhost.delay(fqdn).get()
|
|
|
|
delete_web_vhost_config.delay(fqdn).get()
|
|
|
|
delete_file_website_hierarchy.delay(osuser.username, fqdn).get()
|
|
|
|
deleted = super(Website, self).delete(*args, **kwargs)
|
|
|
|
if not Website.objects.filter(osuser=osuser):
|
|
|
|
delete_web_php_fpm_pool_config.delay(osuser.username).get()
|
|
|
|
return deleted
|