Jan Dittberner
1f485e6b29
- implement delete method and let it call these tasks: - disable_web_vhost - delete_web_vhost_config - delete_file_website_hierarchy - delete_web_php_fpm_pool_config if this was the last website of the user
79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
"""
|
|
This module defines the database models for website handling.
|
|
|
|
"""
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
from django.db import models, transaction
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from domains.models import HostingDomain
|
|
from osusers.models import User as OsUser
|
|
|
|
from fileservertasks.tasks import (
|
|
create_file_website_hierarchy,
|
|
delete_file_website_hierarchy,
|
|
)
|
|
from webtasks.tasks import (
|
|
create_web_php_fpm_pool_config,
|
|
create_web_vhost_config,
|
|
delete_web_php_fpm_pool_config,
|
|
delete_web_vhost_config,
|
|
disable_web_vhost,
|
|
enable_web_vhost,
|
|
)
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
class Website(models.Model):
|
|
"""
|
|
This is the model for a website.
|
|
|
|
"""
|
|
subdomain = models.CharField(
|
|
_('sub domain'), max_length=64)
|
|
osuser = models.ForeignKey(
|
|
OsUser, verbose_name=_('operating system user'))
|
|
domain = models.ForeignKey(
|
|
HostingDomain, verbose_name=_('domain'))
|
|
wildcard = models.BooleanField(_('wildcard'), default=False)
|
|
|
|
class Meta:
|
|
unique_together = [('domain', 'subdomain')]
|
|
verbose_name = _('website')
|
|
verbose_name_plural = _('websites')
|
|
|
|
def __str__(self):
|
|
return self.get_fqdn()
|
|
|
|
def get_fqdn(self):
|
|
return "{subdomain}.{domain}".format(
|
|
subdomain=self.subdomain, domain=self.domain.domain
|
|
)
|
|
|
|
@transaction.atomic
|
|
def save(self, *args, **kwargs):
|
|
if not self.pk:
|
|
fqdn = self.get_fqdn()
|
|
if not Website.objects.filter(osuser=self.osuser).exists():
|
|
create_web_php_fpm_pool_config.delay(
|
|
self.osuser.username).get()
|
|
create_file_website_hierarchy.delay(
|
|
self.osuser.username, fqdn).get()
|
|
create_web_vhost_config.delay(
|
|
self.osuser.username, fqdn, self.wildcard).get()
|
|
enable_web_vhost.delay(fqdn).get()
|
|
return super(Website, self).save(*args, **kwargs)
|
|
|
|
@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
|