class Asset(ProtocolsMixin, OrgModelMixin): # Important PLATFORM_CHOICES = ( ('Linux', 'Linux'), ('Unix', 'Unix'), ('MacOS', 'MacOS'), ('BSD', 'BSD'), ('Windows', 'Windows'), ('Windows2016', 'Windows(2016)'), ('Other', 'Other'), ) id = models.UUIDField(default=uuid.uuid4, primary_key=True) ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True) hostname = models.CharField(max_length=128, verbose_name=_('Hostname')) protocol = models.CharField(max_length=128, default=ProtocolsMixin.PROTOCOL_SSH, choices=ProtocolsMixin.PROTOCOL_CHOICES, verbose_name=_('Protocol')) port = models.IntegerField(default=22, verbose_name=_('Port')) protocols = models.CharField(max_length=128, default='ssh/22', blank=True, verbose_name=_("Protocols")) platform = models.CharField(max_length=128, choices=PLATFORM_CHOICES, default='Linux', verbose_name=_('Platform')) # domain = models.ForeignKey("assets.Domain", null=True, blank=True, related_name='assets', verbose_name=_("Domain"), on_delete=models.SET_NULL) nodes = models.ManyToManyField('assets.Node', default=default_node, related_name='assets', verbose_name=_("Nodes")) is_active = models.BooleanField(default=True, verbose_name=_('Is active')) # Auth admin_user = models.ForeignKey('assets.AdminUser', on_delete=models.PROTECT, null=True, verbose_name=_("Admin user"), related_name='assets') # Some information public_ip = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Public IP')) number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number')) # Collect vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) labels = models.ManyToManyField('assets.Label', blank=True, related_name='assets', verbose_name=_("Labels")) created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created')) comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment')) objects = OrgManager.from_queryset(AssetQuerySet)() _connectivity = None def __str__(self): return '{0.hostname}({0.ip})'.format(self) @property def is_valid(self): warning = '' if not self.is_active: warning += ' inactive' if warning: return False, warning return True, warning def is_windows(self): if self.platform in ("Windows", "Windows2016"): return True else: return False def is_unixlike(self): if self.platform not in ("Windows", "Windows2016", "Other"): return True else: return False def is_support_ansible(self): return self.has_protocol('ssh') and self.platform not in ("Other", ) def get_nodes(self): from .node import Node nodes = self.nodes.all() or [Node.root()] return nodes def get_all_nodes(self, flat=False): nodes = [] for node in self.get_nodes(): _nodes = node.get_ancestor(with_self=True) nodes.append(_nodes) if flat: nodes = list(reduce(lambda x, y: set(x) | set(y), nodes)) return nodes @property def cpu_info(self): info = "" if self.cpu_model: info += self.cpu_model if self.cpu_count and self.cpu_cores: info += "{}*{}".format(self.cpu_count, self.cpu_cores) return info @property def hardware_info(self): if self.cpu_count: return '{} Core {} {}'.format( self.cpu_vcpus or self.cpu_count * self.cpu_cores, self.memory, self.disk_total) else: return '' @property def connectivity(self): if self._connectivity: return self._connectivity if not self.admin_user: return Connectivity.unknown() connectivity = self.admin_user.get_asset_connectivity(self) return connectivity @connectivity.setter def connectivity(self, value): if not self.admin_user: return self.admin_user.set_asset_connectivity(self, value) def get_auth_info(self): if not self.admin_user: return {} self.admin_user.load_specific_asset_auth(self) info = { 'username': self.admin_user.username, 'password': self.admin_user.password, 'private_key': self.admin_user.private_key_file, } return info def as_node(self): from .node import Node fake_node = Node() fake_node.id = self.id fake_node.key = self.id fake_node.value = self.hostname fake_node.asset = self fake_node.is_node = False return fake_node def as_tree_node(self, parent_node): from common.tree import TreeNode icon_skin = 'file' if self.platform.lower() == 'windows': icon_skin = 'windows' elif self.platform.lower() == 'linux': icon_skin = 'linux' data = { 'id': str(self.id), 'name': self.hostname, 'title': self.ip, 'pId': parent_node.key, 'isParent': False, 'open': False, 'iconSkin': icon_skin, 'meta': { 'type': 'asset', 'asset': { 'id': self.id, 'hostname': self.hostname, 'ip': self.ip, 'protocols': self.protocols_as_list, 'platform': self.platform, } } } tree_node = TreeNode(**data) return tree_node class Meta: unique_together = [('org_id', 'hostname')] verbose_name = _("Asset") @classmethod def generate_fake(cls, count=100): from random import seed, choice from django.db import IntegrityError from .node import Node from orgs.utils import get_current_org from orgs.models import Organization org = get_current_org() if not org or not org.is_real(): Organization.default().change_to() nodes = list(Node.objects.all()) seed() for i in range(count): ip = [str(i) for i in random.sample(range(255), 4)] asset = cls(ip='.'.join(ip), hostname='.'.join(ip), admin_user=choice(AdminUser.objects.all()), created_by='Fake') try: asset.save() asset.protocols = 'ssh/22' if nodes and len(nodes) > 3: _nodes = random.sample(nodes, 3) else: _nodes = [Node.default_node()] asset.nodes.set(_nodes) asset.system_users = [ choice(SystemUser.objects.all()) for i in range(3) ] logger.debug('Generate fake asset : %s' % asset.ip) except IntegrityError: print('Error continue') continue
class Asset(OrgModelMixin): # Important PLATFORM_CHOICES = PLATFORM_CHOICES ENV_CHOICES = ENV_CHOICES PROTOCOL_SSH = 'ssh' PROTOCOL_RDP = 'rdp' PROTOCOL_TELNET = 'telnet' PROTOCOL_VNC = 'vnc' PROTOCOL_CHOICES = ( (PROTOCOL_SSH, 'ssh'), (PROTOCOL_RDP, 'rdp'), (PROTOCOL_TELNET, 'telnet (beta)'), (PROTOCOL_VNC, 'vnc'), ) id = models.UUIDField(default=uuid.uuid4, primary_key=True) ip = models.GenericIPAddressField(max_length=32, verbose_name=_('IP'), db_index=True) hostname = models.CharField(max_length=128, verbose_name=_('Hostname')) protocol = models.CharField(max_length=128, default=PROTOCOL_SSH, choices=PROTOCOL_CHOICES, verbose_name=_('Protocol')) port = models.IntegerField(default=22, verbose_name=_('Port')) platform = models.CharField(max_length=128, choices=PLATFORM_CHOICES, default='Linux', verbose_name=_('Platform')) domain = models.ForeignKey("assets.Domain", null=True, blank=True, related_name='assets', verbose_name=_("Domain"), on_delete=models.SET_NULL) nodes = models.ManyToManyField('assets.Node', default=default_node, related_name='assets', verbose_name=_("Nodes")) is_active = models.BooleanField(default=True, verbose_name=_('Is active')) # Auth admin_user = models.ForeignKey('assets.AdminUser', on_delete=models.PROTECT, null=True, verbose_name=_("Admin user")) # Some information public_ip = models.GenericIPAddressField(max_length=32, blank=True, null=True, verbose_name=_('Public IP')) number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number')) # Collect vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) labels = models.ManyToManyField('assets.Label', blank=True, related_name='assets', verbose_name=_("Labels")) created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created')) comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment')) objects = OrgManager.from_queryset(AssetQuerySet)() CONNECTIVITY_CACHE_KEY = '_JMS_ASSET_CONNECTIVITY_{}' UNREACHABLE, REACHABLE, UNKNOWN = range(0, 3) CONNECTIVITY_CHOICES = ( (UNREACHABLE, _("Unreachable")), (REACHABLE, _('Reachable')), (UNKNOWN, _("Unknown")), ) environment = models.CharField(max_length=32, choices=ENV_CHOICES, default='DEV', verbose_name=_('Environment')) # Project projects = models.ManyToManyField( Project, blank=True, verbose_name=_('Projects'), related_name='assets', ) @property def projects_amount(self): return len(self.projects.all()) def __str__(self): return '{0.hostname}({0.ip})'.format(self) @property def is_valid(self): warning = '' if not self.is_active: warning += ' inactive' else: return True, '' return False, warning def support_ansible(self): if self.platform in ("Windows", "Windows2016", "Other"): return False if self.protocol != 'ssh': return False return True def is_unixlike(self): if self.platform not in ("Windows", "Windows2016"): return True else: return False def get_nodes(self): from .node import Node nodes = self.nodes.all() or [Node.root()] return nodes def get_all_nodes(self, flat=False): nodes = [] for node in self.get_nodes(): _nodes = node.get_ancestor(with_self=True) nodes.append(_nodes) if flat: nodes = list(reduce(lambda x, y: set(x) | set(y), nodes)) return nodes @classmethod def get_queryset_by_fullname_list(cls, fullname_list): org_fullname_map = defaultdict(list) for fullname in fullname_list: hostname, org = cls.split_fullname(fullname) org_fullname_map[org].append(hostname) filter_arg = Q() for org, hosts in org_fullname_map.items(): if org.is_real(): filter_arg |= Q(hostname__in=hosts, org_id=org.id) else: filter_arg |= Q(Q(org_id__isnull=True) | Q(org_id=''), hostname__in=hosts) return Asset.objects.filter(filter_arg) @property def hardware_info(self): if self.cpu_count: return '{} Core {} {}'.format( self.cpu_vcpus or self.cpu_count * self.cpu_cores, self.memory, self.disk_total) else: return '' @property def connectivity(self): if not self.is_unixlike(): return self.REACHABLE key = self.CONNECTIVITY_CACHE_KEY.format(str(self.id)) cached = cache.get(key, None) return cached if cached is not None else self.UNKNOWN @connectivity.setter def connectivity(self, value): key = self.CONNECTIVITY_CACHE_KEY.format(str(self.id)) cache.set(key, value, 3600 * 2) def get_auth_info(self): if self.admin_user: self.admin_user.load_specific_asset_auth(self) return { 'username': self.admin_user.username, 'password': self.admin_user.password, 'private_key': self.admin_user.private_key_file, 'become': self.admin_user.become_info, } def as_node(self): from .node import Node fake_node = Node() fake_node.id = self.id fake_node.key = self.id fake_node.value = self.hostname fake_node.asset = self fake_node.is_node = False return fake_node def to_json(self): info = { 'id': self.id, 'hostname': self.hostname, 'ip': self.ip, 'port': self.port, } if self.domain and self.domain.gateway_set.all(): info["gateways"] = [d.id for d in self.domain.gateway_set.all()] return info def _to_secret_json(self): """ Ansible use it create inventory Todo: May be move to ops implements it """ data = self.to_json() if self.admin_user: self.admin_user.load_specific_asset_auth(self) admin_user = self.admin_user data.update({ 'username': admin_user.username, 'password': admin_user.password, 'private_key': admin_user.private_key_file, 'become': admin_user.become_info, 'groups': [node.value for node in self.nodes.all()], }) return data def as_tree_node(self, parent_node): from common.tree import TreeNode icon_skin = 'file' if self.platform.lower() == 'windows': icon_skin = 'windows' elif self.platform.lower() == 'linux': icon_skin = 'linux' data = { 'id': str(self.id), 'name': self.hostname, 'title': self.ip, 'pId': parent_node.key, 'isParent': False, 'open': False, 'iconSkin': icon_skin, 'meta': { 'type': 'asset', 'asset': { 'id': self.id, 'hostname': self.hostname, 'ip': self.ip, 'port': self.port, 'platform': self.platform, 'protocol': self.protocol, } } } tree_node = TreeNode(**data) return tree_node class Meta: unique_together = [('org_id', 'hostname')] verbose_name = _("Asset") @classmethod def generate_fake(cls, count=100): from random import seed, choice import forgery_py from django.db import IntegrityError from .node import Node nodes = list(Node.objects.all()) seed() for i in range(count): ip = [str(i) for i in random.sample(range(255), 4)] asset = cls(ip='.'.join(ip), hostname=forgery_py.internet.user_name(True), admin_user=choice(AdminUser.objects.all()), port=22, created_by='Fake') try: asset.save() if nodes and len(nodes) > 3: _nodes = random.sample(nodes, 3) else: _nodes = [Node.default_node()] asset.nodes.set(_nodes) asset.system_users = [ choice(SystemUser.objects.all()) for i in range(3) ] logger.debug('Generate fake asset : %s' % asset.ip) except IntegrityError: print('Error continue') continue
class Asset(OrgModelMixin): # Important PLATFORM_CHOICES = ( ('Linux', 'Linux'), ('Unix', 'Unix'), ('MacOS', 'MacOS'), ('BSD', 'BSD'), ('Windows', 'Windows'), ('Windows2016', 'Windows(2016)'), ('Other', 'Other'), ) SSH_PROTOCOL = 'ssh' RDP_PROTOCOL = 'rdp' TELNET_PROTOCOL = 'telnet' PROTOCOL_CHOICES = ( (SSH_PROTOCOL, 'ssh'), (RDP_PROTOCOL, 'rdp'), (TELNET_PROTOCOL, 'telnet (beta)'), ) id = models.UUIDField(default=uuid.uuid4, primary_key=True) ip = models.GenericIPAddressField(max_length=32, verbose_name=_('IP'), db_index=True) hostname = models.CharField(max_length=128, verbose_name=_('Hostname')) protocol = models.CharField(max_length=128, default=SSH_PROTOCOL, choices=PROTOCOL_CHOICES, verbose_name=_('Protocol')) port = models.IntegerField(default=22, verbose_name=_('Port')) platform = models.CharField(max_length=128, choices=PLATFORM_CHOICES, default='Linux', verbose_name=_('Platform')) domain = models.ForeignKey("assets.Domain", null=True, blank=True, related_name='assets', verbose_name=_("Domain"), on_delete=models.SET_NULL) nodes = models.ManyToManyField('assets.Node', default=default_node, related_name='assets', verbose_name=_("Nodes")) is_active = models.BooleanField(default=True, verbose_name=_('Is active')) # Auth admin_user = models.ForeignKey('assets.AdminUser', on_delete=models.PROTECT, null=True, verbose_name=_("Admin user")) # Some information public_ip = models.GenericIPAddressField(max_length=32, blank=True, null=True, verbose_name=_('Public IP')) number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number')) # Collect vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) labels = models.ManyToManyField('assets.Label', blank=True, related_name='assets', verbose_name=_("Labels")) created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created')) comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment')) objects = OrgManager.from_queryset(AssetQuerySet)() def __str__(self): return '{0.hostname}({0.ip})'.format(self) @property def is_valid(self): warning = '' if not self.is_active: warning += ' inactive' else: return True, '' return False, warning def is_unixlike(self): if self.platform not in ("Windows", "Windows2016"): return True else: return False def get_nodes(self): from .node import Node nodes = self.nodes.all() or [Node.root()] return nodes def get_all_nodes(self, flat=False): nodes = [] for node in self.get_nodes(): _nodes = node.get_ancestor(with_self=True) _nodes.append(_nodes) if flat: nodes = list(reduce(lambda x, y: set(x) | set(y), nodes)) return nodes @classmethod def get_queryset_by_fullname_list(cls, fullname_list): org_fullname_map = defaultdict(list) for fullname in fullname_list: hostname, org = cls.split_fullname(fullname) org_fullname_map[org].append(hostname) filter_arg = Q() for org, hosts in org_fullname_map.items(): if org.is_real(): filter_arg |= Q(hostname__in=hosts, org_id=org.id) else: filter_arg |= Q(Q(org_id__isnull=True) | Q(org_id=''), hostname__in=hosts) return Asset.objects.filter(filter_arg) @property def hardware_info(self): if self.cpu_count: return '{} Core {} {}'.format( self.cpu_vcpus or self.cpu_count * self.cpu_cores, self.memory, self.disk_total) else: return '' @property def is_connective(self): if not self.is_unixlike(): return True val = cache.get(ASSET_ADMIN_CONN_CACHE_KEY.format(self.hostname)) if val == 1: return True else: return False def to_json(self): info = { 'id': self.id, 'hostname': self.hostname, 'ip': self.ip, 'port': self.port, } if self.domain and self.domain.gateway_set.all(): info["gateways"] = [d.id for d in self.domain.gateway_set.all()] return info def get_auth_info(self): if self.admin_user: return { 'username': self.admin_user.username, 'password': self.admin_user.password, 'private_key': self.admin_user.private_key_file, 'become': self.admin_user.become_info, } def _to_secret_json(self): """ Ansible use it create inventory, First using asset user, otherwise using cluster admin user Todo: May be move to ops implements it """ data = self.to_json() if self.admin_user: admin_user = self.admin_user data.update({ 'username': admin_user.username, 'password': admin_user.password, 'private_key': admin_user.private_key_file, 'become': admin_user.become_info, 'groups': [node.value for node in self.nodes.all()], }) return data class Meta: unique_together = [('org_id', 'hostname')] verbose_name = _("Asset") @classmethod def generate_fake(cls, count=100): from random import seed, choice import forgery_py from django.db import IntegrityError seed() for i in range(count): ip = [str(i) for i in random.sample(range(255), 4)] asset = cls(ip='.'.join(ip), hostname=forgery_py.internet.user_name(True), admin_user=choice(AdminUser.objects.all()), port=22, created_by='Fake') try: asset.save() asset.system_users = [ choice(SystemUser.objects.all()) for i in range(3) ] logger.debug('Generate fake asset : %s' % asset.ip) except IntegrityError: print('Error continue') continue
class Node(OrgModelMixin, FamilyMixin, FullValueMixin, AssetsAmountMixin): id = models.UUIDField(default=uuid.uuid4, primary_key=True) key = models.CharField(unique=True, max_length=64, verbose_name=_("Key")) # '1:1:1:1' value = models.CharField(max_length=128, verbose_name=_("Value")) child_mark = models.IntegerField(default=0) date_create = models.DateTimeField(auto_now_add=True) objects = OrgManager.from_queryset(NodeQuerySet)() is_node = True _parents = None class Meta: verbose_name = _("Node") ordering = ['key'] def __str__(self): return self.full_value def __eq__(self, other): if not other: return False return self.id == other.id def __gt__(self, other): # if self.is_root() and not other.is_root(): # return False # elif not self.is_root() and other.is_root(): # return True self_key = [int(k) for k in self.key.split(':')] other_key = [int(k) for k in other.key.split(':')] self_parent_key = self_key[:-1] other_parent_key = other_key[:-1] if self_parent_key and other_parent_key and \ self_parent_key == other_parent_key: return self.value > other.value # if len(self_parent_key) < len(other_parent_key): # return True # elif len(self_parent_key) > len(other_parent_key): # return False return self_key > other_key def __lt__(self, other): return not self.__gt__(other) @property def name(self): return self.value @property def level(self): return len(self.key.split(':')) def get_next_child_key(self): mark = self.child_mark self.child_mark += 1 self.save() return "{}:{}".format(self.key, mark) def get_next_child_preset_name(self): name = ugettext("New node") values = [ child.value[child.value.rfind(' '):] for child in self.get_children() if child.value.startswith(name) ] values = [int(value) for value in values if value.strip().isdigit()] count = max(values) + 1 if values else 1 return '{} {}'.format(name, count) def create_child(self, value, _id=None): with transaction.atomic(): child_key = self.get_next_child_key() child = self.__class__.objects.create(id=_id, key=child_key, value=value) return child def get_assets(self): from .asset import Asset if self.is_default_node(): assets = Asset.objects.filter(Q(nodes__id=self.id) | Q(nodes__isnull=True)) else: assets = Asset.objects.filter(nodes__id=self.id) return assets.distinct() def get_valid_assets(self): return self.get_assets().valid() def get_all_assets(self): from .asset import Asset pattern = r'^{0}$|^{0}:'.format(self.key) args = [] kwargs = {} if self.is_root(): args.append(Q(nodes__key__regex=pattern) | Q(nodes=None)) else: kwargs['nodes__key__regex'] = pattern assets = Asset.objects.filter(*args, **kwargs).distinct() return assets def get_all_valid_assets(self): return self.get_all_assets().valid() def is_default_node(self): return self.is_root() and self.key == '1' def is_root(self): if self.key.isdigit(): return True else: return False @classmethod def create_root_node(cls): # 如果使用current_org 在set_current_org时会死循环 _current_org = get_current_org() with transaction.atomic(): if not _current_org.is_real(): return cls.default_node() set_current_org(Organization.root()) org_nodes_roots = cls.objects.filter(key__regex=r'^[0-9]+$') org_nodes_roots_keys = org_nodes_roots.values_list('key', flat=True) or ['1'] key = max([int(k) for k in org_nodes_roots_keys]) key = str(key + 1) if key != 0 else '2' set_current_org(_current_org) root = cls.objects.create(key=key, value=_current_org.name) return root @classmethod def root(cls): root = cls.objects.filter(key__regex=r'^[0-9]+$') if root: return root[0] else: return cls.create_root_node() @classmethod def default_node(cls): defaults = {'value': 'Default'} obj, created = cls.objects.get_or_create(defaults=defaults, key='1') return obj def as_tree_node(self): from common.tree import TreeNode name = '{} ({})'.format(self.value, self.assets_amount) data = { 'id': self.key, 'name': name, 'title': name, 'pId': self.parent_key, 'isParent': True, 'open': self.is_root(), 'meta': { 'node': { "id": self.id, "name": self.name, "value": self.value, "key": self.key, "assets_amount": self.assets_amount, }, 'type': 'node' } } tree_node = TreeNode(**data) return tree_node def delete(self, using=None, keep_parents=False): if self.children or self.get_assets(): return return super().delete(using=using, keep_parents=keep_parents) @classmethod def get_queryset(cls): from ..utils import NodeUtil util = NodeUtil() return sorted(util.nodes) @classmethod def generate_fake(cls, count=100): import random org = get_current_org() if not org or not org.is_real(): Organization.default().change_to() for i in range(count): node = random.choice(cls.objects.all()) node.create_child('Node {}'.format(i))