2018-07-12 16:00:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-03-12 08:24:38 +00:00
|
|
|
import uuid
|
|
|
|
from inspect import signature
|
|
|
|
from functools import wraps
|
2019-06-25 06:32:25 +00:00
|
|
|
from werkzeug.local import LocalProxy
|
2019-08-28 03:43:55 +00:00
|
|
|
from contextlib import contextmanager
|
2018-07-20 09:49:47 +00:00
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
from common.local import thread_local
|
2018-07-12 16:00:35 +00:00
|
|
|
from .models import Organization
|
|
|
|
|
|
|
|
|
|
|
|
def get_org_from_request(request):
|
2020-06-09 08:43:07 +00:00
|
|
|
# query中优先级最高
|
|
|
|
oid = request.GET.get("oid")
|
|
|
|
|
|
|
|
# 其次header
|
|
|
|
if not oid:
|
|
|
|
oid = request.META.get("HTTP_X_JMS_ORG")
|
|
|
|
# 其次cookie
|
|
|
|
if not oid:
|
|
|
|
oid = request.COOKIES.get('X-JMS-ORG')
|
|
|
|
# 其次session
|
2018-07-25 03:21:12 +00:00
|
|
|
if not oid:
|
2020-03-23 02:52:26 +00:00
|
|
|
oid = request.session.get("oid")
|
2019-09-27 06:18:51 +00:00
|
|
|
|
2019-06-25 12:32:12 +00:00
|
|
|
if not oid:
|
|
|
|
oid = Organization.DEFAULT_ID
|
2019-09-27 06:18:51 +00:00
|
|
|
if oid.lower() == "default":
|
|
|
|
oid = Organization.DEFAULT_ID
|
|
|
|
elif oid.lower() == "root":
|
|
|
|
oid = Organization.ROOT_ID
|
2021-03-03 03:20:40 +00:00
|
|
|
org = Organization.get_instance(oid, default=Organization.default())
|
2018-07-12 16:00:35 +00:00
|
|
|
return org
|
|
|
|
|
|
|
|
|
|
|
|
def set_current_org(org):
|
2020-03-12 08:24:38 +00:00
|
|
|
if isinstance(org, (str, uuid.UUID)):
|
2019-09-18 14:06:46 +00:00
|
|
|
org = Organization.get_instance(org)
|
2019-06-25 12:32:12 +00:00
|
|
|
setattr(thread_local, 'current_org_id', org.id)
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
|
|
|
2018-07-16 06:47:06 +00:00
|
|
|
def set_to_default_org():
|
|
|
|
set_current_org(Organization.default())
|
|
|
|
|
|
|
|
|
|
|
|
def set_to_root_org():
|
|
|
|
set_current_org(Organization.root())
|
2018-07-20 09:49:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _find(attr):
|
2019-06-25 06:32:25 +00:00
|
|
|
return getattr(thread_local, attr, None)
|
2018-07-20 09:49:47 +00:00
|
|
|
|
|
|
|
|
2018-07-20 10:42:01 +00:00
|
|
|
def get_current_org():
|
2019-06-25 12:32:12 +00:00
|
|
|
org_id = get_current_org_id()
|
2021-03-11 02:46:57 +00:00
|
|
|
if not org_id or org_id == Organization.ROOT_ID:
|
|
|
|
return Organization.root()
|
2021-03-03 03:20:40 +00:00
|
|
|
org = Organization.get_instance(org_id, default=Organization.root())
|
2019-06-25 06:32:25 +00:00
|
|
|
return org
|
2018-07-20 10:42:01 +00:00
|
|
|
|
2018-07-20 09:49:47 +00:00
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
def get_current_org_id():
|
2019-06-25 12:32:12 +00:00
|
|
|
org_id = _find('current_org_id')
|
|
|
|
return org_id
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_org_id_for_serializer():
|
|
|
|
org_id = get_current_org_id()
|
2019-05-21 08:24:01 +00:00
|
|
|
return org_id
|
|
|
|
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
@contextmanager
|
|
|
|
def tmp_to_root_org():
|
|
|
|
ori_org = get_current_org()
|
|
|
|
set_to_root_org()
|
|
|
|
yield
|
|
|
|
if ori_org is not None:
|
|
|
|
set_current_org(ori_org)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def tmp_to_org(org):
|
|
|
|
ori_org = get_current_org()
|
|
|
|
set_current_org(org)
|
|
|
|
yield
|
|
|
|
if ori_org is not None:
|
|
|
|
set_current_org(ori_org)
|
|
|
|
|
|
|
|
|
2022-10-25 11:31:13 +00:00
|
|
|
@contextmanager
|
|
|
|
def tmp_to_builtin_org(system=0, default=0):
|
|
|
|
if system:
|
|
|
|
org_id = Organization.SYSTEM_ID
|
|
|
|
elif default:
|
|
|
|
org_id = Organization.DEFAULT_ID
|
|
|
|
else:
|
|
|
|
raise ValueError("Must set system or default")
|
|
|
|
ori_org = get_current_org()
|
|
|
|
set_current_org(org_id)
|
|
|
|
yield
|
|
|
|
if ori_org is not None:
|
|
|
|
set_current_org(ori_org)
|
|
|
|
|
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
def filter_org_queryset(queryset):
|
2022-11-01 03:52:51 +00:00
|
|
|
locking_org = getattr(queryset.model, 'LOCKING_ORG', None)
|
2022-11-01 08:18:46 +00:00
|
|
|
org = get_current_org()
|
2022-11-01 03:52:51 +00:00
|
|
|
|
2022-11-01 08:18:46 +00:00
|
|
|
if locking_org:
|
|
|
|
kwargs = {'org_id': locking_org}
|
2022-11-01 09:04:44 +00:00
|
|
|
elif org is None or org.is_root():
|
2022-11-01 03:52:51 +00:00
|
|
|
kwargs = {}
|
|
|
|
else:
|
|
|
|
kwargs = {'org_id': org.id}
|
2019-10-18 07:05:45 +00:00
|
|
|
|
|
|
|
# lines = traceback.format_stack()
|
|
|
|
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>")
|
|
|
|
# for line in lines[-10:-1]:
|
|
|
|
# print(line)
|
|
|
|
# print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
|
|
|
queryset = queryset.filter(**kwargs)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def org_aware_func(org_arg_name):
|
|
|
|
"""
|
|
|
|
:param org_arg_name: 函数中包含org_id的对象是哪个参数
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
def decorate(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
sig = signature(func)
|
|
|
|
values = sig.bind(*args, **kwargs)
|
|
|
|
org_aware_resource = values.arguments.get(org_arg_name)
|
|
|
|
if not org_aware_resource:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
if hasattr(org_aware_resource, '__getitem__'):
|
|
|
|
org_aware_resource = org_aware_resource[0]
|
|
|
|
if not hasattr(org_aware_resource, "org_id"):
|
|
|
|
print("Error: {} not has org_id attr".format(org_aware_resource))
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
with tmp_to_org(org_aware_resource.org_id):
|
|
|
|
# print("Current org id: {}".format(org_aware_resource.org_id))
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorate
|
|
|
|
|
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
current_org = LocalProxy(get_current_org)
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
2021-02-08 06:59:20 +00:00
|
|
|
def ensure_in_real_or_default_org(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if not current_org or current_org.is_root():
|
|
|
|
raise ValueError('You must in a real or default org!')
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|