perf: 修复标签搜索

pull/12348/head
ibuler 2023-12-15 18:30:21 +08:00 committed by Eric_Lee
parent 714c44fbf4
commit f7fee0f430
4 changed files with 26 additions and 9 deletions

View File

@ -192,9 +192,9 @@ class LabelFilterBackend(filters.BaseFilterBackend):
kwargs = {}
if ':' in label_id:
k, v = label_id.split(':', 1)
kwargs['label__name'] = k
kwargs['label__name'] = k.strip()
if v != '*':
kwargs['label__value'] = v
kwargs['label__value'] = v.strip()
else:
kwargs['label_id'] = label_id

View File

@ -122,8 +122,12 @@ class LabelRelatedField(serializers.RelatedField):
from labels.models import LabeledResource, Label
if data is None:
return data
k, v = data.split(":", 1)
label, __ = Label.objects.get_or_create(name=k, value=v, defaults={'name': k, 'value': v})
if isinstance(data, dict):
pk = data.get("id") or data.get("pk")
label = Label.objects.get(pk=pk)
else:
k, v = data.split(":", 1)
label, __ = Label.objects.get_or_create(name=k, value=v, defaults={'name': k, 'value': v})
return LabeledResource(label=label)

View File

@ -3,7 +3,6 @@ from rest_framework.decorators import action
from rest_framework.response import Response
from common.api.generic import JMSModelViewSet
from common.utils import is_true
from orgs.mixins.api import OrgBulkModelViewSet
from orgs.mixins.models import OrgModelMixin
from orgs.utils import current_org
@ -55,6 +54,8 @@ class ContentTypeViewSet(JMSModelViewSet):
if issubclass(model, OrgModelMixin):
queryset = model.objects.filter(org_id=current_org.id)
elif hasattr(model, 'get_queryset'):
queryset = model.get_queryset()
else:
queryset = model.objects.all()
@ -78,14 +79,19 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet):
label = get_object_or_404(Label, pk=label_pk)
content_type = get_object_or_404(ContentType, id=res_type)
bound = self.request.query_params.get('bound', '1')
res_ids = LabeledResource.objects.filter(res_type=content_type, label=label) \
res_ids = LabeledResource.objects \
.filter(res_type=content_type, label=label) \
.values_list('res_id', flat=True)
res_ids = set(res_ids)
model = content_type.model_class()
if is_true(bound):
queryset = model.objects.filter(id__in=list(res_ids))
if hasattr(model, 'get_queryset'):
queryset = model.get_queryset()
else:
queryset = model.objects.exclude(id__in=list(res_ids))
queryset = model.objects.all()
if bound == '1':
queryset = queryset.filter(id__in=list(res_ids))
elif bound == '0':
queryset = queryset.exclude(id__in=list(res_ids))
keyword = self.request.query_params.get('search')
if keyword:
queryset = content_type.filter_queryset(queryset, keyword)

View File

@ -862,6 +862,13 @@ class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, LabeledMixin, JSONFilterM
def __str__(self):
return '{0.name}({0.username})'.format(self)
@classmethod
def get_queryset(cls):
queryset = cls.objects.all()
if not current_org.is_root():
queryset = current_org.get_members()
return queryset
@property
def secret_key(self):
instance = self.preferences.filter(name='secret_key').first()