mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-19 10:26:27 +00:00
perf: 优化标签绑定,仅绑定到资产上
This commit is contained in:
@@ -73,7 +73,7 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet):
|
||||
queryset = model.objects.all()
|
||||
if bound == '1':
|
||||
queryset = queryset.filter(id__in=list(res_ids))
|
||||
elif bound == '0':
|
||||
else:
|
||||
queryset = queryset.exclude(id__in=list(res_ids))
|
||||
keyword = self.request.query_params.get('search')
|
||||
if keyword:
|
||||
@@ -90,9 +90,10 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet):
|
||||
LabeledResource.objects \
|
||||
.filter(res_type=content_type, label=label) \
|
||||
.exclude(res_id__in=res_ids).delete()
|
||||
resources = []
|
||||
for res_id in res_ids:
|
||||
resources.append(LabeledResource(res_type=content_type, res_id=res_id, label=label, org_id=current_org.id))
|
||||
resources = [
|
||||
LabeledResource(res_type=content_type, res_id=res_id, label=label, org_id=current_org.id)
|
||||
for res_id in res_ids
|
||||
]
|
||||
LabeledResource.objects.bulk_create(resources, ignore_conflicts=True)
|
||||
return Response({"total": len(res_ids)})
|
||||
|
||||
@@ -129,15 +130,22 @@ class LabeledResourceViewSet(OrgBulkModelViewSet):
|
||||
}
|
||||
ordering_fields = ('res_type', 'date_created')
|
||||
|
||||
# Todo: 这里需要优化,查询 sql 太多
|
||||
def filter_search(self, queryset):
|
||||
keyword = self.request.query_params.get('search')
|
||||
if not keyword:
|
||||
return queryset
|
||||
keyword = keyword.strip().lower()
|
||||
matched = []
|
||||
for instance in queryset:
|
||||
if keyword.lower() in str(instance.resource).lower():
|
||||
matched.append(instance.id)
|
||||
offset = 0
|
||||
limit = 10000
|
||||
while True:
|
||||
page = queryset[offset:offset + limit]
|
||||
if not page:
|
||||
break
|
||||
offset += limit
|
||||
for instance in page:
|
||||
if keyword in str(instance.resource).lower():
|
||||
matched.append(instance.id)
|
||||
return queryset.filter(id__in=matched)
|
||||
|
||||
def get_queryset(self):
|
||||
|
@@ -1,5 +1,6 @@
|
||||
from django.contrib.contenttypes.fields import GenericRelation
|
||||
from django.db import models
|
||||
from django.db.models import OneToOneField
|
||||
|
||||
from .models import LabeledResource
|
||||
|
||||
@@ -12,10 +13,25 @@ class LabeledMixin(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@classmethod
|
||||
def label_model(cls):
|
||||
pk_field = cls._meta.pk
|
||||
model = cls
|
||||
if isinstance(pk_field, OneToOneField):
|
||||
model = pk_field.related_model
|
||||
return model
|
||||
|
||||
@property
|
||||
def real(self):
|
||||
pk_field = self._meta.pk
|
||||
if isinstance(pk_field, OneToOneField):
|
||||
return getattr(self, pk_field.name)
|
||||
return self
|
||||
|
||||
@property
|
||||
def labels(self):
|
||||
return self._labels
|
||||
return self.real._labels
|
||||
|
||||
@labels.setter
|
||||
def labels(self, value):
|
||||
self._labels.set(value, bulk=False)
|
||||
self.real._labels.set(value, bulk=False)
|
||||
|
Reference in New Issue
Block a user