Browse Source

perf(assets): 优化节点生成子节点key生成逻辑 (#5405)

* perf(assets): 优化节点生成子节点key生成逻辑

* perf(assets): 优化写法

* perf(assets): 优化获取子节点的mark

* perf(assets): 再优化一波

* perf(asset): 继续优化这里的写法

Co-authored-by: ibuler <ibuler@qq.com>
pull/5406/head
fit2bot 4 years ago committed by GitHub
parent
commit
b3f359d47b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      apps/assets/models/node.py

18
apps/assets/models/node.py

@ -38,6 +38,7 @@ class FamilyMixin:
__children = None
__all_children = None
is_node = True
child_mark: int
@staticmethod
def clean_children_keys(nodes_keys):
@ -121,11 +122,22 @@ class FamilyMixin:
created = True
return child, created
def get_valid_child_mark(self):
key = "{}:{}".format(self.key, self.child_mark)
if not self.__class__.objects.filter(key=key).exists():
return self.child_mark
children_keys = self.get_children().values_list('key', flat=True)
children_keys_last = [key.split(':')[-1] for key in children_keys]
children_keys_last = [int(k) for k in children_keys_last if k.strip().isdigit()]
max_key_last = max(children_keys_last) if children_keys_last else 1
return max_key_last + 1
def get_next_child_key(self):
mark = self.child_mark
self.child_mark += 1
child_mark = self.get_valid_child_mark()
key = "{}:{}".format(self.key, child_mark)
self.child_mark = child_mark + 1
self.save()
return "{}:{}".format(self.key, mark)
return key
def get_next_child_preset_name(self):
name = ugettext("New node")

Loading…
Cancel
Save