diff --git a/apps/accounts/api/account/account.py b/apps/accounts/api/account/account.py index 17938358c..73540126b 100644 --- a/apps/accounts/api/account/account.py +++ b/apps/accounts/api/account/account.py @@ -74,5 +74,17 @@ class AccountHistoriesSecretAPI(RecordViewLogMixin, ListAPIView): 'list': 'accounts.view_accountsecret', } + def get_object(self): + return get_object_or_404(Account, pk=self.kwargs.get('pk')) + def get_queryset(self): - return self.model.objects.filter(id=self.kwargs.get('pk')) + account = self.get_object() + histories = account.history.all() + last_history = account.history.first() + if not last_history: + return histories + + if account.secret == last_history.secret \ + and account.secret_type == last_history.secret_type: + histories = histories.exclude(history_id=last_history.history_id) + return histories diff --git a/apps/accounts/models/account.py b/apps/accounts/models/account.py index 00934b759..008318c7e 100644 --- a/apps/accounts/models/account.py +++ b/apps/accounts/models/account.py @@ -90,6 +90,10 @@ class Account(AbsConnectivity, BaseAccount): """ @INPUT 手动登录的账号(any) """ return cls(name=AliasAccount.INPUT.label, username=AliasAccount.INPUT.value, secret=None) + @lazyproperty + def versions(self): + return self.history.count() + @classmethod def get_user_account(cls): """ @USER 动态用户的账号(self) """ diff --git a/apps/accounts/serializers/account/account.py b/apps/accounts/serializers/account/account.py index 34333cfe4..9198dc1d7 100644 --- a/apps/accounts/serializers/account/account.py +++ b/apps/accounts/serializers/account/account.py @@ -102,8 +102,8 @@ class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer): class Meta(BaseAccountSerializer.Meta): model = Account fields = BaseAccountSerializer.Meta.fields + [ - 'su_from', 'asset', 'template', - 'push_now', 'source', 'connectivity' + 'su_from', 'asset', 'template', 'version', + 'push_now', 'source', 'connectivity', ] extra_kwargs = { **BaseAccountSerializer.Meta.extra_kwargs, @@ -118,7 +118,8 @@ class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer): @classmethod def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ - queryset = queryset.prefetch_related('asset', 'asset__platform') + queryset = queryset \ + .prefetch_related('asset', 'asset__platform') return queryset diff --git a/apps/accounts/signal_handlers.py b/apps/accounts/signal_handlers.py index bb4eaedb8..b47588192 100644 --- a/apps/accounts/signal_handlers.py +++ b/apps/accounts/signal_handlers.py @@ -1,3 +1,15 @@ +from django.db.models.signals import pre_save +from django.dispatch import receiver + from common.utils import get_logger +from .models import Account logger = get_logger(__name__) + + +@receiver(pre_save, sender=Account) +def on_account_pre_save(sender, instance, created=False, **kwargs): + if created: + instance.version = 1 + else: + instance.version = instance.history.count() diff --git a/apps/locale/zh/LC_MESSAGES/django.po b/apps/locale/zh/LC_MESSAGES/django.po index c3f661344..9ddcbed50 100644 --- a/apps/locale/zh/LC_MESSAGES/django.po +++ b/apps/locale/zh/LC_MESSAGES/django.po @@ -1954,7 +1954,7 @@ msgstr "清理审计会话任务日志" #: authentication/api/confirm.py:40 msgid "This action require verify your MFA" -msgstr "此操作需要验证您的 MFA" +msgstr "该操作需要验证您的 MFA, 请先开启并配置" #: authentication/api/connection_token.py:268 msgid "Account not found"