mirror of https://github.com/jumpserver/jumpserver
perf: account history record only secret
parent
237e7b22fb
commit
cd98ec4cac
|
@ -10,7 +10,7 @@ __all__ = ['AccountHistoryViewSet', 'AccountHistorySecretsViewSet']
|
||||||
class AccountHistoryFilterSet(AccountFilterSet):
|
class AccountHistoryFilterSet(AccountFilterSet):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Account.history.model
|
model = Account.history.model
|
||||||
fields = AccountFilterSet.Meta.fields
|
fields = ['id', 'secret_type']
|
||||||
|
|
||||||
|
|
||||||
class AccountHistoryViewSet(AccountViewSet):
|
class AccountHistoryViewSet(AccountViewSet):
|
||||||
|
|
|
@ -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),
|
||||||
|
]
|
|
@ -9,6 +9,23 @@ from .base import BaseAccount
|
||||||
__all__ = ['Account', 'AccountTemplate']
|
__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 Account(BaseAccount):
|
||||||
class InnerAccount(models.TextChoices):
|
class InnerAccount(models.TextChoices):
|
||||||
INPUT = '@INPUT', '@INPUT'
|
INPUT = '@INPUT', '@INPUT'
|
||||||
|
@ -23,7 +40,9 @@ class Account(BaseAccount):
|
||||||
on_delete=models.SET_NULL, verbose_name=_("Su from")
|
on_delete=models.SET_NULL, verbose_name=_("Su from")
|
||||||
)
|
)
|
||||||
version = models.IntegerField(default=0, verbose_name=_('Version'))
|
version = models.IntegerField(default=0, verbose_name=_('Version'))
|
||||||
history = HistoricalRecords()
|
history = AccountHistoricalRecords(
|
||||||
|
included_fields=['id', 'secret_type', 'secret']
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _('Account')
|
verbose_name = _('Account')
|
||||||
|
|
Loading…
Reference in New Issue