2015-05-07 14:09:37 +00:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.db import models
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from orchestra.core.validators import validate_hostname
|
|
|
|
|
|
|
|
from . import settings
|
|
|
|
|
|
|
|
|
|
|
|
class VPS(models.Model):
|
|
|
|
hostname = models.CharField(_("hostname"), max_length=256, unique=True,
|
2015-04-05 10:46:24 +00:00
|
|
|
validators=[validate_hostname])
|
2014-05-08 16:59:35 +00:00
|
|
|
type = models.CharField(_("type"), max_length=64, choices=settings.VPS_TYPES,
|
2015-04-05 10:46:24 +00:00
|
|
|
default=settings.VPS_DEFAULT_TYPE)
|
2014-05-08 16:59:35 +00:00
|
|
|
template = models.CharField(_("template"), max_length=64,
|
2015-08-05 22:58:35 +00:00
|
|
|
choices=settings.VPS_TEMPLATES, default=settings.VPS_DEFAULT_TEMPLATE,
|
|
|
|
help_text=_("Initial template."))
|
2021-04-22 08:28:00 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("Account"), related_name='vpss')
|
2016-04-06 19:00:16 +00:00
|
|
|
is_active = models.BooleanField(_("active"), default=True)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "VPS"
|
|
|
|
verbose_name_plural = "VPSs"
|
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.hostname
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def set_password(self, raw_password):
|
|
|
|
self.password = make_password(raw_password)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_username(self):
|
|
|
|
return self.hostname
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-03-31 16:18:38 +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
|
|
|
|
2016-04-06 19:00:16 +00:00
|
|
|
@property
|
|
|
|
def active(self):
|
|
|
|
return self.is_active and self.account.is_active
|
|
|
|
|