django-orchestra/orchestra/contrib/vps/models.py

47 lines
1.4 KiB
Python
Raw Normal View History

from django.contrib.auth.hashers import make_password
2014-05-08 16:59:35 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
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."))
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)
2014-05-08 16:59:35 +00:00
class Meta:
verbose_name = "VPS"
verbose_name_plural = "VPSs"
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-05-08 16:59:35 +00:00
return self.hostname
2014-05-08 16:59:35 +00:00
def set_password(self, raw_password):
self.password = make_password(raw_password)
2014-05-08 16:59:35 +00:00
def get_username(self):
return self.hostname
2016-03-31 16:18:38 +00:00
def disable(self):
self.is_active = False
self.save(update_fields=('is_active',))
2016-03-31 16:18:38 +00:00
def enable(self):
self.is_active = False
self.save(update_fields=('is_active',))
2016-04-06 19:00:16 +00:00
@property
def active(self):
return self.is_active and self.account.is_active