From b3f359d47b44115f2c84a55125e8e2e9c8cc9b78 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 12 Jan 2021 12:59:05 +0800 Subject: [PATCH] =?UTF-8?q?perf(assets):=20=E4=BC=98=E5=8C=96=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=94=9F=E6=88=90=E5=AD=90=E8=8A=82=E7=82=B9key?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91=20(#5405)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf(assets): 优化节点生成子节点key生成逻辑 * perf(assets): 优化写法 * perf(assets): 优化获取子节点的mark * perf(assets): 再优化一波 * perf(asset): 继续优化这里的写法 Co-authored-by: ibuler --- apps/assets/models/node.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 710586e6f..58e267f70 100644 --- a/apps/assets/models/node.py +++ b/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")