From 8420c0b22bbace60b1476a832c1679461bd8ffe2 Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Wed, 25 Dec 2024 17:53:11 +0800 Subject: [PATCH] perf: Add GatherAccountDetailField and update serializers --- .../automations/gather_account/filter.py | 3 +-- apps/accounts/const/automation.py | 19 ++++++++++++++++++- .../serializers/automations/gather_account.py | 8 +++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/accounts/automations/gather_account/filter.py b/apps/accounts/automations/gather_account/filter.py index 809e99f9d..eeb6fee29 100644 --- a/apps/accounts/automations/gather_account/filter.py +++ b/apps/accounts/automations/gather_account/filter.py @@ -59,7 +59,7 @@ class GatherAccountsFilter: 'groups': '', } detail = { - 'canlogin': user_info.get('canlogin'), + 'can_login': user_info.get('canlogin'), 'superuser': user_info.get('superuser'), } user['detail'] = detail @@ -87,7 +87,6 @@ class GatherAccountsFilter: 'is_disabled': user_info.get('is_disabled', ''), 'default_database_name': user_info.get('default_database_name', ''), } - print(user) user['detail'] = detail result[user['username']] = user return result diff --git a/apps/accounts/const/automation.py b/apps/accounts/const/automation.py index 9973d4a22..407f43e49 100644 --- a/apps/accounts/const/automation.py +++ b/apps/accounts/const/automation.py @@ -16,7 +16,7 @@ DEFAULT_PASSWORD_RULES = { __all__ = [ 'AutomationTypes', 'SecretStrategy', 'SSHKeyStrategy', 'Connectivity', 'DEFAULT_PASSWORD_LENGTH', 'DEFAULT_PASSWORD_RULES', 'TriggerChoice', - 'PushAccountActionChoice', 'AccountBackupType', 'ChangeSecretRecordStatusChoice', + 'PushAccountActionChoice', 'AccountBackupType', 'ChangeSecretRecordStatusChoice', 'GatherAccountDetailField' ] @@ -114,3 +114,20 @@ class ChangeSecretRecordStatusChoice(models.TextChoices): failed = 'failed', _('Failed') success = 'success', _('Success') pending = 'pending', _('Pending') + + +class GatherAccountDetailField(models.TextChoices): + can_login = 'can_login', _('Can login') + superuser = 'superuser', _('Superuser') + create_date = 'create_date', _('Create date') + is_disabled = 'is_disabled', _('Is disabled') + default_database_name = 'default_database_name', _('Default database name') + uid = 'uid', _('UID') + account_status = 'account_status', _('Account status') + default_tablespace = 'default_tablespace', _('Default tablespace') + roles = 'roles', _('Roles') + privileges = 'privileges', _('Privileges') + groups = 'groups', _('Groups') + sudoers = 'sudoers', 'sudoers' + authorized_keys = 'authorized_keys', _('Authorized keys') + db = 'db', _('DB') diff --git a/apps/accounts/serializers/automations/gather_account.py b/apps/accounts/serializers/automations/gather_account.py index 61c5dbfcd..795fa95b2 100644 --- a/apps/accounts/serializers/automations/gather_account.py +++ b/apps/accounts/serializers/automations/gather_account.py @@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ from rest_framework import serializers -from accounts.const import AutomationTypes +from accounts.const import AutomationTypes, GatherAccountDetailField from accounts.models import GatherAccountsAutomation from accounts.models import GatheredAccount from accounts.serializers.account.account import AccountAssetSerializer as _AccountAssetSerializer @@ -82,7 +82,9 @@ class GatheredAccountDetailsSerializer(serializers.Serializer): obj = get_object_or_404(GatheredAccount, pk=pk) details = obj.detail for key, value in details.items(): + field_dict = GatherAccountDetailField._member_map_ + label = field_dict[key].label if key in field_dict else key if isinstance(value, bool): - self.fields[key] = serializers.BooleanField(label=key, read_only=True) + self.fields[key] = serializers.BooleanField(label=label, read_only=True) else: - self.fields[key] = serializers.CharField(label=key, read_only=True) + self.fields[key] = serializers.CharField(label=label, read_only=True)