perf: account history record only secret

pull/8970/head
feng626 2022-10-09 17:43:13 +08:00 committed by 老广
parent 237e7b22fb
commit cd98ec4cac
3 changed files with 94 additions and 2 deletions

View File

@ -10,7 +10,7 @@ __all__ = ['AccountHistoryViewSet', 'AccountHistorySecretsViewSet']
class AccountHistoryFilterSet(AccountFilterSet):
class Meta:
model = Account.history.model
fields = AccountFilterSet.Meta.fields
fields = ['id', 'secret_type']
class AccountHistoryViewSet(AccountViewSet):

View File

@ -0,0 +1,73 @@
# Generated by Django 3.2.13 on 2022-10-09 08:50
from django.db import migrations
def migrate_create_history_account(apps, schema_editor):
db_alias = schema_editor.connection.alias
account_model = apps.get_model('assets', 'Account')
history_account_model = apps.get_model('assets', 'HistoricalAccount')
history_accounts = []
for account in account_model.objects.using(db_alias).all():
data = {
'id': account.id,
'secret': account.secret,
'secret_type': account.secret_type,
'history_date': account.date_created,
}
history_accounts.append(history_account_model(**data))
history_account_model.objects.using(db_alias).bulk_create(history_accounts)
class Migration(migrations.Migration):
dependencies = [
('assets', '0106_auto_20220916_1556'),
]
operations = [
migrations.RemoveField(
model_name='historicalaccount',
name='asset',
),
migrations.RemoveField(
model_name='historicalaccount',
name='comment',
),
migrations.RemoveField(
model_name='historicalaccount',
name='created_by',
),
migrations.RemoveField(
model_name='historicalaccount',
name='date_created',
),
migrations.RemoveField(
model_name='historicalaccount',
name='date_updated',
),
migrations.RemoveField(
model_name='historicalaccount',
name='name',
),
migrations.RemoveField(
model_name='historicalaccount',
name='org_id',
),
migrations.RemoveField(
model_name='historicalaccount',
name='privileged',
),
migrations.RemoveField(
model_name='historicalaccount',
name='su_from',
),
migrations.RemoveField(
model_name='historicalaccount',
name='username',
),
migrations.RemoveField(
model_name='historicalaccount',
name='version',
),
migrations.RunPython(migrate_create_history_account),
]

View File

@ -9,6 +9,23 @@ from .base import BaseAccount
__all__ = ['Account', 'AccountTemplate']
class AccountHistoricalRecords(HistoricalRecords):
def __init__(self, *args, **kwargs):
self.included_fields = kwargs.pop('included_fields', None)
super().__init__(*args, **kwargs)
def fields_included(self, model):
fields = []
for field in model._meta.fields:
if self.included_fields is None:
if field.name not in self.excluded_fields:
fields.append(field)
else:
if field.name in self.included_fields:
fields.append(field)
return fields
class Account(BaseAccount):
class InnerAccount(models.TextChoices):
INPUT = '@INPUT', '@INPUT'
@ -23,7 +40,9 @@ class Account(BaseAccount):
on_delete=models.SET_NULL, verbose_name=_("Su from")
)
version = models.IntegerField(default=0, verbose_name=_('Version'))
history = HistoricalRecords()
history = AccountHistoricalRecords(
included_fields=['id', 'secret_type', 'secret']
)
class Meta:
verbose_name = _('Account')