fix: Cannot set original org when exception occurs

This commit is contained in:
wangruidong
2025-02-08 11:14:22 +08:00
committed by Bryan
parent 25603e4758
commit 1d36934111

View File

@@ -97,8 +97,10 @@ def get_current_org_id_for_serializer():
@contextmanager
def tmp_to_root_org():
ori_org = get_current_org()
try:
set_to_root_org()
yield
finally:
if ori_org is not None:
set_current_org(ori_org)
@@ -106,9 +108,11 @@ def tmp_to_root_org():
@contextmanager
def tmp_to_org(org):
ori_org = get_current_org()
try:
if org:
set_current_org(org)
yield
finally:
if ori_org is not None:
set_current_org(ori_org)
@@ -121,9 +125,11 @@ def tmp_to_builtin_org(system=0, default=0):
org_id = Organization.DEFAULT_ID
else:
raise ValueError("Must set system or default")
try:
ori_org = get_current_org()
set_current_org(org_id)
yield
finally:
if ori_org is not None:
set_current_org(ori_org)