2018-07-12 16:00:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-07-20 09:49:47 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from common.utils import LocalProxy
|
2018-07-12 16:00:35 +00:00
|
|
|
from .models import Organization
|
|
|
|
|
|
|
|
try:
|
|
|
|
from threading import local
|
|
|
|
except ImportError:
|
|
|
|
from django.utils._threading_local import local
|
|
|
|
|
|
|
|
_thread_locals = local()
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
2018-07-12 16:00:35 +00:00
|
|
|
org = Organization.get_instance(oid)
|
|
|
|
return org
|
|
|
|
|
|
|
|
|
|
|
|
def set_current_org(org):
|
|
|
|
setattr(_thread_locals, 'current_org', org)
|
|
|
|
|
|
|
|
|
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):
|
2018-07-20 10:42:01 +00:00
|
|
|
return getattr(_thread_locals, attr, None)
|
2018-07-20 09:49:47 +00:00
|
|
|
|
|
|
|
|
2018-07-20 10:42:01 +00:00
|
|
|
def get_current_org():
|
|
|
|
return _find('current_org')
|
|
|
|
|
2018-07-20 09:49:47 +00:00
|
|
|
|
2018-07-20 10:42:01 +00:00
|
|
|
current_org = LocalProxy(partial(_find, 'current_org'))
|
|
|
|
current_user = LocalProxy(partial(_find, 'current_user'))
|
|
|
|
current_request = LocalProxy(partial(_find, 'current_request'))
|