perf: 优化账号历史

pull/9693/head
ibuler 2 years ago
parent ee3c548b6b
commit 5d8d0a320b

@ -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

@ -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) """

@ -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

@ -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()

@ -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"

Loading…
Cancel
Save