[Update] 修改get_all_assets

pull/1602/head
ibuler 6 years ago
parent a48fb9de8d
commit 7e3cf908a1

@ -110,16 +110,22 @@ class Node(OrgModelMixin):
def get_all_assets(self): def get_all_assets(self):
from .asset import Asset from .asset import Asset
if self.is_root(): pattern = r'^{0}$|^{0}:'.format(self.key)
assets = Asset.objects.all() args = []
kwargs = {}
if self.is_default_node():
args.append(Q(nodes__key__regex=pattern) | Q(nodes__key=None))
else: else:
pattern = r'^{0}$|^{0}:'.format(self.key) kwargs['nodes__key__regex'] = pattern
assets = Asset.objects.filter(nodes__key__regex=pattern) assets = Asset.objects.filter(*args, **kwargs)
return assets return assets
def get_all_valid_assets(self): def get_all_valid_assets(self):
return self.get_all_assets().valid() return self.get_all_assets().valid()
def is_default_node(self):
return self.is_root() and self.key == '0'
def is_root(self): def is_root(self):
if self.key.isdigit(): if self.key.isdigit():
return True return True
@ -129,7 +135,7 @@ class Node(OrgModelMixin):
@property @property
def parent(self): def parent(self):
if self.is_root(): if self.is_root():
return self.__class__.root() return self
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)
@ -139,17 +145,17 @@ class Node(OrgModelMixin):
@parent.setter @parent.setter
def parent(self, parent): def parent(self, parent):
if self.is_node: if not self.is_node:
children = self.get_all_children() self.key = parent.key + ':fake'
old_key = self.key return
with transaction.atomic(): children = self.get_all_children()
self.key = parent.get_next_child_key() old_key = self.key
for child in children: with transaction.atomic():
child.key = child.key.replace(old_key, self.key, 1) self.key = parent.get_next_child_key()
child.save() for child in children:
self.save() child.key = child.key.replace(old_key, self.key, 1)
else: child.save()
self.key = parent.key+':fake' self.save()
def get_ancestor(self, with_self=False): def get_ancestor(self, with_self=False):
if self.is_root(): if self.is_root():
@ -175,7 +181,7 @@ class Node(OrgModelMixin):
set_current_org(Organization.root()) set_current_org(Organization.root())
org_nodes_roots = cls.objects.filter(key__regex=r'^[0-9]+$') org_nodes_roots = cls.objects.filter(key__regex=r'^[0-9]+$')
org_nodes_roots_keys = org_nodes_roots.values_list('key', flat=True) org_nodes_roots_keys = org_nodes_roots.values_list('key', flat=True)
max_value = max([int(k) for k in org_nodes_roots_keys]) if org_nodes_roots_keys else 0 max_value = max([int(k) for k in org_nodes_roots_keys]) if org_nodes_roots_keys else -1
set_current_org(_current_org) set_current_org(_current_org)
root = cls.objects.create(key=str(max_value+1), value=_current_org.name) root = cls.objects.create(key=str(max_value+1), value=_current_org.name)
return root return root

Loading…
Cancel
Save