diff --git a/apps/assets/api/account/history.py b/apps/assets/api/account/history.py index 0ddc659ad..cfcbfeb8e 100644 --- a/apps/assets/api/account/history.py +++ b/apps/assets/api/account/history.py @@ -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): diff --git a/apps/assets/migrations/0107_account_history.py b/apps/assets/migrations/0107_account_history.py new file mode 100644 index 000000000..66ec1e36b --- /dev/null +++ b/apps/assets/migrations/0107_account_history.py @@ -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), + ] diff --git a/apps/assets/models/account.py b/apps/assets/models/account.py index 4394f6583..faf503b91 100644 --- a/apps/assets/models/account.py +++ b/apps/assets/models/account.py @@ -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')