From 32081ea6ece8333747a4e06b433d8f5711944946 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 27 Feb 2023 14:20:58 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=B7=BB=E5=8A=A0=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8D=E7=9A=84=E6=8E=A8=E8=8D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit perf: 修改账号推荐 --- apps/accounts/api/account/account.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/accounts/api/account/account.py b/apps/accounts/api/account/account.py index 64dc745f8..a594493a6 100644 --- a/apps/accounts/api/account/account.py +++ b/apps/accounts/api/account/account.py @@ -6,7 +6,7 @@ from rest_framework.response import Response from accounts import serializers from accounts.filters import AccountFilterSet from accounts.models import Account -from assets.models import Asset +from assets.models import Asset, Node from common.permissions import UserConfirmation, ConfirmType from common.views.mixins import RecordViewLogMixin from orgs.mixins.api import OrgBulkModelViewSet @@ -28,6 +28,7 @@ class AccountViewSet(OrgBulkModelViewSet): rbac_perms = { 'partial_update': ['accounts.change_account'], 'su_from_accounts': 'accounts.view_account', + 'username_suggestions': 'accounts.view_account', } @action(methods=['get'], detail=False, url_path='su-from-accounts') @@ -47,6 +48,29 @@ class AccountViewSet(OrgBulkModelViewSet): serializer = serializers.AccountSerializer(accounts, many=True) return Response(data=serializer.data) + @action(methods=['get'], detail=False, url_path='username-suggestions') + def username_suggestions(self, request, *args, **kwargs): + asset_ids = request.query_params.get('assets') + node_keys = request.query_params.get('keys') + username = request.query_params.get('username') + + assets = Asset.objects.all() + if asset_ids: + assets = assets.filter(id__in=asset_ids.split(',')) + if node_keys: + patten = Node.get_node_all_children_key_pattern(node_keys.split(',')) + assets = assets.filter(nodes__key__regex=patten) + + accounts = Account.objects.filter(asset__in=assets) + if username: + accounts = accounts.filter(username__icontains=username) + usernames = list(accounts.values_list('username', flat=True).distinct()[:10]) + usernames.sort() + common = [i for i in usernames if i in usernames if i.lower() in ['root', 'admin', 'administrator']] + others = [i for i in usernames if i not in common] + usernames = common + others + return Response(data=usernames) + class AccountSecretsViewSet(RecordViewLogMixin, AccountViewSet): """