perf: 优化drf filter, 修改terminal filter

perf: 去掉debug

perf: 去掉debug
pull/6967/head
ibuler 2021-09-27 11:28:29 +08:00 committed by Jiangjie.Bai
parent 456116938d
commit 0b593f4555
5 changed files with 22 additions and 10 deletions

View File

@ -4,6 +4,7 @@ from __future__ import unicode_literals
from collections import OrderedDict
import datetime
from itertools import chain
from django.core.exceptions import PermissionDenied
from django.http import Http404
@ -101,7 +102,13 @@ class SimpleMetadataWithFilters(SimpleMetadata):
elif hasattr(view, 'get_filterset_fields'):
fields = view.get_filterset_fields(request)
elif hasattr(view, 'filterset_class'):
fields = view.filterset_class.Meta.fields
fields = view.filterset_class.Meta.fields + \
list(view.filterset_class.declared_filters.keys())
if hasattr(view, 'custom_filter_fields'):
# 不能写 fields += view.custom_filter_fields
# 会改变 view 的 filter_fields
fields = list(fields) + view.custom_filter_fields
if isinstance(fields, dict):
fields = list(fields.keys())

View File

@ -112,7 +112,8 @@ class ExtraFilterFieldsMixin:
backends = list(chain(
self.filter_backends,
self.default_added_filters,
self.extra_filter_backends))
self.extra_filter_backends
))
return backends
def filter_queryset(self, queryset):

View File

@ -2491,7 +2491,7 @@ msgstr "账户是否有效"
#: perms/serializers/asset/permission.py:67 users/serializers/user.py:29
#: users/serializers/user.py:79
msgid "Is expired"
msgstr "是否过期"
msgstr "过期"
#: perms/serializers/application/permission.py:40
#: perms/serializers/asset/permission.py:70 users/serializers/group.py:34
@ -5285,15 +5285,15 @@ msgstr "密码策略"
#: users/serializers/user.py:25
msgid "MFA enabled"
msgstr "是否开启多因子认证"
msgstr "MFA"
#: users/serializers/user.py:26
msgid "MFA force enabled"
msgstr "强制启用多因子认证"
msgstr "强制 MFA"
#: users/serializers/user.py:27
msgid "MFA level display"
msgstr "多因子认证等级名称"
msgstr "MFA 等级名称"
#: users/serializers/user.py:28
msgid "Login blocked"

View File

@ -8,6 +8,7 @@ from rest_framework import generics
from rest_framework.views import APIView, Response
from rest_framework import status
from django.conf import settings
from django_filters import rest_framework as filters
from django.utils.translation import gettext_lazy as _
from common.exceptions import JMSException
@ -30,6 +31,7 @@ class TerminalViewSet(JMSBulkModelViewSet):
serializer_class = serializers.TerminalSerializer
permission_classes = (IsSuperUser,)
filterset_fields = ['name', 'remote_addr', 'type']
custom_filter_fields = ['status']
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
@ -75,10 +77,10 @@ class TerminalViewSet(JMSBulkModelViewSet):
def filter_queryset(self, queryset):
queryset = super().filter_queryset(queryset)
status = self.request.query_params.get('status')
if not status:
s = self.request.query_params.get('status')
if not s:
return queryset
filtered_queryset_id = [str(q.id) for q in queryset if q.status == status]
filtered_queryset_id = [str(q.id) for q in queryset if q.latest_status == s]
queryset = queryset.filter(id__in=filtered_queryset_id)
return queryset

View File

@ -5,6 +5,7 @@ from common.drf.serializers import BulkModelSerializer, AdaptedBulkListSerialize
from common.utils import is_uuid
from users.serializers import ServiceAccountSerializer
from common.utils import get_request_ip
from .. import const
from ..models import (
Terminal, Status, Session, Task, CommandStorage, ReplayStorage
@ -37,7 +38,8 @@ class StatusSerializer(serializers.ModelSerializer):
class TerminalSerializer(BulkModelSerializer):
session_online = serializers.ReadOnlyField(source='get_online_session_count')
is_alive = serializers.BooleanField(read_only=True)
status = serializers.CharField(read_only=True, source='latest_status')
is_active = serializers.BooleanField(read_only=True, label='Is active')
status = serializers.ChoiceField(read_only=True, choices=const.ComponentStatusChoices.choices, source='latest_status')
status_display = serializers.CharField(read_only=True, source='latest_status_display')
stat = StatusSerializer(read_only=True, source='latest_stat')