diff --git a/apps/accounts/models/account.py b/apps/accounts/models/account.py index 4ea416778..0b38d696b 100644 --- a/apps/accounts/models/account.py +++ b/apps/accounts/models/account.py @@ -2,10 +2,10 @@ from django.db import models from django.utils.translation import gettext_lazy as _ from simple_history.models import HistoricalRecords -from common.utils import lazyproperty -from ..const import AliasAccount, Source from assets.models.base import AbsConnectivity +from common.utils import lazyproperty from .base import BaseAccount +from ..const import AliasAccount, Source __all__ = ['Account', 'AccountTemplate'] @@ -98,6 +98,14 @@ class Account(AbsConnectivity, BaseAccount): """ 排除自己和以自己为 su-from 的账号 """ return self.asset.accounts.exclude(id=self.id).exclude(su_from=self) + def secret_changed(self): + history = self.history.first() + if not history: + return True + if history.secret != self.secret or history.secret_type != self.secret_type: + return True + return False + class AccountTemplate(BaseAccount): class Meta: diff --git a/apps/accounts/signal_handlers.py b/apps/accounts/signal_handlers.py index 1323a17be..df2b0e5b7 100644 --- a/apps/accounts/signal_handlers.py +++ b/apps/accounts/signal_handlers.py @@ -1,9 +1,6 @@ -from django.db.models.signals import post_save from django.db.models.signals import pre_save from django.dispatch import receiver -from assets.models import Asset -from common.decorators import on_transaction_commit from common.utils import get_logger from .models import Account @@ -11,16 +8,10 @@ logger = get_logger(__name__) @receiver(pre_save, sender=Account) -def on_account_pre_create(sender, instance, **kwargs): - # 升级版本号 - instance.version += 1 +def on_account_pre_create(sender, instance, update_fields=(), **kwargs): + # 这是创建时 + if instance.version == 0 or instance.secret_changed(): + instance.version += 1 + # 即使在 root 组织也不怕 instance.org_id = instance.asset.org_id - - -@receiver(post_save, sender=Asset) -@on_transaction_commit -def on_asset_create(sender, instance, created=False, **kwargs): - if not created: - return - # PushAccountManager.trigger_by_asset_create(instance) diff --git a/apps/ops/migrations/0023_auto_20220912_0021.py b/apps/ops/migrations/0023_auto_20220912_0021.py index edf8b0eeb..18c29eb67 100644 --- a/apps/ops/migrations/0023_auto_20220912_0021.py +++ b/apps/ops/migrations/0023_auto_20220912_0021.py @@ -227,7 +227,7 @@ class Migration(migrations.Migration): verbose_name='Creator')), ], options={ - 'verbose_name': 'AdHoc', + 'verbose_name': 'Adhoc', }, ), migrations.CreateModel(