perf(orgs): 默认组织改为实体组织,并支持全局组织 (#5617)

* perf(orgs): 默认组织改为实体组织

* perf: 添加获取当前组织信息的api

* perf: 资产列表在 root 组织下的表现

* fix: 修复 root 组织引起的问题

* perf: 优化OrgModelMixin save; org_root获取; org_roles获取; UserCanUseCurrentOrg权限类

Co-authored-by: ibuler <ibuler@qq.com>
Co-authored-by: Bai <bugatti_it@163.com>
This commit is contained in:
fit2bot
2021-03-02 14:57:48 +08:00
committed by GitHub
parent 51c9a89b1f
commit a56ac7b34e
19 changed files with 381 additions and 298 deletions

View File

@@ -40,7 +40,7 @@ def compute_parent_key(key):
class NodeQuerySet(models.QuerySet):
def delete(self):
raise NotImplementedError
#
class FamilyMixin:
__parents = None
@@ -446,8 +446,9 @@ class SomeNodesMixin:
@classmethod
def default_node(cls):
with tmp_to_org(Organization.default()):
defaults = {'value': cls.default_value}
default_org = Organization.default()
with tmp_to_org(default_org):
defaults = {'value': default_org.name}
try:
obj, created = cls.objects.get_or_create(
defaults=defaults, key=cls.default_key,
@@ -482,25 +483,34 @@ class SomeNodesMixin:
@classmethod
def create_org_root_node(cls):
# 如果使用current_org 在set_current_org时会死循环
ori_org = get_current_org()
with transaction.atomic():
if not ori_org.is_real():
return cls.default_node()
key = cls.get_next_org_root_node_key()
root = cls.objects.create(key=key, value=ori_org.name)
return root
@classmethod
def org_root(cls):
root = cls.objects.filter(parent_key='')\
.filter(key__regex=r'^[0-9]+$')\
.exclude(key__startswith='-')\
def org_root_nodes(cls):
nodes = cls.objects.filter(parent_key='') \
.filter(key__regex=r'^[0-9]+$') \
.exclude(key__startswith='-') \
.order_by('key')
if root:
return root[0]
return nodes
@classmethod
def org_root(cls):
org_roots = cls.org_root_nodes()
if org_roots:
return org_roots[0]
ori_org = get_current_org()
# 如果使用current_org 在set_current_org时会死循环
if ori_org.is_root():
root = cls.default_node()
elif ori_org.is_default():
root = cls.default_node()
else:
return cls.create_org_root_node()
root = cls.create_org_root_node()
return root
@classmethod
def initial_some_nodes(cls):
@@ -519,9 +529,6 @@ class SomeNodesMixin:
if not node_key1:
logger.info("Not found node that `key` = 1")
return
if not node_key1.org.is_real():
logger.info("Org is not real for node that `key` = 1")
return
with transaction.atomic():
with tmp_to_org(node_key1.org):