添加 choices 获取 label方法

pull/8873/head
feng626 2022-08-22 11:47:45 +08:00
parent fe4df4b179
commit a7d193464e
3 changed files with 22 additions and 11 deletions

View File

@ -1,6 +1,6 @@
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from common.db.models import IncludesTextChoicesMeta from common.db.models import IncludesTextChoicesMeta, ChoicesMixin
from common.tree import TreeNode from common.tree import TreeNode
@ -24,7 +24,7 @@ class PlatformMixin:
} }
class Category(PlatformMixin, models.TextChoices): class Category(PlatformMixin, ChoicesMixin, models.TextChoices):
HOST = 'host', _('Host') HOST = 'host', _('Host')
NETWORK = 'network', _("NetworkDevice") NETWORK = 'network', _("NetworkDevice")
DATABASE = 'database', _("Database") DATABASE = 'database', _("Database")
@ -60,7 +60,7 @@ class Category(PlatformMixin, models.TextChoices):
} }
class HostTypes(PlatformMixin, models.TextChoices): class HostTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
LINUX = 'linux', 'Linux' LINUX = 'linux', 'Linux'
WINDOWS = 'windows', 'Windows' WINDOWS = 'windows', 'Windows'
UNIX = 'unix', 'Unix' UNIX = 'unix', 'Unix'
@ -84,14 +84,14 @@ class HostTypes(PlatformMixin, models.TextChoices):
} }
class NetworkTypes(PlatformMixin, models.TextChoices): class NetworkTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
SWITCH = 'switch', _("Switch") SWITCH = 'switch', _("Switch")
ROUTER = 'router', _("Router") ROUTER = 'router', _("Router")
FIREWALL = 'firewall', _("Firewall") FIREWALL = 'firewall', _("Firewall")
OTHER_NETWORK = 'other_network', _("Other device") OTHER_NETWORK = 'other_network', _("Other device")
class DatabaseTypes(PlatformMixin, models.TextChoices): class DatabaseTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
MYSQL = 'mysql', 'MySQL' MYSQL = 'mysql', 'MySQL'
MARIADB = 'mariadb', 'MariaDB' MARIADB = 'mariadb', 'MariaDB'
POSTGRESQL = 'postgresql', 'PostgreSQL' POSTGRESQL = 'postgresql', 'PostgreSQL'
@ -110,15 +110,15 @@ class DatabaseTypes(PlatformMixin, models.TextChoices):
return meta return meta
class WebTypes(PlatformMixin, models.TextChoices): class WebTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
General = 'general', 'General' General = 'general', 'General'
class CloudTypes(PlatformMixin, models.TextChoices): class CloudTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
K8S = 'k8s', 'Kubernetes' K8S = 'k8s', 'Kubernetes'
class AllTypes(metaclass=IncludesTextChoicesMeta): class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta):
choices: list choices: list
includes = [ includes = [
HostTypes, NetworkTypes, DatabaseTypes, HostTypes, NetworkTypes, DatabaseTypes,
@ -202,7 +202,7 @@ class AllTypes(metaclass=IncludesTextChoicesMeta):
return nodes return nodes
class Protocol(models.TextChoices): class Protocol(ChoicesMixin, models.TextChoices):
ssh = 'ssh', 'SSH' ssh = 'ssh', 'SSH'
rdp = 'rdp', 'RDP' rdp = 'rdp', 'RDP'
telnet = 'telnet', 'Telnet' telnet = 'telnet', 'Telnet'

View File

@ -10,6 +10,7 @@ from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from orgs.mixins.models import OrgManager, JMSOrgBaseModel from orgs.mixins.models import OrgManager, JMSOrgBaseModel
from ...const import Category
from ..platform import Platform from ..platform import Platform
from ..base import AbsConnectivity from ..base import AbsConnectivity
@ -121,7 +122,8 @@ class Asset(AbsConnectivity, NodesRelationMixin, JMSOrgBaseModel):
@property @property
def type_display(self): def type_display(self):
return self.platform.type value = self.platform.type
return Category.get_label(value)
@property @property
def category(self): def category(self):
@ -129,7 +131,8 @@ class Asset(AbsConnectivity, NodesRelationMixin, JMSOrgBaseModel):
@property @property
def category_display(self): def category_display(self):
return self.platform.category value = self.platform.category
return Category.get_label(value)
def as_node(self): def as_node(self):
from assets.models import Node from assets.models import Node

View File

@ -85,6 +85,14 @@ class BitOperationChoice:
return [(cls.NAME_MAP[i], j) for i, j in cls.DB_CHOICES] return [(cls.NAME_MAP[i], j) for i, j in cls.DB_CHOICES]
class ChoicesMixin:
_value2label_map_: dict
@classmethod
def get_label(cls, value: (str, int)):
return cls._value2label_map_[value]
class BaseCreateUpdateModel(models.Model): class BaseCreateUpdateModel(models.Model):
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
updated_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Updated by')) updated_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Updated by'))