2018-07-12 16:00:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-10-18 07:05:45 +00:00
|
|
|
import traceback
|
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):
|
|
|
|
oid = request.session.get("oid")
|
2018-07-25 03:21:12 +00:00
|
|
|
if not oid:
|
|
|
|
oid = request.META.get("HTTP_X_JMS_ORG")
|
2019-09-27 06:18:51 +00:00
|
|
|
|
|
|
|
request_params_oid = request.GET.get("oid")
|
|
|
|
if request_params_oid:
|
|
|
|
oid = request.GET.get("oid")
|
|
|
|
|
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
|
2018-07-12 16:00:35 +00:00
|
|
|
org = Organization.get_instance(oid)
|
|
|
|
return org
|
|
|
|
|
|
|
|
|
|
|
|
def set_current_org(org):
|
2019-09-18 14:06:46 +00:00
|
|
|
if isinstance(org, str):
|
|
|
|
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()
|
|
|
|
if org_id is None:
|
|
|
|
return None
|
2019-06-25 06:32:25 +00:00
|
|
|
org = Organization.get_instance(org_id)
|
|
|
|
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()
|
|
|
|
if org_id == Organization.DEFAULT_ID:
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
def get_org_filters():
|
|
|
|
kwargs = {}
|
|
|
|
|
|
|
|
_current_org = get_current_org()
|
|
|
|
if _current_org is None:
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
if _current_org.is_real():
|
|
|
|
kwargs['org_id'] = _current_org.id
|
|
|
|
elif _current_org.is_default():
|
|
|
|
kwargs["org_id"] = ''
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
|
|
|
def filter_org_queryset(queryset):
|
|
|
|
kwargs = get_org_filters()
|
|
|
|
|
|
|
|
#
|
|
|
|
# lines = traceback.format_stack()
|
|
|
|
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>")
|
|
|
|
# for line in lines[-10:-1]:
|
|
|
|
# print(line)
|
|
|
|
# print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
|
|
|
queryset = queryset.filter(**kwargs)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
current_org = LocalProxy(get_current_org)
|