Commit 237d51cf authored by ibuler's avatar ibuler

[Update] 拆分filter org

parent 31280a38
...@@ -5,7 +5,7 @@ from rest_framework.viewsets import ModelViewSet ...@@ -5,7 +5,7 @@ from rest_framework.viewsets import ModelViewSet
from rest_framework_bulk import BulkModelViewSet from rest_framework_bulk import BulkModelViewSet
from common.mixins import CommonApiMixin from common.mixins import CommonApiMixin
from ..utils import set_to_root_org from ..utils import set_to_root_org, filter_org_queryset
from ..models import Organization from ..models import Organization
__all__ = [ __all__ = [
...@@ -22,7 +22,9 @@ class RootOrgViewMixin: ...@@ -22,7 +22,9 @@ class RootOrgViewMixin:
class OrgQuerySetMixin: class OrgQuerySetMixin:
def get_queryset(self): def get_queryset(self):
queryset = super().get_queryset().all() queryset = super().get_queryset()
queryset = filter_org_queryset(queryset)
if hasattr(self, 'swagger_fake_view'): if hasattr(self, 'swagger_fake_view'):
return queryset[:1] return queryset[:1]
if hasattr(self, 'action') and self.action == 'list' and \ if hasattr(self, 'action') and self.action == 'list' and \
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import traceback
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
...@@ -9,6 +8,7 @@ from django.core.exceptions import ValidationError ...@@ -9,6 +8,7 @@ from django.core.exceptions import ValidationError
from common.utils import get_logger from common.utils import get_logger
from ..utils import ( from ..utils import (
set_current_org, get_current_org, current_org, set_current_org, get_current_org, current_org,
filter_org_queryset,
) )
from ..models import Organization from ..models import Organization
...@@ -23,24 +23,7 @@ class OrgManager(models.Manager): ...@@ -23,24 +23,7 @@ class OrgManager(models.Manager):
def get_queryset(self): def get_queryset(self):
queryset = super(OrgManager, self).get_queryset() queryset = super(OrgManager, self).get_queryset()
kwargs = {} return filter_org_queryset(queryset)
_current_org = get_current_org()
if _current_org is None:
kwargs['id'] = None
elif _current_org.is_real():
kwargs['org_id'] = _current_org.id
elif _current_org.is_default():
queryset = queryset.filter(org_id="")
#
# lines = traceback.format_stack()
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>")
# for line in lines[-10:-1]:
# print(line)
# print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
queryset = queryset.filter(**kwargs)
return queryset
def all(self): def all(self):
if not current_org: if not current_org:
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import traceback
from werkzeug.local import LocalProxy from werkzeug.local import LocalProxy
from contextlib import contextmanager from contextlib import contextmanager
...@@ -82,4 +83,25 @@ def tmp_to_org(org): ...@@ -82,4 +83,25 @@ def tmp_to_org(org):
set_current_org(ori_org) set_current_org(ori_org)
def filter_org_queryset(queryset):
kwargs = {}
_current_org = get_current_org()
if _current_org is None:
return queryset.none()
if _current_org.is_real():
kwargs['org_id'] = _current_org.id
elif _current_org.is_default():
kwargs["org_id"] = ''
#
# lines = traceback.format_stack()
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>")
# for line in lines[-10:-1]:
# print(line)
# print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
queryset = queryset.filter(**kwargs)
return queryset
current_org = LocalProxy(get_current_org) current_org = LocalProxy(get_current_org)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment