mirror of https://github.com/jumpserver/jumpserver
[Update] 修改树形结构
parent
bff3868b8f
commit
a96bda8ca9
|
@ -65,15 +65,25 @@ class Node(models.Model):
|
||||||
key__regex=r'^{}:[0-9]+$'.format(self.key)
|
key__regex=r'^{}:[0-9]+$'.format(self.key)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_children_with_self(self):
|
||||||
|
return self.__class__.objects.filter(
|
||||||
|
key__regex=r'^{0}$|^{0}:[0-9]+$'.format(self.key)
|
||||||
|
)
|
||||||
|
|
||||||
def get_all_children(self):
|
def get_all_children(self):
|
||||||
return self.__class__.objects.filter(
|
return self.__class__.objects.filter(
|
||||||
key__startswith='{}:'.format(self.key)
|
key__startswith='{}:'.format(self.key)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_all_children_with_self(self):
|
||||||
|
return self.__class__.objects.filter(
|
||||||
|
key__regex=r'^{0}$|^{0}:'.format(self.key)
|
||||||
|
)
|
||||||
|
|
||||||
def get_family(self):
|
def get_family(self):
|
||||||
children = list(self.get_all_children())
|
ancestor = self.ancestor
|
||||||
children.append(self)
|
children = self.get_all_children()
|
||||||
return children
|
return [*tuple(ancestor), self, *tuple(children)]
|
||||||
|
|
||||||
def get_assets(self):
|
def get_assets(self):
|
||||||
from .asset import Asset
|
from .asset import Asset
|
||||||
|
@ -88,7 +98,7 @@ class Node(models.Model):
|
||||||
if self.is_root():
|
if self.is_root():
|
||||||
assets = Asset.objects.all()
|
assets = Asset.objects.all()
|
||||||
else:
|
else:
|
||||||
nodes = self.get_family()
|
nodes = self.get_all_children_with_self()
|
||||||
assets = Asset.objects.filter(nodes__in=nodes).distinct()
|
assets = Asset.objects.filter(nodes__in=nodes).distinct()
|
||||||
return assets
|
return assets
|
||||||
|
|
||||||
|
@ -108,18 +118,15 @@ class Node(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent(self):
|
def parent(self):
|
||||||
if self.key == "0":
|
if self.key == "0" or not self.key.startswith("0"):
|
||||||
return self.__class__.root()
|
|
||||||
elif not self.key.startswith("0"):
|
|
||||||
return self.__class__.root()
|
return self.__class__.root()
|
||||||
|
|
||||||
parent_key = ":".join(self.key.split(":")[:-1])
|
parent_key = ":".join(self.key.split(":")[:-1])
|
||||||
try:
|
try:
|
||||||
parent = self.__class__.objects.get(key=parent_key)
|
parent = self.__class__.objects.get(key=parent_key)
|
||||||
|
return parent
|
||||||
except Node.DoesNotExist:
|
except Node.DoesNotExist:
|
||||||
return self.__class__.root()
|
return self.__class__.root()
|
||||||
else:
|
|
||||||
return parent
|
|
||||||
|
|
||||||
@parent.setter
|
@parent.setter
|
||||||
def parent(self, parent):
|
def parent(self, parent):
|
||||||
|
@ -127,14 +134,20 @@ class Node(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ancestor(self):
|
def ancestor(self):
|
||||||
if self.parent == self.__class__.root():
|
_key = self.key.split(':')
|
||||||
|
ancestor_keys = []
|
||||||
|
|
||||||
|
if self.is_root():
|
||||||
return [self.__class__.root()]
|
return [self.__class__.root()]
|
||||||
else:
|
|
||||||
return [self.parent, *tuple(self.parent.ancestor)]
|
for i in range(len(_key)-1):
|
||||||
|
_key.pop()
|
||||||
|
ancestor_keys.append(':'.join(_key))
|
||||||
|
return self.__class__.objects.filter(key__in=ancestor_keys)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ancestor_with_node(self):
|
def ancestor_with_self(self):
|
||||||
ancestor = self.ancestor
|
ancestor = list(self.ancestor)
|
||||||
ancestor.insert(0, self)
|
ancestor.insert(0, self)
|
||||||
return ancestor
|
return ancestor
|
||||||
|
|
||||||
|
|
|
@ -229,7 +229,11 @@ LOGGING = {
|
||||||
'django_auth_ldap': {
|
'django_auth_ldap': {
|
||||||
'handlers': ['console', 'ansible_logs'],
|
'handlers': ['console', 'ansible_logs'],
|
||||||
'level': "INFO",
|
'level': "INFO",
|
||||||
}
|
},
|
||||||
|
# 'django.db': {
|
||||||
|
# 'handlers': ['console', 'file'],
|
||||||
|
# 'level': 'DEBUG'
|
||||||
|
# }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue