jumpserver/apps/assets/models/platform.py

71 lines
3.1 KiB
Python
Raw Normal View History

2022-04-02 10:35:46 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2022-04-07 10:51:35 +00:00
from assets.const import Category, AllTypes
2022-07-17 06:28:55 +00:00
from common.db.fields import JsonDictTextField
2022-04-02 10:35:46 +00:00
2022-04-20 02:15:20 +00:00
2022-08-18 09:58:59 +00:00
__all__ = ['Platform', 'PlatformProtocol']
class PlatformProtocol(models.Model):
name = models.CharField(max_length=32, verbose_name=_('Name'))
port = models.IntegerField(verbose_name=_('Port'))
setting = models.JSONField(verbose_name=_('Setting'), default=dict)
2022-04-02 10:35:46 +00:00
class Platform(models.Model):
2022-08-10 11:27:08 +00:00
"""
对资产提供 约束和默认值
对资产进行抽象
"""
2022-04-02 10:35:46 +00:00
CHARSET_CHOICES = (
('utf8', 'UTF-8'),
('gbk', 'GBK'),
)
name = models.SlugField(verbose_name=_("Name"), unique=True, allow_unicode=True)
2022-08-24 08:14:32 +00:00
category = models.CharField(default='host', max_length=32, verbose_name=_("Category"))
type = models.CharField(max_length=32, default='linux', verbose_name=_("Type"))
2022-04-02 10:35:46 +00:00
charset = models.CharField(default='utf8', choices=CHARSET_CHOICES, max_length=8, verbose_name=_("Charset"))
meta = JsonDictTextField(blank=True, null=True, verbose_name=_("Meta"))
internal = models.BooleanField(default=False, verbose_name=_("Internal"))
comment = models.TextField(blank=True, null=True, verbose_name=_("Comment"))
2022-04-30 15:19:43 +00:00
domain_enabled = models.BooleanField(default=True, verbose_name=_("Domain enabled"))
domain_default = models.ForeignKey(
'assets.Domain', null=True, on_delete=models.SET_NULL,
verbose_name=_("Domain default")
)
protocols_enabled = models.BooleanField(default=True, verbose_name=_("Protocols enabled"))
2022-08-18 09:58:59 +00:00
protocols = models.ManyToManyField(PlatformProtocol, blank=True, verbose_name=_("Protocols"))
2022-08-10 11:27:08 +00:00
# Accounts
# 这应该和账号有关
2022-08-15 10:31:57 +00:00
su_enabled = models.BooleanField(default=False, verbose_name=_("Su enabled"))
2022-08-10 11:27:08 +00:00
su_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("SU method"))
ping_enabled = models.BooleanField(default=False)
ping_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Ping method"))
verify_account_enabled = models.BooleanField(default=False, verbose_name=_("Verify account enabled"))
verify_account_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Verify account method"))
create_account_enabled = models.BooleanField(default=False, verbose_name=_("Create account enabled"))
create_account_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Create account method"))
change_password_enabled = models.BooleanField(default=False, verbose_name=_("Change password enabled"))
change_password_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Change password method"))
2022-04-02 10:35:46 +00:00
2022-05-05 08:18:05 +00:00
@property
2022-08-10 11:27:08 +00:00
def type_constraints(self):
return AllTypes.get_constraints(self.category, self.type)
2022-05-02 13:37:42 +00:00
2022-04-02 10:35:46 +00:00
@classmethod
def default(cls):
linux, created = cls.objects.get_or_create(
defaults={'name': 'Linux'}, name='Linux'
)
return linux.id
def __str__(self):
return self.name
class Meta:
verbose_name = _("Platform")
# ordering = ('name',)