From fde87029162a2bfd0e48723553d885315f905ab2 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Fri, 1 Nov 2019 19:38:56 +0800 Subject: [PATCH 1/5] =?UTF-8?q?[Update]=20=E4=BF=AE=E5=A4=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20Default=20=E8=8A=82=E7=82=B9=E7=9A=84=20Bu?= =?UTF-8?q?g=EF=BC=88=E5=8E=9F=E5=9B=A0=EF=BC=9A=E5=9C=A8=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=BB=84=E7=BB=87=E4=B8=8B=20Default=20=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=BB=98=E8=AE=A4=E7=9A=84=20key=201=20=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=AD=98=E5=9C=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/node.py | 47 ++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index dd1f706f2..d8668f5f5 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -6,6 +6,7 @@ import time from django.db import models, transaction from django.db.models import Q +from django.db.utils import IntegrityError from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext from django.core.cache import cache @@ -336,6 +337,17 @@ class SomeNodesMixin: else: return False + @classmethod + def get_next_org_root_node_key(cls): + with tmp_to_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) + if not org_nodes_roots_keys: + org_nodes_roots_keys = ['1'] + max_key = max([int(k) for k in org_nodes_roots_keys]) + key = str(max_key + 1) if max_key != 0 else '2' + return key + @classmethod def create_org_root_node(cls): # 如果使用current_org 在set_current_org时会死循环 @@ -344,12 +356,7 @@ class SomeNodesMixin: if not ori_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) - if not org_nodes_roots_keys: - org_nodes_roots_keys = ['1'] - key = max([int(k) for k in org_nodes_roots_keys]) - key = str(key + 1) if key != 0 else '2' + key = cls.get_next_org_root_node_key() set_current_org(ori_org) root = cls.objects.create(key=key, value=ori_org.name) return root @@ -400,11 +407,37 @@ class SomeNodesMixin: @classmethod def initial_some_nodes(cls): - cls.default_node() + try: + cls.default_node() + except IntegrityError as e: + logger.error("Create default node failed") + cls.modify_other_org_root_node_key() + cls.default_node() cls.empty_node() cls.ungrouped_node() cls.favorite_node() + @classmethod + def modify_other_org_root_node_key(cls): + """ + 解决创建 default 节点失败的问题, + 因为在其他组织下存在 default 节点,故在 DEFAULT 组织下 get 不到 create 失败 + """ + logger.info("Modify other org root node key") + with transaction.atomic(): + with tmp_to_org(Organization.root()): + node = cls.objects.get(key='1') + with tmp_to_org(node.org): + for n in cls.objects.all(): + old_key = n.key + key_list = n.key.split(':') + org_root_node_new_key = cls.get_next_org_root_node_key() + key_list[0] = org_root_node_new_key + new_key = ':'.join(key_list) + n.key = new_key + n.save() + logger.info('Modify key ( {} > {} )'.format(old_key, new_key)) + class Node(OrgModelMixin, SomeNodesMixin, TreeMixin, FamilyMixin, FullValueMixin, NodeAssetsMixin): id = models.UUIDField(default=uuid.uuid4, primary_key=True) From a0be7333fb5b9ea404f900e3867daf4d1ae5daa4 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Fri, 1 Nov 2019 19:41:28 +0800 Subject: [PATCH 2/5] =?UTF-8?q?[Update]=20=E4=BF=AE=E5=A4=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20Default=20=E8=8A=82=E7=82=B9=E7=9A=84=20Bu?= =?UTF-8?q?g=EF=BC=88=E5=8E=9F=E5=9B=A0=EF=BC=9A=E5=9C=A8=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=BB=84=E7=BB=87=E4=B8=8B=20Default=20=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=BB=98=E8=AE=A4=E7=9A=84=20key=201=20=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=AD=98=E5=9C=A8=EF=BC=892?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/node.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index d8668f5f5..0417464b7 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -410,7 +410,7 @@ class SomeNodesMixin: try: cls.default_node() except IntegrityError as e: - logger.error("Create default node failed") + logger.error("Create default node failed: {}".format(e)) cls.modify_other_org_root_node_key() cls.default_node() cls.empty_node() From d7948425dff130f8f1f591e5dcac37380774a059 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Fri, 1 Nov 2019 19:45:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?[Update]=20=E4=BF=AE=E5=A4=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20Default=20=E8=8A=82=E7=82=B9=E7=9A=84=20Bu?= =?UTF-8?q?g=EF=BC=88=E5=8E=9F=E5=9B=A0=EF=BC=9A=E5=9C=A8=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=BB=84=E7=BB=87=E4=B8=8B=20Default=20=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=BB=98=E8=AE=A4=E7=9A=84=20key=201=20=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=AD=98=E5=9C=A8=EF=BC=893?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/node.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 0417464b7..b9f18b9ca 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -426,7 +426,13 @@ class SomeNodesMixin: logger.info("Modify other org root node key") with transaction.atomic(): with tmp_to_org(Organization.root()): - node = cls.objects.get(key='1') + node = cls.objects.filter(key='1').first() + if not node: + logger.info("Not found node that `key` = 1") + return + if not node.org.is_real(): + logger.info("Org is not real for node that `key` = 1") + return with tmp_to_org(node.org): for n in cls.objects.all(): old_key = n.key From ca178c534ead0212a6aad011a5efe6ad6d9d0a16 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Fri, 1 Nov 2019 20:02:34 +0800 Subject: [PATCH 4/5] =?UTF-8?q?[Update]=20=E4=BF=AE=E5=A4=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20Default=20=E8=8A=82=E7=82=B9=E7=9A=84=20Bu?= =?UTF-8?q?g=EF=BC=88=E5=8E=9F=E5=9B=A0=EF=BC=9A=E5=9C=A8=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=BB=84=E7=BB=87=E4=B8=8B=20Default=20=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=BB=98=E8=AE=A4=E7=9A=84=20key=201=20=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=AD=98=E5=9C=A8=EF=BC=894?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/node.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index b9f18b9ca..bb5369ec3 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -391,9 +391,16 @@ class SomeNodesMixin: def default_node(cls): with tmp_to_org(Organization.default()): defaults = {'value': cls.default_value} - obj, created = cls.objects.get_or_create( - defaults=defaults, key=cls.default_key, - ) + try: + obj, created = cls.objects.get_or_create( + defaults=defaults, key=cls.default_key, + ) + except IntegrityError as e: + logger.error("Create default node failed: {}".format(e)) + cls.modify_other_org_root_node_key() + obj, created = cls.objects.get_or_create( + defaults=defaults, key=cls.default_key, + ) return obj @classmethod @@ -407,12 +414,7 @@ class SomeNodesMixin: @classmethod def initial_some_nodes(cls): - try: - cls.default_node() - except IntegrityError as e: - logger.error("Create default node failed: {}".format(e)) - cls.modify_other_org_root_node_key() - cls.default_node() + cls.default_node() cls.empty_node() cls.ungrouped_node() cls.favorite_node() @@ -424,20 +426,22 @@ class SomeNodesMixin: 因为在其他组织下存在 default 节点,故在 DEFAULT 组织下 get 不到 create 失败 """ logger.info("Modify other org root node key") - with transaction.atomic(): - with tmp_to_org(Organization.root()): - node = cls.objects.filter(key='1').first() - if not node: + + with tmp_to_org(Organization.root()): + node_key1 = cls.objects.filter(key='1').first() + if not node_key1: logger.info("Not found node that `key` = 1") return - if not node.org.is_real(): + if not node_key1.org.is_real(): logger.info("Org is not real for node that `key` = 1") return - with tmp_to_org(node.org): + + with transaction.atomic(): + with tmp_to_org(node_key1.org): + org_root_node_new_key = cls.get_next_org_root_node_key() for n in cls.objects.all(): old_key = n.key key_list = n.key.split(':') - org_root_node_new_key = cls.get_next_org_root_node_key() key_list[0] = org_root_node_new_key new_key = ':'.join(key_list) n.key = new_key From b51ad05a4be7efd00465280c7b2e4ee259e98106 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Fri, 1 Nov 2019 20:18:36 +0800 Subject: [PATCH 5/5] =?UTF-8?q?[Update]=20=E4=BF=AE=E5=A4=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20Default=20=E8=8A=82=E7=82=B9=E7=9A=84=20Bu?= =?UTF-8?q?g=EF=BC=88=E5=8E=9F=E5=9B=A0=EF=BC=9A=E5=9C=A8=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=BB=84=E7=BB=87=E4=B8=8B=20Default=20=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=BB=98=E8=AE=A4=E7=9A=84=20key=201=20=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=AD=98=E5=9C=A8=EF=BC=895?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/node.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index bb5369ec3..e6ceb1f66 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -355,9 +355,7 @@ class SomeNodesMixin: with transaction.atomic(): if not ori_org.is_real(): return cls.default_node() - set_current_org(Organization.root()) key = cls.get_next_org_root_node_key() - set_current_org(ori_org) root = cls.objects.create(key=key, value=ori_org.name) return root